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 err = got_error_from_errno2("realpath", target_path);
1011 goto done;
1015 /* Only allow symlinks pointing at paths within the work tree. */
1016 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1017 abspath : target_path), worktree->root_path,
1018 strlen(worktree->root_path))) {
1019 /* install as a regular file */
1020 got_object_blob_rewind(blob);
1021 err = install_blob(worktree, ondisk_path, path,
1022 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1023 restoring_missing_file, reverting_versioned_file,
1024 repo, progress_cb, progress_arg);
1025 goto done;
1028 if (symlink(target_path, ondisk_path) == -1) {
1029 if (errno == EEXIST) {
1030 struct stat sb;
1031 ssize_t elen;
1032 char etarget[PATH_MAX];
1033 if (lstat(ondisk_path, &sb) == -1) {
1034 err = got_error_from_errno2("lstat",
1035 ondisk_path);
1036 goto done;
1038 if (!S_ISLNK(sb.st_mode)) {
1039 err = got_error_path(ondisk_path,
1040 GOT_ERR_FILE_OBSTRUCTED);
1041 goto done;
1043 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1044 if (elen == -1) {
1045 err = got_error_from_errno2("readlink",
1046 ondisk_path);
1047 goto done;
1049 if (elen == target_len &&
1050 memcmp(etarget, target_path, target_len) == 0) {
1051 err = NULL; /* nothing to do */
1052 goto done;
1053 } else {
1054 if (unlink(ondisk_path) == -1) {
1055 err = got_error_from_errno2("unlink",
1056 ondisk_path);
1057 goto done;
1059 if (symlink(target_path, ondisk_path) == -1) {
1060 err = got_error_from_errno3("symlink",
1061 target_path, ondisk_path);
1062 goto done;
1065 err = (*progress_cb)(progress_arg,
1066 GOT_STATUS_UPDATE, path);
1067 goto done;
1071 if (errno == ENOENT) {
1072 char *parent = dirname(ondisk_path);
1073 if (parent == NULL) {
1074 err = got_error_from_errno2("dirname",
1075 ondisk_path);
1076 goto done;
1078 err = add_dir_on_disk(worktree, parent);
1079 if (err)
1080 goto done;
1082 * Retry, and fall through to error handling
1083 * below if this second attempt fails.
1085 if (symlink(target_path, ondisk_path) != -1) {
1086 err = NULL; /* success */
1087 goto done;
1091 /* Handle errors from first or second creation attempt. */
1092 if (errno == ENAMETOOLONG) {
1093 /* bad target path; install as a regular file */
1094 got_object_blob_rewind(blob);
1095 err = install_blob(worktree, ondisk_path, path,
1096 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1097 restoring_missing_file, reverting_versioned_file,
1098 repo, progress_cb, progress_arg);
1099 } else if (errno == ENOTDIR) {
1100 err = got_error_path(ondisk_path,
1101 GOT_ERR_FILE_OBSTRUCTED);
1102 } else {
1103 err = got_error_from_errno3("symlink",
1104 target_path, ondisk_path);
1106 } else
1107 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1108 done:
1109 free(resolved_path);
1110 free(abspath);
1111 return err;
1114 static const struct got_error *
1115 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1116 const char *path, mode_t te_mode, mode_t st_mode,
1117 struct got_blob_object *blob, int restoring_missing_file,
1118 int reverting_versioned_file, struct got_repository *repo,
1119 got_worktree_checkout_cb progress_cb, void *progress_arg)
1121 const struct got_error *err = NULL;
1122 int fd = -1;
1123 size_t len, hdrlen;
1124 int update = 0;
1125 char *tmppath = NULL;
1127 if (S_ISLNK(te_mode))
1128 return install_symlink(worktree, ondisk_path, path, te_mode,
1129 st_mode, blob, restoring_missing_file,
1130 reverting_versioned_file, repo, progress_cb, progress_arg);
1132 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1133 GOT_DEFAULT_FILE_MODE);
1134 if (fd == -1) {
1135 if (errno == ENOENT) {
1136 char *parent = dirname(path);
1137 if (parent == NULL)
1138 return got_error_from_errno2("dirname", path);
1139 err = add_dir_on_disk(worktree, parent);
1140 if (err)
1141 return err;
1142 fd = open(ondisk_path,
1143 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1144 GOT_DEFAULT_FILE_MODE);
1145 if (fd == -1)
1146 return got_error_from_errno2("open",
1147 ondisk_path);
1148 } else if (errno == EEXIST) {
1149 if (!S_ISREG(st_mode)) {
1150 /* TODO file is obstructed; do something */
1151 err = got_error_path(ondisk_path,
1152 GOT_ERR_FILE_OBSTRUCTED);
1153 goto done;
1154 } else {
1155 err = got_opentemp_named_fd(&tmppath, &fd,
1156 ondisk_path);
1157 if (err)
1158 goto done;
1159 update = 1;
1161 } else
1162 return got_error_from_errno2("open", ondisk_path);
1165 if (restoring_missing_file)
1166 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1167 else if (reverting_versioned_file)
1168 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1169 else
1170 err = (*progress_cb)(progress_arg,
1171 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1172 if (err)
1173 goto done;
1175 hdrlen = got_object_blob_get_hdrlen(blob);
1176 do {
1177 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1178 err = got_object_blob_read_block(&len, blob);
1179 if (err)
1180 break;
1181 if (len > 0) {
1182 /* Skip blob object header first time around. */
1183 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1184 if (outlen == -1) {
1185 err = got_error_from_errno("write");
1186 goto done;
1187 } else if (outlen != len - hdrlen) {
1188 err = got_error(GOT_ERR_IO);
1189 goto done;
1191 hdrlen = 0;
1193 } while (len != 0);
1195 if (fsync(fd) != 0) {
1196 err = got_error_from_errno("fsync");
1197 goto done;
1200 if (update) {
1201 if (rename(tmppath, ondisk_path) != 0) {
1202 err = got_error_from_errno3("rename", tmppath,
1203 ondisk_path);
1204 unlink(tmppath);
1205 goto done;
1209 if (chmod(ondisk_path,
1210 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1211 err = got_error_from_errno2("chmod", ondisk_path);
1212 goto done;
1215 done:
1216 if (fd != -1 && close(fd) != 0 && err == NULL)
1217 err = got_error_from_errno("close");
1218 free(tmppath);
1219 return err;
1222 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1223 static const struct got_error *
1224 get_modified_file_content_status(unsigned char *status, FILE *f)
1226 const struct got_error *err = NULL;
1227 const char *markers[3] = {
1228 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1229 GOT_DIFF_CONFLICT_MARKER_SEP,
1230 GOT_DIFF_CONFLICT_MARKER_END
1232 int i = 0;
1233 char *line;
1234 size_t len;
1235 const char delim[3] = {'\0', '\0', '\0'};
1237 while (*status == GOT_STATUS_MODIFY) {
1238 line = fparseln(f, &len, NULL, delim, 0);
1239 if (line == NULL) {
1240 if (feof(f))
1241 break;
1242 err = got_ferror(f, GOT_ERR_IO);
1243 break;
1246 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1247 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1248 == 0)
1249 *status = GOT_STATUS_CONFLICT;
1250 else
1251 i++;
1255 return err;
1258 static int
1259 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1261 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1262 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1265 static int
1266 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1268 return !(ie->ctime_sec == sb->st_ctime &&
1269 ie->ctime_nsec == sb->st_ctimensec &&
1270 ie->mtime_sec == sb->st_mtime &&
1271 ie->mtime_nsec == sb->st_mtimensec &&
1272 ie->size == (sb->st_size & 0xffffffff) &&
1273 !xbit_differs(ie, sb->st_mode));
1276 static unsigned char
1277 get_staged_status(struct got_fileindex_entry *ie)
1279 switch (got_fileindex_entry_stage_get(ie)) {
1280 case GOT_FILEIDX_STAGE_ADD:
1281 return GOT_STATUS_ADD;
1282 case GOT_FILEIDX_STAGE_DELETE:
1283 return GOT_STATUS_DELETE;
1284 case GOT_FILEIDX_STAGE_MODIFY:
1285 return GOT_STATUS_MODIFY;
1286 default:
1287 return GOT_STATUS_NO_CHANGE;
1291 static const struct got_error *
1292 get_symlink_status(unsigned char *status, struct stat *sb,
1293 struct got_fileindex_entry *ie, const char *abspath,
1294 int dirfd, const char *de_name, struct got_blob_object *blob)
1296 const struct got_error *err = NULL;
1297 char target_path[PATH_MAX];
1298 char etarget[PATH_MAX];
1299 ssize_t elen;
1300 size_t len, target_len = 0;
1301 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1302 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1304 *status = GOT_STATUS_NO_CHANGE;
1306 /* Blob object content specifies the target path of the link. */
1307 do {
1308 err = got_object_blob_read_block(&len, blob);
1309 if (err)
1310 return err;
1311 if (len + target_len >= sizeof(target_path)) {
1313 * Should not happen. The blob contents were OK
1314 * when this symlink was installed.
1316 return got_error(GOT_ERR_NO_SPACE);
1318 if (len > 0) {
1319 /* Skip blob object header first time around. */
1320 memcpy(target_path + target_len, buf + hdrlen,
1321 len - hdrlen);
1322 target_len += len - hdrlen;
1323 hdrlen = 0;
1325 } while (len != 0);
1326 target_path[target_len] = '\0';
1328 if (dirfd != -1) {
1329 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1330 if (elen == -1)
1331 return got_error_from_errno2("readlinkat", abspath);
1332 } else {
1333 elen = readlink(abspath, etarget, sizeof(etarget));
1334 if (elen == -1)
1335 return got_error_from_errno2("readlinkat", abspath);
1338 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1339 *status = GOT_STATUS_MODIFY;
1341 return NULL;
1344 static const struct got_error *
1345 get_file_status(unsigned char *status, struct stat *sb,
1346 struct got_fileindex_entry *ie, const char *abspath,
1347 int dirfd, const char *de_name, struct got_repository *repo)
1349 const struct got_error *err = NULL;
1350 struct got_object_id id;
1351 size_t hdrlen;
1352 int fd = -1;
1353 FILE *f = NULL;
1354 uint8_t fbuf[8192];
1355 struct got_blob_object *blob = NULL;
1356 size_t flen, blen;
1357 unsigned char staged_status = get_staged_status(ie);
1359 *status = GOT_STATUS_NO_CHANGE;
1362 * Whenever the caller provides a directory descriptor and a
1363 * directory entry name for the file, use them! This prevents
1364 * race conditions if filesystem paths change beneath our feet.
1366 if (dirfd != -1) {
1367 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1368 if (errno == ENOENT) {
1369 if (got_fileindex_entry_has_file_on_disk(ie))
1370 *status = GOT_STATUS_MISSING;
1371 else
1372 *status = GOT_STATUS_DELETE;
1373 goto done;
1375 err = got_error_from_errno2("fstatat", abspath);
1376 goto done;
1378 } else {
1379 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1380 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1381 return got_error_from_errno2("open", abspath);
1382 else if (fd == -1 && errno == ELOOP) {
1383 if (lstat(abspath, sb) == -1)
1384 return got_error_from_errno2("lstat", abspath);
1385 } else if (fd == -1 || fstat(fd, sb) == -1) {
1386 if (errno == ENOENT) {
1387 if (got_fileindex_entry_has_file_on_disk(ie))
1388 *status = GOT_STATUS_MISSING;
1389 else
1390 *status = GOT_STATUS_DELETE;
1391 goto done;
1393 err = got_error_from_errno2("fstat", abspath);
1394 goto done;
1398 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1399 *status = GOT_STATUS_OBSTRUCTED;
1400 goto done;
1403 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1404 *status = GOT_STATUS_DELETE;
1405 goto done;
1406 } else if (!got_fileindex_entry_has_blob(ie) &&
1407 staged_status != GOT_STATUS_ADD) {
1408 *status = GOT_STATUS_ADD;
1409 goto done;
1412 if (!stat_info_differs(ie, sb))
1413 goto done;
1415 if (staged_status == GOT_STATUS_MODIFY ||
1416 staged_status == GOT_STATUS_ADD)
1417 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1418 else
1419 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1421 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1422 if (err)
1423 goto done;
1425 if (S_ISLNK(sb->st_mode)) {
1426 /* Staging changes to symlinks is not yet(?) supported. */
1427 if (staged_status != GOT_STATUS_NO_CHANGE) {
1428 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1429 goto done;
1431 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1432 de_name, blob);
1433 goto done;
1437 if (dirfd != -1) {
1438 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1439 if (fd == -1) {
1440 err = got_error_from_errno2("openat", abspath);
1441 goto done;
1445 f = fdopen(fd, "r");
1446 if (f == NULL) {
1447 err = got_error_from_errno2("fdopen", abspath);
1448 goto done;
1450 fd = -1;
1451 hdrlen = got_object_blob_get_hdrlen(blob);
1452 for (;;) {
1453 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1454 err = got_object_blob_read_block(&blen, blob);
1455 if (err)
1456 goto done;
1457 /* Skip length of blob object header first time around. */
1458 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1459 if (flen == 0 && ferror(f)) {
1460 err = got_error_from_errno("fread");
1461 goto done;
1463 if (blen == 0) {
1464 if (flen != 0)
1465 *status = GOT_STATUS_MODIFY;
1466 break;
1467 } else if (flen == 0) {
1468 if (blen != 0)
1469 *status = GOT_STATUS_MODIFY;
1470 break;
1471 } else if (blen - hdrlen == flen) {
1472 /* Skip blob object header first time around. */
1473 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1474 *status = GOT_STATUS_MODIFY;
1475 break;
1477 } else {
1478 *status = GOT_STATUS_MODIFY;
1479 break;
1481 hdrlen = 0;
1484 if (*status == GOT_STATUS_MODIFY) {
1485 rewind(f);
1486 err = get_modified_file_content_status(status, f);
1487 } else if (xbit_differs(ie, sb->st_mode))
1488 *status = GOT_STATUS_MODE_CHANGE;
1489 done:
1490 if (blob)
1491 got_object_blob_close(blob);
1492 if (f != NULL && fclose(f) == EOF && err == NULL)
1493 err = got_error_from_errno2("fclose", abspath);
1494 if (fd != -1 && close(fd) == -1 && err == NULL)
1495 err = got_error_from_errno2("close", abspath);
1496 return err;
1500 * Update timestamps in the file index if a file is unmodified and
1501 * we had to run a full content comparison to find out.
1503 static const struct got_error *
1504 sync_timestamps(char *ondisk_path, unsigned char status,
1505 struct got_fileindex_entry *ie, struct stat *sb)
1507 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1508 return got_fileindex_entry_update(ie, ondisk_path,
1509 ie->blob_sha1, ie->commit_sha1, 1);
1511 return NULL;
1514 static const struct got_error *
1515 update_blob(struct got_worktree *worktree,
1516 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1517 struct got_tree_entry *te, const char *path,
1518 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1519 void *progress_arg)
1521 const struct got_error *err = NULL;
1522 struct got_blob_object *blob = NULL;
1523 char *ondisk_path;
1524 unsigned char status = GOT_STATUS_NO_CHANGE;
1525 struct stat sb;
1527 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1528 return got_error_from_errno("asprintf");
1530 if (ie) {
1531 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1532 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1533 goto done;
1535 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1536 repo);
1537 if (err)
1538 goto done;
1539 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1540 sb.st_mode = got_fileindex_perms_to_st(ie);
1541 } else
1542 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1544 if (status == GOT_STATUS_OBSTRUCTED) {
1545 err = (*progress_cb)(progress_arg, status, path);
1546 goto done;
1548 if (status == GOT_STATUS_CONFLICT) {
1549 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1550 path);
1551 goto done;
1554 if (ie && status != GOT_STATUS_MISSING &&
1555 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1556 if (got_fileindex_entry_has_commit(ie) &&
1557 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1558 SHA1_DIGEST_LENGTH) == 0) {
1559 err = sync_timestamps(ondisk_path, status, ie, &sb);
1560 if (err)
1561 goto done;
1562 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1563 path);
1564 goto done;
1566 if (got_fileindex_entry_has_blob(ie) &&
1567 memcmp(ie->blob_sha1, te->id.sha1,
1568 SHA1_DIGEST_LENGTH) == 0) {
1569 err = sync_timestamps(ondisk_path, status, ie, &sb);
1570 goto done;
1574 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1575 if (err)
1576 goto done;
1578 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1579 int update_timestamps;
1580 struct got_blob_object *blob2 = NULL;
1581 char *label_orig = NULL;
1582 if (got_fileindex_entry_has_blob(ie)) {
1583 struct got_object_id id2;
1584 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1585 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1586 if (err)
1587 goto done;
1589 if (got_fileindex_entry_has_commit(ie)) {
1590 char id_str[SHA1_DIGEST_STRING_LENGTH];
1591 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1592 sizeof(id_str)) == NULL) {
1593 err = got_error_path(id_str,
1594 GOT_ERR_BAD_OBJ_ID_STR);
1595 goto done;
1597 if (asprintf(&label_orig, "%s: commit %s",
1598 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1599 err = got_error_from_errno("asprintf");
1600 goto done;
1603 err = merge_blob(&update_timestamps, worktree, blob2,
1604 ondisk_path, path, sb.st_mode, label_orig, blob,
1605 worktree->base_commit_id, repo,
1606 progress_cb, progress_arg);
1607 free(label_orig);
1608 if (blob2)
1609 got_object_blob_close(blob2);
1610 if (err)
1611 goto done;
1613 * Do not update timestamps of files with local changes.
1614 * Otherwise, a future status walk would treat them as
1615 * unmodified files again.
1617 err = got_fileindex_entry_update(ie, ondisk_path,
1618 blob->id.sha1, worktree->base_commit_id->sha1,
1619 update_timestamps);
1620 } else if (status == GOT_STATUS_MODE_CHANGE) {
1621 err = got_fileindex_entry_update(ie, ondisk_path,
1622 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1623 } else if (status == GOT_STATUS_DELETE) {
1624 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1625 if (err)
1626 goto done;
1627 err = got_fileindex_entry_update(ie, ondisk_path,
1628 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1629 if (err)
1630 goto done;
1631 } else {
1632 err = install_blob(worktree, ondisk_path, path, te->mode,
1633 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1634 repo, progress_cb, progress_arg);
1635 if (err)
1636 goto done;
1637 if (ie) {
1638 err = got_fileindex_entry_update(ie, ondisk_path,
1639 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1640 } else {
1641 err = create_fileindex_entry(fileindex,
1642 worktree->base_commit_id, ondisk_path, path,
1643 &blob->id);
1645 if (err)
1646 goto done;
1648 got_object_blob_close(blob);
1649 done:
1650 free(ondisk_path);
1651 return err;
1654 static const struct got_error *
1655 remove_ondisk_file(const char *root_path, const char *path)
1657 const struct got_error *err = NULL;
1658 char *ondisk_path = NULL;
1660 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1661 return got_error_from_errno("asprintf");
1663 if (unlink(ondisk_path) == -1) {
1664 if (errno != ENOENT)
1665 err = got_error_from_errno2("unlink", ondisk_path);
1666 } else {
1667 char *parent = dirname(ondisk_path);
1668 while (parent && strcmp(parent, root_path) != 0) {
1669 if (rmdir(parent) == -1) {
1670 if (errno != ENOTEMPTY)
1671 err = got_error_from_errno2("rmdir",
1672 parent);
1673 break;
1675 parent = dirname(parent);
1678 free(ondisk_path);
1679 return err;
1682 static const struct got_error *
1683 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1684 struct got_fileindex_entry *ie, struct got_repository *repo,
1685 got_worktree_checkout_cb progress_cb, void *progress_arg)
1687 const struct got_error *err = NULL;
1688 unsigned char status;
1689 struct stat sb;
1690 char *ondisk_path;
1692 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1693 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1695 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1696 == -1)
1697 return got_error_from_errno("asprintf");
1699 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1700 if (err)
1701 goto done;
1703 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1704 status == GOT_STATUS_ADD) {
1705 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1706 if (err)
1707 goto done;
1709 * Preserve the working file and change the deleted blob's
1710 * entry into a schedule-add entry.
1712 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1713 0);
1714 } else {
1715 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1716 if (err)
1717 goto done;
1718 if (status == GOT_STATUS_NO_CHANGE) {
1719 err = remove_ondisk_file(worktree->root_path, ie->path);
1720 if (err)
1721 goto done;
1723 got_fileindex_entry_remove(fileindex, ie);
1725 done:
1726 free(ondisk_path);
1727 return err;
1730 struct diff_cb_arg {
1731 struct got_fileindex *fileindex;
1732 struct got_worktree *worktree;
1733 struct got_repository *repo;
1734 got_worktree_checkout_cb progress_cb;
1735 void *progress_arg;
1736 got_cancel_cb cancel_cb;
1737 void *cancel_arg;
1740 static const struct got_error *
1741 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1742 struct got_tree_entry *te, const char *parent_path)
1744 struct diff_cb_arg *a = arg;
1746 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1747 return got_error(GOT_ERR_CANCELLED);
1749 return update_blob(a->worktree, a->fileindex, ie, te,
1750 ie->path, a->repo, a->progress_cb, a->progress_arg);
1753 static const struct got_error *
1754 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1756 struct diff_cb_arg *a = arg;
1758 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1759 return got_error(GOT_ERR_CANCELLED);
1761 return delete_blob(a->worktree, a->fileindex, ie,
1762 a->repo, a->progress_cb, a->progress_arg);
1765 static const struct got_error *
1766 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1768 struct diff_cb_arg *a = arg;
1769 const struct got_error *err;
1770 char *path;
1772 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1773 return got_error(GOT_ERR_CANCELLED);
1775 if (got_object_tree_entry_is_submodule(te))
1776 return NULL;
1778 if (asprintf(&path, "%s%s%s", parent_path,
1779 parent_path[0] ? "/" : "", te->name)
1780 == -1)
1781 return got_error_from_errno("asprintf");
1783 if (S_ISDIR(te->mode))
1784 err = add_dir_on_disk(a->worktree, path);
1785 else
1786 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1787 a->repo, a->progress_cb, a->progress_arg);
1789 free(path);
1790 return err;
1793 static const struct got_error *
1794 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1796 const struct got_error *err = NULL;
1797 char *uuidstr = NULL;
1798 uint32_t uuid_status;
1800 *refname = NULL;
1802 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1803 if (uuid_status != uuid_s_ok)
1804 return got_error_uuid(uuid_status, "uuid_to_string");
1806 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1807 == -1) {
1808 err = got_error_from_errno("asprintf");
1809 *refname = NULL;
1811 free(uuidstr);
1812 return err;
1815 const struct got_error *
1816 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1818 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1821 static const struct got_error *
1822 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1824 return get_ref_name(refname, worktree,
1825 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1828 static const struct got_error *
1829 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1831 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1834 static const struct got_error *
1835 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1837 return get_ref_name(refname, worktree,
1838 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1841 static const struct got_error *
1842 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1844 return get_ref_name(refname, worktree,
1845 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1848 static const struct got_error *
1849 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1851 return get_ref_name(refname, worktree,
1852 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1855 static const struct got_error *
1856 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1858 return get_ref_name(refname, worktree,
1859 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1862 static const struct got_error *
1863 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1865 return get_ref_name(refname, worktree,
1866 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1869 static const struct got_error *
1870 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1872 return get_ref_name(refname, worktree,
1873 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1876 const struct got_error *
1877 got_worktree_get_histedit_script_path(char **path,
1878 struct got_worktree *worktree)
1880 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1881 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1882 *path = NULL;
1883 return got_error_from_errno("asprintf");
1885 return NULL;
1889 * Prevent Git's garbage collector from deleting our base commit by
1890 * setting a reference to our base commit's ID.
1892 static const struct got_error *
1893 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1895 const struct got_error *err = NULL;
1896 struct got_reference *ref = NULL;
1897 char *refname;
1899 err = got_worktree_get_base_ref_name(&refname, worktree);
1900 if (err)
1901 return err;
1903 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1904 if (err)
1905 goto done;
1907 err = got_ref_write(ref, repo);
1908 done:
1909 free(refname);
1910 if (ref)
1911 got_ref_close(ref);
1912 return err;
1915 static const struct got_error *
1916 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1918 const struct got_error *err = NULL;
1920 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1921 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1922 err = got_error_from_errno("asprintf");
1923 *fileindex_path = NULL;
1925 return err;
1929 static const struct got_error *
1930 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1931 struct got_worktree *worktree)
1933 const struct got_error *err = NULL;
1934 FILE *index = NULL;
1936 *fileindex_path = NULL;
1937 *fileindex = got_fileindex_alloc();
1938 if (*fileindex == NULL)
1939 return got_error_from_errno("got_fileindex_alloc");
1941 err = get_fileindex_path(fileindex_path, worktree);
1942 if (err)
1943 goto done;
1945 index = fopen(*fileindex_path, "rb");
1946 if (index == NULL) {
1947 if (errno != ENOENT)
1948 err = got_error_from_errno2("fopen", *fileindex_path);
1949 } else {
1950 err = got_fileindex_read(*fileindex, index);
1951 if (fclose(index) != 0 && err == NULL)
1952 err = got_error_from_errno("fclose");
1954 done:
1955 if (err) {
1956 free(*fileindex_path);
1957 *fileindex_path = NULL;
1958 got_fileindex_free(*fileindex);
1959 *fileindex = NULL;
1961 return err;
1964 struct bump_base_commit_id_arg {
1965 struct got_object_id *base_commit_id;
1966 const char *path;
1967 size_t path_len;
1968 const char *entry_name;
1969 got_worktree_checkout_cb progress_cb;
1970 void *progress_arg;
1973 /* Bump base commit ID of all files within an updated part of the work tree. */
1974 static const struct got_error *
1975 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1977 const struct got_error *err;
1978 struct bump_base_commit_id_arg *a = arg;
1980 if (a->entry_name) {
1981 if (strcmp(ie->path, a->path) != 0)
1982 return NULL;
1983 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1984 return NULL;
1986 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1987 SHA1_DIGEST_LENGTH) == 0)
1988 return NULL;
1990 if (a->progress_cb) {
1991 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1992 ie->path);
1993 if (err)
1994 return err;
1996 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1997 return NULL;
2000 static const struct got_error *
2001 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2003 const struct got_error *err = NULL;
2004 char *new_fileindex_path = NULL;
2005 FILE *new_index = NULL;
2006 struct timespec timeout;
2008 err = got_opentemp_named(&new_fileindex_path, &new_index,
2009 fileindex_path);
2010 if (err)
2011 goto done;
2013 err = got_fileindex_write(fileindex, new_index);
2014 if (err)
2015 goto done;
2017 if (rename(new_fileindex_path, fileindex_path) != 0) {
2018 err = got_error_from_errno3("rename", new_fileindex_path,
2019 fileindex_path);
2020 unlink(new_fileindex_path);
2024 * Sleep for a short amount of time to ensure that files modified after
2025 * this program exits have a different time stamp from the one which
2026 * was recorded in the file index.
2028 timeout.tv_sec = 0;
2029 timeout.tv_nsec = 1;
2030 nanosleep(&timeout, NULL);
2031 done:
2032 if (new_index)
2033 fclose(new_index);
2034 free(new_fileindex_path);
2035 return err;
2038 static const struct got_error *
2039 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2040 struct got_object_id **tree_id, const char *wt_relpath,
2041 struct got_worktree *worktree, struct got_repository *repo)
2043 const struct got_error *err = NULL;
2044 struct got_object_id *id = NULL;
2045 char *in_repo_path = NULL;
2046 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2048 *entry_type = GOT_OBJ_TYPE_ANY;
2049 *tree_relpath = NULL;
2050 *tree_id = NULL;
2052 if (wt_relpath[0] == '\0') {
2053 /* Check out all files within the work tree. */
2054 *entry_type = GOT_OBJ_TYPE_TREE;
2055 *tree_relpath = strdup("");
2056 if (*tree_relpath == NULL) {
2057 err = got_error_from_errno("strdup");
2058 goto done;
2060 err = got_object_id_by_path(tree_id, repo,
2061 worktree->base_commit_id, worktree->path_prefix);
2062 if (err)
2063 goto done;
2064 return NULL;
2067 /* Check out a subset of files in the work tree. */
2069 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2070 is_root_wt ? "" : "/", wt_relpath) == -1) {
2071 err = got_error_from_errno("asprintf");
2072 goto done;
2075 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2076 in_repo_path);
2077 if (err)
2078 goto done;
2080 free(in_repo_path);
2081 in_repo_path = NULL;
2083 err = got_object_get_type(entry_type, repo, id);
2084 if (err)
2085 goto done;
2087 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2088 /* Check out a single file. */
2089 if (strchr(wt_relpath, '/') == NULL) {
2090 /* Check out a single file in work tree's root dir. */
2091 in_repo_path = strdup(worktree->path_prefix);
2092 if (in_repo_path == NULL) {
2093 err = got_error_from_errno("strdup");
2094 goto done;
2096 *tree_relpath = strdup("");
2097 if (*tree_relpath == NULL) {
2098 err = got_error_from_errno("strdup");
2099 goto done;
2101 } else {
2102 /* Check out a single file in a subdirectory. */
2103 err = got_path_dirname(tree_relpath, wt_relpath);
2104 if (err)
2105 return err;
2106 if (asprintf(&in_repo_path, "%s%s%s",
2107 worktree->path_prefix, is_root_wt ? "" : "/",
2108 *tree_relpath) == -1) {
2109 err = got_error_from_errno("asprintf");
2110 goto done;
2113 err = got_object_id_by_path(tree_id, repo,
2114 worktree->base_commit_id, in_repo_path);
2115 } else {
2116 /* Check out all files within a subdirectory. */
2117 *tree_id = got_object_id_dup(id);
2118 if (*tree_id == NULL) {
2119 err = got_error_from_errno("got_object_id_dup");
2120 goto done;
2122 *tree_relpath = strdup(wt_relpath);
2123 if (*tree_relpath == NULL) {
2124 err = got_error_from_errno("strdup");
2125 goto done;
2128 done:
2129 free(id);
2130 free(in_repo_path);
2131 if (err) {
2132 *entry_type = GOT_OBJ_TYPE_ANY;
2133 free(*tree_relpath);
2134 *tree_relpath = NULL;
2135 free(*tree_id);
2136 *tree_id = NULL;
2138 return err;
2141 static const struct got_error *
2142 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2143 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2144 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2145 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2147 const struct got_error *err = NULL;
2148 struct got_commit_object *commit = NULL;
2149 struct got_tree_object *tree = NULL;
2150 struct got_fileindex_diff_tree_cb diff_cb;
2151 struct diff_cb_arg arg;
2153 err = ref_base_commit(worktree, repo);
2154 if (err) {
2155 if (!(err->code == GOT_ERR_ERRNO &&
2156 (errno == EACCES || errno == EROFS)))
2157 goto done;
2158 err = (*progress_cb)(progress_arg,
2159 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2160 if (err)
2161 return err;
2164 err = got_object_open_as_commit(&commit, repo,
2165 worktree->base_commit_id);
2166 if (err)
2167 goto done;
2169 err = got_object_open_as_tree(&tree, repo, tree_id);
2170 if (err)
2171 goto done;
2173 if (entry_name &&
2174 got_object_tree_find_entry(tree, entry_name) == NULL) {
2175 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2176 goto done;
2179 diff_cb.diff_old_new = diff_old_new;
2180 diff_cb.diff_old = diff_old;
2181 diff_cb.diff_new = diff_new;
2182 arg.fileindex = fileindex;
2183 arg.worktree = worktree;
2184 arg.repo = repo;
2185 arg.progress_cb = progress_cb;
2186 arg.progress_arg = progress_arg;
2187 arg.cancel_cb = cancel_cb;
2188 arg.cancel_arg = cancel_arg;
2189 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2190 entry_name, repo, &diff_cb, &arg);
2191 done:
2192 if (tree)
2193 got_object_tree_close(tree);
2194 if (commit)
2195 got_object_commit_close(commit);
2196 return err;
2199 const struct got_error *
2200 got_worktree_checkout_files(struct got_worktree *worktree,
2201 struct got_pathlist_head *paths, struct got_repository *repo,
2202 got_worktree_checkout_cb progress_cb, void *progress_arg,
2203 got_cancel_cb cancel_cb, void *cancel_arg)
2205 const struct got_error *err = NULL, *sync_err, *unlockerr;
2206 struct got_commit_object *commit = NULL;
2207 struct got_tree_object *tree = NULL;
2208 struct got_fileindex *fileindex = NULL;
2209 char *fileindex_path = NULL;
2210 struct got_pathlist_entry *pe;
2211 struct tree_path_data {
2212 SIMPLEQ_ENTRY(tree_path_data) entry;
2213 struct got_object_id *tree_id;
2214 int entry_type;
2215 char *relpath;
2216 char *entry_name;
2217 } *tpd = NULL;
2218 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2220 SIMPLEQ_INIT(&tree_paths);
2222 err = lock_worktree(worktree, LOCK_EX);
2223 if (err)
2224 return err;
2226 /* Map all specified paths to in-repository trees. */
2227 TAILQ_FOREACH(pe, paths, entry) {
2228 tpd = malloc(sizeof(*tpd));
2229 if (tpd == NULL) {
2230 err = got_error_from_errno("malloc");
2231 goto done;
2234 err = find_tree_entry_for_checkout(&tpd->entry_type,
2235 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2236 if (err) {
2237 free(tpd);
2238 goto done;
2241 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2242 err = got_path_basename(&tpd->entry_name, pe->path);
2243 if (err) {
2244 free(tpd->relpath);
2245 free(tpd->tree_id);
2246 free(tpd);
2247 goto done;
2249 } else
2250 tpd->entry_name = NULL;
2252 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2256 * Read the file index.
2257 * Checking out files is supposed to be an idempotent operation.
2258 * If the on-disk file index is incomplete we will try to complete it.
2260 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2261 if (err)
2262 goto done;
2264 tpd = SIMPLEQ_FIRST(&tree_paths);
2265 TAILQ_FOREACH(pe, paths, entry) {
2266 struct bump_base_commit_id_arg bbc_arg;
2268 err = checkout_files(worktree, fileindex, tpd->relpath,
2269 tpd->tree_id, tpd->entry_name, repo,
2270 progress_cb, progress_arg, cancel_cb, cancel_arg);
2271 if (err)
2272 break;
2274 bbc_arg.base_commit_id = worktree->base_commit_id;
2275 bbc_arg.entry_name = tpd->entry_name;
2276 bbc_arg.path = pe->path;
2277 bbc_arg.path_len = pe->path_len;
2278 bbc_arg.progress_cb = progress_cb;
2279 bbc_arg.progress_arg = progress_arg;
2280 err = got_fileindex_for_each_entry_safe(fileindex,
2281 bump_base_commit_id, &bbc_arg);
2282 if (err)
2283 break;
2285 tpd = SIMPLEQ_NEXT(tpd, entry);
2287 sync_err = sync_fileindex(fileindex, fileindex_path);
2288 if (sync_err && err == NULL)
2289 err = sync_err;
2290 done:
2291 free(fileindex_path);
2292 if (tree)
2293 got_object_tree_close(tree);
2294 if (commit)
2295 got_object_commit_close(commit);
2296 if (fileindex)
2297 got_fileindex_free(fileindex);
2298 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2299 tpd = SIMPLEQ_FIRST(&tree_paths);
2300 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2301 free(tpd->relpath);
2302 free(tpd->tree_id);
2303 free(tpd);
2305 unlockerr = lock_worktree(worktree, LOCK_SH);
2306 if (unlockerr && err == NULL)
2307 err = unlockerr;
2308 return err;
2311 struct merge_file_cb_arg {
2312 struct got_worktree *worktree;
2313 struct got_fileindex *fileindex;
2314 got_worktree_checkout_cb progress_cb;
2315 void *progress_arg;
2316 got_cancel_cb cancel_cb;
2317 void *cancel_arg;
2318 const char *label_orig;
2319 struct got_object_id *commit_id2;
2322 static const struct got_error *
2323 merge_file_cb(void *arg, struct got_blob_object *blob1,
2324 struct got_blob_object *blob2, struct got_object_id *id1,
2325 struct got_object_id *id2, const char *path1, const char *path2,
2326 mode_t mode1, mode_t mode2, struct got_repository *repo)
2328 static const struct got_error *err = NULL;
2329 struct merge_file_cb_arg *a = arg;
2330 struct got_fileindex_entry *ie;
2331 char *ondisk_path = NULL;
2332 struct stat sb;
2333 unsigned char status;
2334 int local_changes_subsumed;
2336 if (blob1 && blob2) {
2337 ie = got_fileindex_entry_get(a->fileindex, path2,
2338 strlen(path2));
2339 if (ie == NULL)
2340 return (*a->progress_cb)(a->progress_arg,
2341 GOT_STATUS_MISSING, path2);
2343 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2344 path2) == -1)
2345 return got_error_from_errno("asprintf");
2347 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2348 repo);
2349 if (err)
2350 goto done;
2352 if (status == GOT_STATUS_DELETE) {
2353 err = (*a->progress_cb)(a->progress_arg,
2354 GOT_STATUS_MERGE, path2);
2355 goto done;
2357 if (status != GOT_STATUS_NO_CHANGE &&
2358 status != GOT_STATUS_MODIFY &&
2359 status != GOT_STATUS_CONFLICT &&
2360 status != GOT_STATUS_ADD) {
2361 err = (*a->progress_cb)(a->progress_arg, status, path2);
2362 goto done;
2365 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2366 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2367 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2368 } else if (blob1) {
2369 ie = got_fileindex_entry_get(a->fileindex, path1,
2370 strlen(path1));
2371 if (ie == NULL)
2372 return (*a->progress_cb)(a->progress_arg,
2373 GOT_STATUS_MISSING, path1);
2375 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2376 path1) == -1)
2377 return got_error_from_errno("asprintf");
2379 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2380 repo);
2381 if (err)
2382 goto done;
2384 switch (status) {
2385 case GOT_STATUS_NO_CHANGE:
2386 err = (*a->progress_cb)(a->progress_arg,
2387 GOT_STATUS_DELETE, path1);
2388 if (err)
2389 goto done;
2390 err = remove_ondisk_file(a->worktree->root_path, path1);
2391 if (err)
2392 goto done;
2393 if (ie)
2394 got_fileindex_entry_mark_deleted_from_disk(ie);
2395 break;
2396 case GOT_STATUS_DELETE:
2397 case GOT_STATUS_MISSING:
2398 err = (*a->progress_cb)(a->progress_arg,
2399 GOT_STATUS_DELETE, path1);
2400 if (err)
2401 goto done;
2402 if (ie)
2403 got_fileindex_entry_mark_deleted_from_disk(ie);
2404 break;
2405 case GOT_STATUS_ADD:
2406 case GOT_STATUS_MODIFY:
2407 case GOT_STATUS_CONFLICT:
2408 err = (*a->progress_cb)(a->progress_arg,
2409 GOT_STATUS_CANNOT_DELETE, path1);
2410 if (err)
2411 goto done;
2412 break;
2413 case GOT_STATUS_OBSTRUCTED:
2414 err = (*a->progress_cb)(a->progress_arg, status, path1);
2415 if (err)
2416 goto done;
2417 break;
2418 default:
2419 break;
2421 } else if (blob2) {
2422 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2423 path2) == -1)
2424 return got_error_from_errno("asprintf");
2425 ie = got_fileindex_entry_get(a->fileindex, path2,
2426 strlen(path2));
2427 if (ie) {
2428 err = get_file_status(&status, &sb, ie, ondisk_path,
2429 -1, NULL, repo);
2430 if (err)
2431 goto done;
2432 if (status != GOT_STATUS_NO_CHANGE &&
2433 status != GOT_STATUS_MODIFY &&
2434 status != GOT_STATUS_CONFLICT &&
2435 status != GOT_STATUS_ADD) {
2436 err = (*a->progress_cb)(a->progress_arg,
2437 status, path2);
2438 goto done;
2440 err = merge_blob(&local_changes_subsumed, a->worktree,
2441 NULL, ondisk_path, path2, sb.st_mode,
2442 a->label_orig, blob2, a->commit_id2, repo,
2443 a->progress_cb,
2444 a->progress_arg);
2445 if (status == GOT_STATUS_DELETE) {
2446 err = got_fileindex_entry_update(ie,
2447 ondisk_path, blob2->id.sha1,
2448 a->worktree->base_commit_id->sha1, 0);
2449 if (err)
2450 goto done;
2452 } else {
2453 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2454 err = install_blob(a->worktree, ondisk_path, path2,
2455 /* XXX get this from parent tree! */
2456 GOT_DEFAULT_FILE_MODE,
2457 sb.st_mode, blob2, 0, 0, repo,
2458 a->progress_cb, a->progress_arg);
2459 if (err)
2460 goto done;
2461 err = got_fileindex_entry_alloc(&ie, path2);
2462 if (err)
2463 goto done;
2464 err = got_fileindex_entry_update(ie, ondisk_path,
2465 NULL, NULL, 1);
2466 if (err) {
2467 got_fileindex_entry_free(ie);
2468 goto done;
2470 err = got_fileindex_entry_add(a->fileindex, ie);
2471 if (err) {
2472 got_fileindex_entry_free(ie);
2473 goto done;
2477 done:
2478 free(ondisk_path);
2479 return err;
2482 struct check_merge_ok_arg {
2483 struct got_worktree *worktree;
2484 struct got_repository *repo;
2487 static const struct got_error *
2488 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2490 const struct got_error *err = NULL;
2491 struct check_merge_ok_arg *a = arg;
2492 unsigned char status;
2493 struct stat sb;
2494 char *ondisk_path;
2496 /* Reject merges into a work tree with mixed base commits. */
2497 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2498 SHA1_DIGEST_LENGTH))
2499 return got_error(GOT_ERR_MIXED_COMMITS);
2501 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2502 == -1)
2503 return got_error_from_errno("asprintf");
2505 /* Reject merges into a work tree with conflicted files. */
2506 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2507 if (err)
2508 return err;
2509 if (status == GOT_STATUS_CONFLICT)
2510 return got_error(GOT_ERR_CONFLICTS);
2512 return NULL;
2515 static const struct got_error *
2516 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2517 const char *fileindex_path, struct got_object_id *commit_id1,
2518 struct got_object_id *commit_id2, struct got_repository *repo,
2519 got_worktree_checkout_cb progress_cb, void *progress_arg,
2520 got_cancel_cb cancel_cb, void *cancel_arg)
2522 const struct got_error *err = NULL, *sync_err;
2523 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2524 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2525 struct merge_file_cb_arg arg;
2526 char *label_orig = NULL;
2528 if (commit_id1) {
2529 char *id_str;
2531 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2532 worktree->path_prefix);
2533 if (err)
2534 goto done;
2536 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2537 if (err)
2538 goto done;
2540 err = got_object_id_str(&id_str, commit_id1);
2541 if (err)
2542 goto done;
2544 if (asprintf(&label_orig, "%s: commit %s",
2545 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2546 err = got_error_from_errno("asprintf");
2547 free(id_str);
2548 goto done;
2550 free(id_str);
2553 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2554 worktree->path_prefix);
2555 if (err)
2556 goto done;
2558 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2559 if (err)
2560 goto done;
2562 arg.worktree = worktree;
2563 arg.fileindex = fileindex;
2564 arg.progress_cb = progress_cb;
2565 arg.progress_arg = progress_arg;
2566 arg.cancel_cb = cancel_cb;
2567 arg.cancel_arg = cancel_arg;
2568 arg.label_orig = label_orig;
2569 arg.commit_id2 = commit_id2;
2570 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2571 sync_err = sync_fileindex(fileindex, fileindex_path);
2572 if (sync_err && err == NULL)
2573 err = sync_err;
2574 done:
2575 if (tree1)
2576 got_object_tree_close(tree1);
2577 if (tree2)
2578 got_object_tree_close(tree2);
2579 free(label_orig);
2580 return err;
2583 const struct got_error *
2584 got_worktree_merge_files(struct got_worktree *worktree,
2585 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2586 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2587 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2589 const struct got_error *err, *unlockerr;
2590 char *fileindex_path = NULL;
2591 struct got_fileindex *fileindex = NULL;
2592 struct check_merge_ok_arg mok_arg;
2594 err = lock_worktree(worktree, LOCK_EX);
2595 if (err)
2596 return err;
2598 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2599 if (err)
2600 goto done;
2602 mok_arg.worktree = worktree;
2603 mok_arg.repo = repo;
2604 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2605 &mok_arg);
2606 if (err)
2607 goto done;
2609 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2610 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2611 done:
2612 if (fileindex)
2613 got_fileindex_free(fileindex);
2614 free(fileindex_path);
2615 unlockerr = lock_worktree(worktree, LOCK_SH);
2616 if (unlockerr && err == NULL)
2617 err = unlockerr;
2618 return err;
2621 struct diff_dir_cb_arg {
2622 struct got_fileindex *fileindex;
2623 struct got_worktree *worktree;
2624 const char *status_path;
2625 size_t status_path_len;
2626 struct got_repository *repo;
2627 got_worktree_status_cb status_cb;
2628 void *status_arg;
2629 got_cancel_cb cancel_cb;
2630 void *cancel_arg;
2631 /* A pathlist containing per-directory pathlists of ignore patterns. */
2632 struct got_pathlist_head ignores;
2633 int report_unchanged;
2634 int no_ignores;
2637 static const struct got_error *
2638 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2639 int dirfd, const char *de_name,
2640 got_worktree_status_cb status_cb, void *status_arg,
2641 struct got_repository *repo, int report_unchanged)
2643 const struct got_error *err = NULL;
2644 unsigned char status = GOT_STATUS_NO_CHANGE;
2645 unsigned char staged_status = get_staged_status(ie);
2646 struct stat sb;
2647 struct got_object_id blob_id, commit_id, staged_blob_id;
2648 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2649 struct got_object_id *staged_blob_idp = NULL;
2651 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2652 if (err)
2653 return err;
2655 if (status == GOT_STATUS_NO_CHANGE &&
2656 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2657 return NULL;
2659 if (got_fileindex_entry_has_blob(ie)) {
2660 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2661 blob_idp = &blob_id;
2663 if (got_fileindex_entry_has_commit(ie)) {
2664 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2665 commit_idp = &commit_id;
2667 if (staged_status == GOT_STATUS_ADD ||
2668 staged_status == GOT_STATUS_MODIFY) {
2669 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2670 SHA1_DIGEST_LENGTH);
2671 staged_blob_idp = &staged_blob_id;
2674 return (*status_cb)(status_arg, status, staged_status,
2675 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2678 static const struct got_error *
2679 status_old_new(void *arg, struct got_fileindex_entry *ie,
2680 struct dirent *de, const char *parent_path, int dirfd)
2682 const struct got_error *err = NULL;
2683 struct diff_dir_cb_arg *a = arg;
2684 char *abspath;
2686 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2687 return got_error(GOT_ERR_CANCELLED);
2689 if (got_path_cmp(parent_path, a->status_path,
2690 strlen(parent_path), a->status_path_len) != 0 &&
2691 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2692 return NULL;
2694 if (parent_path[0]) {
2695 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2696 parent_path, de->d_name) == -1)
2697 return got_error_from_errno("asprintf");
2698 } else {
2699 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2700 de->d_name) == -1)
2701 return got_error_from_errno("asprintf");
2704 err = report_file_status(ie, abspath, dirfd, de->d_name,
2705 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2706 free(abspath);
2707 return err;
2710 static const struct got_error *
2711 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2713 struct diff_dir_cb_arg *a = arg;
2714 struct got_object_id blob_id, commit_id;
2715 unsigned char status;
2717 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2718 return got_error(GOT_ERR_CANCELLED);
2720 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2721 return NULL;
2723 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2724 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2725 if (got_fileindex_entry_has_file_on_disk(ie))
2726 status = GOT_STATUS_MISSING;
2727 else
2728 status = GOT_STATUS_DELETE;
2729 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2730 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2733 void
2734 free_ignorelist(struct got_pathlist_head *ignorelist)
2736 struct got_pathlist_entry *pe;
2738 TAILQ_FOREACH(pe, ignorelist, entry)
2739 free((char *)pe->path);
2740 got_pathlist_free(ignorelist);
2743 void
2744 free_ignores(struct got_pathlist_head *ignores)
2746 struct got_pathlist_entry *pe;
2748 TAILQ_FOREACH(pe, ignores, entry) {
2749 struct got_pathlist_head *ignorelist = pe->data;
2750 free_ignorelist(ignorelist);
2751 free((char *)pe->path);
2753 got_pathlist_free(ignores);
2756 static const struct got_error *
2757 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2759 const struct got_error *err = NULL;
2760 struct got_pathlist_entry *pe = NULL;
2761 struct got_pathlist_head *ignorelist;
2762 char *line = NULL, *pattern, *dirpath = NULL;
2763 size_t linesize = 0;
2764 ssize_t linelen;
2766 ignorelist = calloc(1, sizeof(*ignorelist));
2767 if (ignorelist == NULL)
2768 return got_error_from_errno("calloc");
2769 TAILQ_INIT(ignorelist);
2771 while ((linelen = getline(&line, &linesize, f)) != -1) {
2772 if (linelen > 0 && line[linelen - 1] == '\n')
2773 line[linelen - 1] = '\0';
2775 /* Git's ignores may contain comments. */
2776 if (line[0] == '#')
2777 continue;
2779 /* Git's negated patterns are not (yet?) supported. */
2780 if (line[0] == '!')
2781 continue;
2783 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2784 line) == -1) {
2785 err = got_error_from_errno("asprintf");
2786 goto done;
2788 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2789 if (err)
2790 goto done;
2792 if (ferror(f)) {
2793 err = got_error_from_errno("getline");
2794 goto done;
2797 dirpath = strdup(path);
2798 if (dirpath == NULL) {
2799 err = got_error_from_errno("strdup");
2800 goto done;
2802 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2803 done:
2804 free(line);
2805 if (err || pe == NULL) {
2806 free(dirpath);
2807 free_ignorelist(ignorelist);
2809 return err;
2812 int
2813 match_ignores(struct got_pathlist_head *ignores, const char *path)
2815 struct got_pathlist_entry *pe;
2817 /* Handle patterns which match in all directories. */
2818 TAILQ_FOREACH(pe, ignores, entry) {
2819 struct got_pathlist_head *ignorelist = pe->data;
2820 struct got_pathlist_entry *pi;
2822 TAILQ_FOREACH(pi, ignorelist, entry) {
2823 const char *p, *pattern = pi->path;
2825 if (strncmp(pattern, "**/", 3) != 0)
2826 continue;
2827 pattern += 3;
2828 p = path;
2829 while (*p) {
2830 if (fnmatch(pattern, p,
2831 FNM_PATHNAME | FNM_LEADING_DIR)) {
2832 /* Retry in next directory. */
2833 while (*p && *p != '/')
2834 p++;
2835 while (*p == '/')
2836 p++;
2837 continue;
2839 return 1;
2845 * The ignores pathlist contains ignore lists from children before
2846 * parents, so we can find the most specific ignorelist by walking
2847 * ignores backwards.
2849 pe = TAILQ_LAST(ignores, got_pathlist_head);
2850 while (pe) {
2851 if (got_path_is_child(path, pe->path, pe->path_len)) {
2852 struct got_pathlist_head *ignorelist = pe->data;
2853 struct got_pathlist_entry *pi;
2854 TAILQ_FOREACH(pi, ignorelist, entry) {
2855 const char *pattern = pi->path;
2856 int flags = FNM_LEADING_DIR;
2857 if (strstr(pattern, "/**/") == NULL)
2858 flags |= FNM_PATHNAME;
2859 if (fnmatch(pattern, path, flags))
2860 continue;
2861 return 1;
2864 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2867 return 0;
2870 static const struct got_error *
2871 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2872 const char *path, int dirfd, const char *ignores_filename)
2874 const struct got_error *err = NULL;
2875 char *ignorespath;
2876 int fd = -1;
2877 FILE *ignoresfile = NULL;
2879 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2880 path[0] ? "/" : "", ignores_filename) == -1)
2881 return got_error_from_errno("asprintf");
2883 if (dirfd != -1) {
2884 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2885 if (fd == -1) {
2886 if (errno != ENOENT && errno != EACCES)
2887 err = got_error_from_errno2("openat",
2888 ignorespath);
2889 } else {
2890 ignoresfile = fdopen(fd, "r");
2891 if (ignoresfile == NULL)
2892 err = got_error_from_errno2("fdopen",
2893 ignorespath);
2894 else {
2895 fd = -1;
2896 err = read_ignores(ignores, path, ignoresfile);
2899 } else {
2900 ignoresfile = fopen(ignorespath, "r");
2901 if (ignoresfile == NULL) {
2902 if (errno != ENOENT && errno != EACCES)
2903 err = got_error_from_errno2("fopen",
2904 ignorespath);
2905 } else
2906 err = read_ignores(ignores, path, ignoresfile);
2909 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2910 err = got_error_from_errno2("fclose", path);
2911 if (fd != -1 && close(fd) == -1 && err == NULL)
2912 err = got_error_from_errno2("close", path);
2913 free(ignorespath);
2914 return err;
2917 static const struct got_error *
2918 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2920 const struct got_error *err = NULL;
2921 struct diff_dir_cb_arg *a = arg;
2922 char *path = NULL;
2924 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2925 return got_error(GOT_ERR_CANCELLED);
2927 if (parent_path[0]) {
2928 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2929 return got_error_from_errno("asprintf");
2930 } else {
2931 path = de->d_name;
2934 if (de->d_type != DT_DIR &&
2935 got_path_is_child(path, a->status_path, a->status_path_len)
2936 && !match_ignores(&a->ignores, path))
2937 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2938 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2939 if (parent_path[0])
2940 free(path);
2941 return err;
2944 static const struct got_error *
2945 status_traverse(void *arg, const char *path, int dirfd)
2947 const struct got_error *err = NULL;
2948 struct diff_dir_cb_arg *a = arg;
2950 if (a->no_ignores)
2951 return NULL;
2953 err = add_ignores(&a->ignores, a->worktree->root_path,
2954 path, dirfd, ".cvsignore");
2955 if (err)
2956 return err;
2958 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2959 dirfd, ".gitignore");
2961 return err;
2964 static const struct got_error *
2965 report_single_file_status(const char *path, const char *ondisk_path,
2966 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2967 void *status_arg, struct got_repository *repo, int report_unchanged)
2969 struct got_fileindex_entry *ie;
2970 struct stat sb;
2972 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2973 if (ie)
2974 return report_file_status(ie, ondisk_path, -1, NULL,
2975 status_cb, status_arg, repo, report_unchanged);
2977 if (lstat(ondisk_path, &sb) == -1) {
2978 if (errno != ENOENT)
2979 return got_error_from_errno2("lstat", ondisk_path);
2980 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2981 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2982 return NULL;
2985 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
2986 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2987 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2989 return NULL;
2992 static const struct got_error *
2993 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2994 const char *root_path, const char *path)
2996 const struct got_error *err;
2997 char *parent_path, *next_parent_path = NULL;
2999 err = add_ignores(ignores, root_path, "", -1,
3000 ".cvsignore");
3001 if (err)
3002 return err;
3004 err = add_ignores(ignores, root_path, "", -1,
3005 ".gitignore");
3006 if (err)
3007 return err;
3009 err = got_path_dirname(&parent_path, path);
3010 if (err) {
3011 if (err->code == GOT_ERR_BAD_PATH)
3012 return NULL; /* cannot traverse parent */
3013 return err;
3015 for (;;) {
3016 err = add_ignores(ignores, root_path, parent_path, -1,
3017 ".cvsignore");
3018 if (err)
3019 break;
3020 err = add_ignores(ignores, root_path, parent_path, -1,
3021 ".gitignore");
3022 if (err)
3023 break;
3024 err = got_path_dirname(&next_parent_path, parent_path);
3025 if (err) {
3026 if (err->code == GOT_ERR_BAD_PATH)
3027 err = NULL; /* traversed everything */
3028 break;
3030 free(parent_path);
3031 parent_path = next_parent_path;
3032 next_parent_path = NULL;
3035 free(parent_path);
3036 free(next_parent_path);
3037 return err;
3040 static const struct got_error *
3041 worktree_status(struct got_worktree *worktree, const char *path,
3042 struct got_fileindex *fileindex, struct got_repository *repo,
3043 got_worktree_status_cb status_cb, void *status_arg,
3044 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3045 int report_unchanged)
3047 const struct got_error *err = NULL;
3048 int fd = -1;
3049 struct got_fileindex_diff_dir_cb fdiff_cb;
3050 struct diff_dir_cb_arg arg;
3051 char *ondisk_path = NULL;
3053 TAILQ_INIT(&arg.ignores);
3055 if (asprintf(&ondisk_path, "%s%s%s",
3056 worktree->root_path, path[0] ? "/" : "", path) == -1)
3057 return got_error_from_errno("asprintf");
3059 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3060 if (fd == -1) {
3061 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3062 errno != ELOOP)
3063 err = got_error_from_errno2("open", ondisk_path);
3064 else
3065 err = report_single_file_status(path, ondisk_path,
3066 fileindex, status_cb, status_arg, repo,
3067 report_unchanged);
3068 } else {
3069 fdiff_cb.diff_old_new = status_old_new;
3070 fdiff_cb.diff_old = status_old;
3071 fdiff_cb.diff_new = status_new;
3072 fdiff_cb.diff_traverse = status_traverse;
3073 arg.fileindex = fileindex;
3074 arg.worktree = worktree;
3075 arg.status_path = path;
3076 arg.status_path_len = strlen(path);
3077 arg.repo = repo;
3078 arg.status_cb = status_cb;
3079 arg.status_arg = status_arg;
3080 arg.cancel_cb = cancel_cb;
3081 arg.cancel_arg = cancel_arg;
3082 arg.report_unchanged = report_unchanged;
3083 arg.no_ignores = no_ignores;
3084 if (!no_ignores) {
3085 err = add_ignores_from_parent_paths(&arg.ignores,
3086 worktree->root_path, path);
3087 if (err)
3088 goto done;
3090 err = got_fileindex_diff_dir(fileindex, fd,
3091 worktree->root_path, path, repo, &fdiff_cb, &arg);
3093 done:
3094 free_ignores(&arg.ignores);
3095 if (fd != -1 && close(fd) != 0 && err == NULL)
3096 err = got_error_from_errno("close");
3097 free(ondisk_path);
3098 return err;
3101 const struct got_error *
3102 got_worktree_status(struct got_worktree *worktree,
3103 struct got_pathlist_head *paths, struct got_repository *repo,
3104 got_worktree_status_cb status_cb, void *status_arg,
3105 got_cancel_cb cancel_cb, void *cancel_arg)
3107 const struct got_error *err = NULL;
3108 char *fileindex_path = NULL;
3109 struct got_fileindex *fileindex = NULL;
3110 struct got_pathlist_entry *pe;
3112 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3113 if (err)
3114 return err;
3116 TAILQ_FOREACH(pe, paths, entry) {
3117 err = worktree_status(worktree, pe->path, fileindex, repo,
3118 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3119 if (err)
3120 break;
3122 free(fileindex_path);
3123 got_fileindex_free(fileindex);
3124 return err;
3127 const struct got_error *
3128 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3129 const char *arg)
3131 const struct got_error *err = NULL;
3132 char *resolved = NULL, *cwd = NULL, *path = NULL;
3133 size_t len;
3134 struct stat sb;
3136 *wt_path = NULL;
3138 cwd = getcwd(NULL, 0);
3139 if (cwd == NULL)
3140 return got_error_from_errno("getcwd");
3142 if (lstat(arg, &sb) == -1) {
3143 if (errno != ENOENT) {
3144 err = got_error_from_errno2("lstat", arg);
3145 goto done;
3148 if (S_ISLNK(sb.st_mode)) {
3150 * We cannot use realpath(3) with symlinks since we want to
3151 * operate on the symlink itself.
3152 * But we can make the path absolute, assuming it is relative
3153 * to the current working directory, and then canonicalize it.
3155 char *abspath = NULL;
3156 char canonpath[PATH_MAX];
3157 if (!got_path_is_absolute(arg)) {
3158 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3159 err = got_error_from_errno("asprintf");
3160 goto done;
3164 err = got_canonpath(abspath ? abspath : arg, canonpath,
3165 sizeof(canonpath));
3166 if (err)
3167 goto done;
3168 resolved = strdup(canonpath);
3169 if (resolved == NULL) {
3170 err = got_error_from_errno("strdup");
3171 goto done;
3173 } else {
3174 resolved = realpath(arg, NULL);
3175 if (resolved == NULL) {
3176 if (errno != ENOENT) {
3177 err = got_error_from_errno2("realpath", arg);
3178 goto done;
3180 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3181 err = got_error_from_errno("asprintf");
3182 goto done;
3187 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3188 strlen(got_worktree_get_root_path(worktree)))) {
3189 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3190 goto done;
3193 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3194 err = got_path_skip_common_ancestor(&path,
3195 got_worktree_get_root_path(worktree), resolved);
3196 if (err)
3197 goto done;
3198 } else {
3199 path = strdup("");
3200 if (path == NULL) {
3201 err = got_error_from_errno("strdup");
3202 goto done;
3206 /* XXX status walk can't deal with trailing slash! */
3207 len = strlen(path);
3208 while (len > 0 && path[len - 1] == '/') {
3209 path[len - 1] = '\0';
3210 len--;
3212 done:
3213 free(resolved);
3214 free(cwd);
3215 if (err == NULL)
3216 *wt_path = path;
3217 else
3218 free(path);
3219 return err;
3222 struct schedule_addition_args {
3223 struct got_worktree *worktree;
3224 struct got_fileindex *fileindex;
3225 got_worktree_checkout_cb progress_cb;
3226 void *progress_arg;
3227 struct got_repository *repo;
3230 static const struct got_error *
3231 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3232 const char *relpath, struct got_object_id *blob_id,
3233 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3234 int dirfd, const char *de_name)
3236 struct schedule_addition_args *a = arg;
3237 const struct got_error *err = NULL;
3238 struct got_fileindex_entry *ie;
3239 struct stat sb;
3240 char *ondisk_path;
3242 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3243 relpath) == -1)
3244 return got_error_from_errno("asprintf");
3246 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3247 if (ie) {
3248 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3249 de_name, a->repo);
3250 if (err)
3251 goto done;
3252 /* Re-adding an existing entry is a no-op. */
3253 if (status == GOT_STATUS_ADD)
3254 goto done;
3255 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3256 if (err)
3257 goto done;
3260 if (status != GOT_STATUS_UNVERSIONED) {
3261 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3262 goto done;
3265 err = got_fileindex_entry_alloc(&ie, relpath);
3266 if (err)
3267 goto done;
3268 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3269 if (err) {
3270 got_fileindex_entry_free(ie);
3271 goto done;
3273 err = got_fileindex_entry_add(a->fileindex, ie);
3274 if (err) {
3275 got_fileindex_entry_free(ie);
3276 goto done;
3278 done:
3279 free(ondisk_path);
3280 if (err)
3281 return err;
3282 if (status == GOT_STATUS_ADD)
3283 return NULL;
3284 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3287 const struct got_error *
3288 got_worktree_schedule_add(struct got_worktree *worktree,
3289 struct got_pathlist_head *paths,
3290 got_worktree_checkout_cb progress_cb, void *progress_arg,
3291 struct got_repository *repo, int no_ignores)
3293 struct got_fileindex *fileindex = NULL;
3294 char *fileindex_path = NULL;
3295 const struct got_error *err = NULL, *sync_err, *unlockerr;
3296 struct got_pathlist_entry *pe;
3297 struct schedule_addition_args saa;
3299 err = lock_worktree(worktree, LOCK_EX);
3300 if (err)
3301 return err;
3303 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3304 if (err)
3305 goto done;
3307 saa.worktree = worktree;
3308 saa.fileindex = fileindex;
3309 saa.progress_cb = progress_cb;
3310 saa.progress_arg = progress_arg;
3311 saa.repo = repo;
3313 TAILQ_FOREACH(pe, paths, entry) {
3314 err = worktree_status(worktree, pe->path, fileindex, repo,
3315 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3316 if (err)
3317 break;
3319 sync_err = sync_fileindex(fileindex, fileindex_path);
3320 if (sync_err && err == NULL)
3321 err = sync_err;
3322 done:
3323 free(fileindex_path);
3324 if (fileindex)
3325 got_fileindex_free(fileindex);
3326 unlockerr = lock_worktree(worktree, LOCK_SH);
3327 if (unlockerr && err == NULL)
3328 err = unlockerr;
3329 return err;
3332 struct schedule_deletion_args {
3333 struct got_worktree *worktree;
3334 struct got_fileindex *fileindex;
3335 got_worktree_delete_cb progress_cb;
3336 void *progress_arg;
3337 struct got_repository *repo;
3338 int delete_local_mods;
3339 int keep_on_disk;
3342 static const struct got_error *
3343 schedule_for_deletion(void *arg, unsigned char status,
3344 unsigned char staged_status, const char *relpath,
3345 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3346 struct got_object_id *commit_id, int dirfd, const char *de_name)
3348 struct schedule_deletion_args *a = arg;
3349 const struct got_error *err = NULL;
3350 struct got_fileindex_entry *ie = NULL;
3351 struct stat sb;
3352 char *ondisk_path, *parent = NULL;
3354 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3355 if (ie == NULL)
3356 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3358 staged_status = get_staged_status(ie);
3359 if (staged_status != GOT_STATUS_NO_CHANGE) {
3360 if (staged_status == GOT_STATUS_DELETE)
3361 return NULL;
3362 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3365 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3366 relpath) == -1)
3367 return got_error_from_errno("asprintf");
3369 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3370 a->repo);
3371 if (err)
3372 goto done;
3374 if (status != GOT_STATUS_NO_CHANGE) {
3375 if (status == GOT_STATUS_DELETE)
3376 goto done;
3377 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3378 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3379 goto done;
3381 if (status != GOT_STATUS_MODIFY &&
3382 status != GOT_STATUS_MISSING) {
3383 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3384 goto done;
3388 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3389 if (dirfd != -1) {
3390 if (unlinkat(dirfd, de_name, 0) != 0) {
3391 err = got_error_from_errno2("unlinkat",
3392 ondisk_path);
3393 goto done;
3395 } else if (unlink(ondisk_path) != 0) {
3396 err = got_error_from_errno2("unlink", ondisk_path);
3397 goto done;
3400 parent = dirname(ondisk_path);
3402 if (parent == NULL) {
3403 err = got_error_from_errno2("dirname", ondisk_path);
3404 goto done;
3406 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3407 if (rmdir(parent) == -1) {
3408 if (errno != ENOTEMPTY)
3409 err = got_error_from_errno2("rmdir",
3410 parent);
3411 break;
3413 parent = dirname(parent);
3414 if (parent == NULL) {
3415 err = got_error_from_errno2("dirname", parent);
3416 goto done;
3421 got_fileindex_entry_mark_deleted_from_disk(ie);
3422 done:
3423 free(ondisk_path);
3424 if (err)
3425 return err;
3426 if (status == GOT_STATUS_DELETE)
3427 return NULL;
3428 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3429 staged_status, relpath);
3432 const struct got_error *
3433 got_worktree_schedule_delete(struct got_worktree *worktree,
3434 struct got_pathlist_head *paths, int delete_local_mods,
3435 got_worktree_delete_cb progress_cb, void *progress_arg,
3436 struct got_repository *repo, int keep_on_disk)
3438 struct got_fileindex *fileindex = NULL;
3439 char *fileindex_path = NULL;
3440 const struct got_error *err = NULL, *sync_err, *unlockerr;
3441 struct got_pathlist_entry *pe;
3442 struct schedule_deletion_args sda;
3444 err = lock_worktree(worktree, LOCK_EX);
3445 if (err)
3446 return err;
3448 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3449 if (err)
3450 goto done;
3452 sda.worktree = worktree;
3453 sda.fileindex = fileindex;
3454 sda.progress_cb = progress_cb;
3455 sda.progress_arg = progress_arg;
3456 sda.repo = repo;
3457 sda.delete_local_mods = delete_local_mods;
3458 sda.keep_on_disk = keep_on_disk;
3460 TAILQ_FOREACH(pe, paths, entry) {
3461 err = worktree_status(worktree, pe->path, fileindex, repo,
3462 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3463 if (err)
3464 break;
3466 sync_err = sync_fileindex(fileindex, fileindex_path);
3467 if (sync_err && err == NULL)
3468 err = sync_err;
3469 done:
3470 free(fileindex_path);
3471 if (fileindex)
3472 got_fileindex_free(fileindex);
3473 unlockerr = lock_worktree(worktree, LOCK_SH);
3474 if (unlockerr && err == NULL)
3475 err = unlockerr;
3476 return err;
3479 static const struct got_error *
3480 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3482 const struct got_error *err = NULL;
3483 char *line = NULL;
3484 size_t linesize = 0, n;
3485 ssize_t linelen;
3487 linelen = getline(&line, &linesize, infile);
3488 if (linelen == -1) {
3489 if (ferror(infile)) {
3490 err = got_error_from_errno("getline");
3491 goto done;
3493 return NULL;
3495 if (outfile) {
3496 n = fwrite(line, 1, linelen, outfile);
3497 if (n != linelen) {
3498 err = got_ferror(outfile, GOT_ERR_IO);
3499 goto done;
3502 if (rejectfile) {
3503 n = fwrite(line, 1, linelen, rejectfile);
3504 if (n != linelen)
3505 err = got_ferror(outfile, GOT_ERR_IO);
3507 done:
3508 free(line);
3509 return err;
3512 static const struct got_error *
3513 skip_one_line(FILE *f)
3515 char *line = NULL;
3516 size_t linesize = 0;
3517 ssize_t linelen;
3519 linelen = getline(&line, &linesize, f);
3520 if (linelen == -1) {
3521 if (ferror(f))
3522 return got_error_from_errno("getline");
3523 return NULL;
3525 free(line);
3526 return NULL;
3529 static const struct got_error *
3530 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3531 int start_old, int end_old, int start_new, int end_new,
3532 FILE *outfile, FILE *rejectfile)
3534 const struct got_error *err;
3536 /* Copy old file's lines leading up to patch. */
3537 while (!feof(f1) && *line_cur1 < start_old) {
3538 err = copy_one_line(f1, outfile, NULL);
3539 if (err)
3540 return err;
3541 (*line_cur1)++;
3543 /* Skip new file's lines leading up to patch. */
3544 while (!feof(f2) && *line_cur2 < start_new) {
3545 if (rejectfile)
3546 err = copy_one_line(f2, NULL, rejectfile);
3547 else
3548 err = skip_one_line(f2);
3549 if (err)
3550 return err;
3551 (*line_cur2)++;
3553 /* Copy patched lines. */
3554 while (!feof(f2) && *line_cur2 <= end_new) {
3555 err = copy_one_line(f2, outfile, NULL);
3556 if (err)
3557 return err;
3558 (*line_cur2)++;
3560 /* Skip over old file's replaced lines. */
3561 while (!feof(f1) && *line_cur1 <= end_old) {
3562 if (rejectfile)
3563 err = copy_one_line(f1, NULL, rejectfile);
3564 else
3565 err = skip_one_line(f1);
3566 if (err)
3567 return err;
3568 (*line_cur1)++;
3571 return NULL;
3574 static const struct got_error *
3575 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3576 FILE *outfile, FILE *rejectfile)
3578 const struct got_error *err;
3580 if (outfile) {
3581 /* Copy old file's lines until EOF. */
3582 while (!feof(f1)) {
3583 err = copy_one_line(f1, outfile, NULL);
3584 if (err)
3585 return err;
3586 (*line_cur1)++;
3589 if (rejectfile) {
3590 /* Copy new file's lines until EOF. */
3591 while (!feof(f2)) {
3592 err = copy_one_line(f2, NULL, rejectfile);
3593 if (err)
3594 return err;
3595 (*line_cur2)++;
3599 return NULL;
3602 static const struct got_error *
3603 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3604 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3605 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3606 int *line_cur2, FILE *outfile, FILE *rejectfile,
3607 got_worktree_patch_cb patch_cb, void *patch_arg)
3609 const struct got_error *err = NULL;
3610 int start_old = change->cv.a;
3611 int end_old = change->cv.b;
3612 int start_new = change->cv.c;
3613 int end_new = change->cv.d;
3614 long pos1, pos2;
3615 FILE *hunkfile;
3617 *choice = GOT_PATCH_CHOICE_NONE;
3619 hunkfile = got_opentemp();
3620 if (hunkfile == NULL)
3621 return got_error_from_errno("got_opentemp");
3623 pos1 = ftell(f1);
3624 pos2 = ftell(f2);
3626 /* XXX TODO needs error checking */
3627 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3629 if (fseek(f1, pos1, SEEK_SET) == -1) {
3630 err = got_ferror(f1, GOT_ERR_IO);
3631 goto done;
3633 if (fseek(f2, pos2, SEEK_SET) == -1) {
3634 err = got_ferror(f1, GOT_ERR_IO);
3635 goto done;
3637 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3638 err = got_ferror(hunkfile, GOT_ERR_IO);
3639 goto done;
3642 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3643 hunkfile, n, nchanges);
3644 if (err)
3645 goto done;
3647 switch (*choice) {
3648 case GOT_PATCH_CHOICE_YES:
3649 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3650 end_old, start_new, end_new, outfile, rejectfile);
3651 break;
3652 case GOT_PATCH_CHOICE_NO:
3653 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3654 end_old, start_new, end_new, rejectfile, outfile);
3655 break;
3656 case GOT_PATCH_CHOICE_QUIT:
3657 break;
3658 default:
3659 err = got_error(GOT_ERR_PATCH_CHOICE);
3660 break;
3662 done:
3663 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3664 err = got_error_from_errno("fclose");
3665 return err;
3668 struct revert_file_args {
3669 struct got_worktree *worktree;
3670 struct got_fileindex *fileindex;
3671 got_worktree_checkout_cb progress_cb;
3672 void *progress_arg;
3673 got_worktree_patch_cb patch_cb;
3674 void *patch_arg;
3675 struct got_repository *repo;
3678 static const struct got_error *
3679 create_patched_content(char **path_outfile, int reverse_patch,
3680 struct got_object_id *blob_id, const char *path2,
3681 int dirfd2, const char *de_name2,
3682 const char *relpath, struct got_repository *repo,
3683 got_worktree_patch_cb patch_cb, void *patch_arg)
3685 const struct got_error *err;
3686 struct got_blob_object *blob = NULL;
3687 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3688 int fd2 = -1;
3689 char *path1 = NULL, *id_str = NULL;
3690 struct stat sb1, sb2;
3691 struct got_diff_changes *changes = NULL;
3692 struct got_diff_state *ds = NULL;
3693 struct got_diff_args *args = NULL;
3694 struct got_diff_change *change;
3695 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3696 int n = 0;
3698 *path_outfile = NULL;
3700 err = got_object_id_str(&id_str, blob_id);
3701 if (err)
3702 return err;
3704 if (dirfd2 != -1) {
3705 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3706 if (fd2 == -1) {
3707 err = got_error_from_errno2("openat", path2);
3708 goto done;
3710 } else {
3711 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3712 if (fd2 == -1) {
3713 err = got_error_from_errno2("open", path2);
3714 goto done;
3717 if (fstat(fd2, &sb2) == -1) {
3718 err = got_error_from_errno2("fstat", path2);
3719 goto done;
3722 f2 = fdopen(fd2, "r");
3723 if (f2 == NULL) {
3724 err = got_error_from_errno2("fdopen", path2);
3725 goto done;
3727 fd2 = -1;
3729 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3730 if (err)
3731 goto done;
3733 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3734 if (err)
3735 goto done;
3737 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3738 if (err)
3739 goto done;
3741 if (stat(path1, &sb1) == -1) {
3742 err = got_error_from_errno2("stat", path1);
3743 goto done;
3746 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3747 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3748 if (err)
3749 goto done;
3751 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3752 if (err)
3753 goto done;
3755 if (fseek(f1, 0L, SEEK_SET) == -1)
3756 return got_ferror(f1, GOT_ERR_IO);
3757 if (fseek(f2, 0L, SEEK_SET) == -1)
3758 return got_ferror(f2, GOT_ERR_IO);
3759 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3760 int choice;
3761 err = apply_or_reject_change(&choice, change, ++n,
3762 changes->nchanges, ds, args, diff_flags, relpath,
3763 f1, f2, &line_cur1, &line_cur2,
3764 reverse_patch ? NULL : outfile,
3765 reverse_patch ? outfile : NULL,
3766 patch_cb, patch_arg);
3767 if (err)
3768 goto done;
3769 if (choice == GOT_PATCH_CHOICE_YES)
3770 have_content = 1;
3771 else if (choice == GOT_PATCH_CHOICE_QUIT)
3772 break;
3774 if (have_content) {
3775 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3776 reverse_patch ? NULL : outfile,
3777 reverse_patch ? outfile : NULL);
3778 if (err)
3779 goto done;
3781 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3782 err = got_error_from_errno2("chmod", path2);
3783 goto done;
3786 done:
3787 free(id_str);
3788 if (blob)
3789 got_object_blob_close(blob);
3790 if (f1 && fclose(f1) == EOF && err == NULL)
3791 err = got_error_from_errno2("fclose", path1);
3792 if (f2 && fclose(f2) == EOF && err == NULL)
3793 err = got_error_from_errno2("fclose", path2);
3794 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3795 err = got_error_from_errno2("close", path2);
3796 if (outfile && fclose(outfile) == EOF && err == NULL)
3797 err = got_error_from_errno2("fclose", *path_outfile);
3798 if (path1 && unlink(path1) == -1 && err == NULL)
3799 err = got_error_from_errno2("unlink", path1);
3800 if (err || !have_content) {
3801 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3802 err = got_error_from_errno2("unlink", *path_outfile);
3803 free(*path_outfile);
3804 *path_outfile = NULL;
3806 free(args);
3807 if (ds) {
3808 got_diff_state_free(ds);
3809 free(ds);
3811 if (changes)
3812 got_diff_free_changes(changes);
3813 free(path1);
3814 return err;
3817 static const struct got_error *
3818 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3819 const char *relpath, struct got_object_id *blob_id,
3820 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3821 int dirfd, const char *de_name)
3823 struct revert_file_args *a = arg;
3824 const struct got_error *err = NULL;
3825 char *parent_path = NULL;
3826 struct got_fileindex_entry *ie;
3827 struct got_tree_object *tree = NULL;
3828 struct got_object_id *tree_id = NULL;
3829 const struct got_tree_entry *te = NULL;
3830 char *tree_path = NULL, *te_name;
3831 char *ondisk_path = NULL, *path_content = NULL;
3832 struct got_blob_object *blob = NULL;
3834 /* Reverting a staged deletion is a no-op. */
3835 if (status == GOT_STATUS_DELETE &&
3836 staged_status != GOT_STATUS_NO_CHANGE)
3837 return NULL;
3839 if (status == GOT_STATUS_UNVERSIONED)
3840 return (*a->progress_cb)(a->progress_arg,
3841 GOT_STATUS_UNVERSIONED, relpath);
3843 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3844 if (ie == NULL)
3845 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3847 /* Construct in-repository path of tree which contains this blob. */
3848 err = got_path_dirname(&parent_path, ie->path);
3849 if (err) {
3850 if (err->code != GOT_ERR_BAD_PATH)
3851 goto done;
3852 parent_path = strdup("/");
3853 if (parent_path == NULL) {
3854 err = got_error_from_errno("strdup");
3855 goto done;
3858 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3859 tree_path = strdup(parent_path);
3860 if (tree_path == NULL) {
3861 err = got_error_from_errno("strdup");
3862 goto done;
3864 } else {
3865 if (got_path_is_root_dir(parent_path)) {
3866 tree_path = strdup(a->worktree->path_prefix);
3867 if (tree_path == NULL) {
3868 err = got_error_from_errno("strdup");
3869 goto done;
3871 } else {
3872 if (asprintf(&tree_path, "%s/%s",
3873 a->worktree->path_prefix, parent_path) == -1) {
3874 err = got_error_from_errno("asprintf");
3875 goto done;
3880 err = got_object_id_by_path(&tree_id, a->repo,
3881 a->worktree->base_commit_id, tree_path);
3882 if (err) {
3883 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3884 (status == GOT_STATUS_ADD ||
3885 staged_status == GOT_STATUS_ADD)))
3886 goto done;
3887 } else {
3888 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3889 if (err)
3890 goto done;
3892 te_name = basename(ie->path);
3893 if (te_name == NULL) {
3894 err = got_error_from_errno2("basename", ie->path);
3895 goto done;
3898 te = got_object_tree_find_entry(tree, te_name);
3899 if (te == NULL && status != GOT_STATUS_ADD &&
3900 staged_status != GOT_STATUS_ADD) {
3901 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3902 goto done;
3906 switch (status) {
3907 case GOT_STATUS_ADD:
3908 if (a->patch_cb) {
3909 int choice = GOT_PATCH_CHOICE_NONE;
3910 err = (*a->patch_cb)(&choice, a->patch_arg,
3911 status, ie->path, NULL, 1, 1);
3912 if (err)
3913 goto done;
3914 if (choice != GOT_PATCH_CHOICE_YES)
3915 break;
3917 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3918 ie->path);
3919 if (err)
3920 goto done;
3921 got_fileindex_entry_remove(a->fileindex, ie);
3922 break;
3923 case GOT_STATUS_DELETE:
3924 if (a->patch_cb) {
3925 int choice = GOT_PATCH_CHOICE_NONE;
3926 err = (*a->patch_cb)(&choice, a->patch_arg,
3927 status, ie->path, NULL, 1, 1);
3928 if (err)
3929 goto done;
3930 if (choice != GOT_PATCH_CHOICE_YES)
3931 break;
3933 /* fall through */
3934 case GOT_STATUS_MODIFY:
3935 case GOT_STATUS_MODE_CHANGE:
3936 case GOT_STATUS_CONFLICT:
3937 case GOT_STATUS_MISSING: {
3938 struct got_object_id id;
3939 if (staged_status == GOT_STATUS_ADD ||
3940 staged_status == GOT_STATUS_MODIFY) {
3941 memcpy(id.sha1, ie->staged_blob_sha1,
3942 SHA1_DIGEST_LENGTH);
3943 } else
3944 memcpy(id.sha1, ie->blob_sha1,
3945 SHA1_DIGEST_LENGTH);
3946 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3947 if (err)
3948 goto done;
3950 if (asprintf(&ondisk_path, "%s/%s",
3951 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3952 err = got_error_from_errno("asprintf");
3953 goto done;
3956 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3957 status == GOT_STATUS_CONFLICT)) {
3958 err = create_patched_content(&path_content, 1, &id,
3959 ondisk_path, dirfd, de_name, ie->path, a->repo,
3960 a->patch_cb, a->patch_arg);
3961 if (err || path_content == NULL)
3962 break;
3963 if (rename(path_content, ondisk_path) == -1) {
3964 err = got_error_from_errno3("rename",
3965 path_content, ondisk_path);
3966 goto done;
3968 } else {
3969 err = install_blob(a->worktree, ondisk_path, ie->path,
3970 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3971 got_fileindex_perms_to_st(ie), blob, 0, 1,
3972 a->repo, a->progress_cb, a->progress_arg);
3973 if (err)
3974 goto done;
3975 if (status == GOT_STATUS_DELETE ||
3976 status == GOT_STATUS_MODE_CHANGE) {
3977 err = got_fileindex_entry_update(ie,
3978 ondisk_path, blob->id.sha1,
3979 a->worktree->base_commit_id->sha1, 1);
3980 if (err)
3981 goto done;
3984 break;
3986 default:
3987 break;
3989 done:
3990 free(ondisk_path);
3991 free(path_content);
3992 free(parent_path);
3993 free(tree_path);
3994 if (blob)
3995 got_object_blob_close(blob);
3996 if (tree)
3997 got_object_tree_close(tree);
3998 free(tree_id);
3999 return err;
4002 const struct got_error *
4003 got_worktree_revert(struct got_worktree *worktree,
4004 struct got_pathlist_head *paths,
4005 got_worktree_checkout_cb progress_cb, void *progress_arg,
4006 got_worktree_patch_cb patch_cb, void *patch_arg,
4007 struct got_repository *repo)
4009 struct got_fileindex *fileindex = NULL;
4010 char *fileindex_path = NULL;
4011 const struct got_error *err = NULL, *unlockerr = NULL;
4012 const struct got_error *sync_err = NULL;
4013 struct got_pathlist_entry *pe;
4014 struct revert_file_args rfa;
4016 err = lock_worktree(worktree, LOCK_EX);
4017 if (err)
4018 return err;
4020 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4021 if (err)
4022 goto done;
4024 rfa.worktree = worktree;
4025 rfa.fileindex = fileindex;
4026 rfa.progress_cb = progress_cb;
4027 rfa.progress_arg = progress_arg;
4028 rfa.patch_cb = patch_cb;
4029 rfa.patch_arg = patch_arg;
4030 rfa.repo = repo;
4031 TAILQ_FOREACH(pe, paths, entry) {
4032 err = worktree_status(worktree, pe->path, fileindex, repo,
4033 revert_file, &rfa, NULL, NULL, 0, 0);
4034 if (err)
4035 break;
4037 sync_err = sync_fileindex(fileindex, fileindex_path);
4038 if (sync_err && err == NULL)
4039 err = sync_err;
4040 done:
4041 free(fileindex_path);
4042 if (fileindex)
4043 got_fileindex_free(fileindex);
4044 unlockerr = lock_worktree(worktree, LOCK_SH);
4045 if (unlockerr && err == NULL)
4046 err = unlockerr;
4047 return err;
4050 static void
4051 free_commitable(struct got_commitable *ct)
4053 free(ct->path);
4054 free(ct->in_repo_path);
4055 free(ct->ondisk_path);
4056 free(ct->blob_id);
4057 free(ct->base_blob_id);
4058 free(ct->staged_blob_id);
4059 free(ct->base_commit_id);
4060 free(ct);
4063 struct collect_commitables_arg {
4064 struct got_pathlist_head *commitable_paths;
4065 struct got_repository *repo;
4066 struct got_worktree *worktree;
4067 int have_staged_files;
4070 static const struct got_error *
4071 collect_commitables(void *arg, unsigned char status,
4072 unsigned char staged_status, const char *relpath,
4073 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 struct collect_commitables_arg *a = arg;
4077 const struct got_error *err = NULL;
4078 struct got_commitable *ct = NULL;
4079 struct got_pathlist_entry *new = NULL;
4080 char *parent_path = NULL, *path = NULL;
4081 struct stat sb;
4083 if (a->have_staged_files) {
4084 if (staged_status != GOT_STATUS_MODIFY &&
4085 staged_status != GOT_STATUS_ADD &&
4086 staged_status != GOT_STATUS_DELETE)
4087 return NULL;
4088 } else {
4089 if (status == GOT_STATUS_CONFLICT)
4090 return got_error(GOT_ERR_COMMIT_CONFLICT);
4092 if (status != GOT_STATUS_MODIFY &&
4093 status != GOT_STATUS_MODE_CHANGE &&
4094 status != GOT_STATUS_ADD &&
4095 status != GOT_STATUS_DELETE)
4096 return NULL;
4099 if (asprintf(&path, "/%s", relpath) == -1) {
4100 err = got_error_from_errno("asprintf");
4101 goto done;
4103 if (strcmp(path, "/") == 0) {
4104 parent_path = strdup("");
4105 if (parent_path == NULL)
4106 return got_error_from_errno("strdup");
4107 } else {
4108 err = got_path_dirname(&parent_path, path);
4109 if (err)
4110 return err;
4113 ct = calloc(1, sizeof(*ct));
4114 if (ct == NULL) {
4115 err = got_error_from_errno("calloc");
4116 goto done;
4119 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4120 relpath) == -1) {
4121 err = got_error_from_errno("asprintf");
4122 goto done;
4124 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4125 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4126 } else {
4127 if (dirfd != -1) {
4128 if (fstatat(dirfd, de_name, &sb,
4129 AT_SYMLINK_NOFOLLOW) == -1) {
4130 err = got_error_from_errno2("fstatat",
4131 ct->ondisk_path);
4132 goto done;
4134 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4135 err = got_error_from_errno2("lstat", ct->ondisk_path);
4136 goto done;
4138 ct->mode = sb.st_mode;
4141 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4142 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4143 relpath) == -1) {
4144 err = got_error_from_errno("asprintf");
4145 goto done;
4148 ct->status = status;
4149 ct->staged_status = staged_status;
4150 ct->blob_id = NULL; /* will be filled in when blob gets created */
4151 if (ct->status != GOT_STATUS_ADD &&
4152 ct->staged_status != GOT_STATUS_ADD) {
4153 ct->base_blob_id = got_object_id_dup(blob_id);
4154 if (ct->base_blob_id == NULL) {
4155 err = got_error_from_errno("got_object_id_dup");
4156 goto done;
4158 ct->base_commit_id = got_object_id_dup(commit_id);
4159 if (ct->base_commit_id == NULL) {
4160 err = got_error_from_errno("got_object_id_dup");
4161 goto done;
4164 if (ct->staged_status == GOT_STATUS_ADD ||
4165 ct->staged_status == GOT_STATUS_MODIFY) {
4166 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4167 if (ct->staged_blob_id == NULL) {
4168 err = got_error_from_errno("got_object_id_dup");
4169 goto done;
4172 ct->path = strdup(path);
4173 if (ct->path == NULL) {
4174 err = got_error_from_errno("strdup");
4175 goto done;
4177 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4178 done:
4179 if (ct && (err || new == NULL))
4180 free_commitable(ct);
4181 free(parent_path);
4182 free(path);
4183 return err;
4186 static const struct got_error *write_tree(struct got_object_id **, int *,
4187 struct got_tree_object *, const char *, struct got_pathlist_head *,
4188 got_worktree_status_cb status_cb, void *status_arg,
4189 struct got_repository *);
4191 static const struct got_error *
4192 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4193 struct got_tree_entry *te, const char *parent_path,
4194 struct got_pathlist_head *commitable_paths,
4195 got_worktree_status_cb status_cb, void *status_arg,
4196 struct got_repository *repo)
4198 const struct got_error *err = NULL;
4199 struct got_tree_object *subtree;
4200 char *subpath;
4202 if (asprintf(&subpath, "%s%s%s", parent_path,
4203 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4204 return got_error_from_errno("asprintf");
4206 err = got_object_open_as_tree(&subtree, repo, &te->id);
4207 if (err)
4208 return err;
4210 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4211 commitable_paths, status_cb, status_arg, repo);
4212 got_object_tree_close(subtree);
4213 free(subpath);
4214 return err;
4217 static const struct got_error *
4218 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4220 const struct got_error *err = NULL;
4221 char *ct_parent_path = NULL;
4223 *match = 0;
4225 if (strchr(ct->in_repo_path, '/') == NULL) {
4226 *match = got_path_is_root_dir(path);
4227 return NULL;
4230 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4231 if (err)
4232 return err;
4233 *match = (strcmp(path, ct_parent_path) == 0);
4234 free(ct_parent_path);
4235 return err;
4238 static mode_t
4239 get_ct_file_mode(struct got_commitable *ct)
4241 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4244 static const struct got_error *
4245 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4246 struct got_tree_entry *te, struct got_commitable *ct)
4248 const struct got_error *err = NULL;
4250 *new_te = NULL;
4252 err = got_object_tree_entry_dup(new_te, te);
4253 if (err)
4254 goto done;
4256 (*new_te)->mode = get_ct_file_mode(ct);
4258 if (ct->staged_status == GOT_STATUS_MODIFY)
4259 memcpy(&(*new_te)->id, ct->staged_blob_id,
4260 sizeof((*new_te)->id));
4261 else
4262 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4263 done:
4264 if (err && *new_te) {
4265 free(*new_te);
4266 *new_te = NULL;
4268 return err;
4271 static const struct got_error *
4272 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4273 struct got_commitable *ct)
4275 const struct got_error *err = NULL;
4276 char *ct_name;
4278 *new_te = NULL;
4280 *new_te = calloc(1, sizeof(**new_te));
4281 if (*new_te == NULL)
4282 return got_error_from_errno("calloc");
4284 ct_name = basename(ct->path);
4285 if (ct_name == NULL) {
4286 err = got_error_from_errno2("basename", ct->path);
4287 goto done;
4289 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4290 sizeof((*new_te)->name)) {
4291 err = got_error(GOT_ERR_NO_SPACE);
4292 goto done;
4295 (*new_te)->mode = get_ct_file_mode(ct);
4297 if (ct->staged_status == GOT_STATUS_ADD)
4298 memcpy(&(*new_te)->id, ct->staged_blob_id,
4299 sizeof((*new_te)->id));
4300 else
4301 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4302 done:
4303 if (err && *new_te) {
4304 free(*new_te);
4305 *new_te = NULL;
4307 return err;
4310 static const struct got_error *
4311 insert_tree_entry(struct got_tree_entry *new_te,
4312 struct got_pathlist_head *paths)
4314 const struct got_error *err = NULL;
4315 struct got_pathlist_entry *new_pe;
4317 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4318 if (err)
4319 return err;
4320 if (new_pe == NULL)
4321 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4322 return NULL;
4325 static const struct got_error *
4326 report_ct_status(struct got_commitable *ct,
4327 got_worktree_status_cb status_cb, void *status_arg)
4329 const char *ct_path = ct->path;
4330 unsigned char status;
4332 while (ct_path[0] == '/')
4333 ct_path++;
4335 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4336 status = ct->staged_status;
4337 else
4338 status = ct->status;
4340 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4341 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4344 static const struct got_error *
4345 match_modified_subtree(int *modified, struct got_tree_entry *te,
4346 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4348 const struct got_error *err = NULL;
4349 struct got_pathlist_entry *pe;
4350 char *te_path;
4352 *modified = 0;
4354 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4355 got_path_is_root_dir(base_tree_path) ? "" : "/",
4356 te->name) == -1)
4357 return got_error_from_errno("asprintf");
4359 TAILQ_FOREACH(pe, commitable_paths, entry) {
4360 struct got_commitable *ct = pe->data;
4361 *modified = got_path_is_child(ct->in_repo_path, te_path,
4362 strlen(te_path));
4363 if (*modified)
4364 break;
4367 free(te_path);
4368 return err;
4371 static const struct got_error *
4372 match_deleted_or_modified_ct(struct got_commitable **ctp,
4373 struct got_tree_entry *te, const char *base_tree_path,
4374 struct got_pathlist_head *commitable_paths)
4376 const struct got_error *err = NULL;
4377 struct got_pathlist_entry *pe;
4379 *ctp = NULL;
4381 TAILQ_FOREACH(pe, commitable_paths, entry) {
4382 struct got_commitable *ct = pe->data;
4383 char *ct_name = NULL;
4384 int path_matches;
4386 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4387 if (ct->status != GOT_STATUS_MODIFY &&
4388 ct->status != GOT_STATUS_MODE_CHANGE &&
4389 ct->status != GOT_STATUS_DELETE)
4390 continue;
4391 } else {
4392 if (ct->staged_status != GOT_STATUS_MODIFY &&
4393 ct->staged_status != GOT_STATUS_DELETE)
4394 continue;
4397 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4398 continue;
4400 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4401 if (err)
4402 return err;
4403 if (!path_matches)
4404 continue;
4406 ct_name = basename(pe->path);
4407 if (ct_name == NULL)
4408 return got_error_from_errno2("basename", pe->path);
4410 if (strcmp(te->name, ct_name) != 0)
4411 continue;
4413 *ctp = ct;
4414 break;
4417 return err;
4420 static const struct got_error *
4421 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4422 const char *child_path, const char *path_base_tree,
4423 struct got_pathlist_head *commitable_paths,
4424 got_worktree_status_cb status_cb, void *status_arg,
4425 struct got_repository *repo)
4427 const struct got_error *err = NULL;
4428 struct got_tree_entry *new_te;
4429 char *subtree_path;
4430 struct got_object_id *id = NULL;
4431 int nentries;
4433 *new_tep = NULL;
4435 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4436 got_path_is_root_dir(path_base_tree) ? "" : "/",
4437 child_path) == -1)
4438 return got_error_from_errno("asprintf");
4440 new_te = calloc(1, sizeof(*new_te));
4441 if (new_te == NULL)
4442 return got_error_from_errno("calloc");
4443 new_te->mode = S_IFDIR;
4445 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4446 sizeof(new_te->name)) {
4447 err = got_error(GOT_ERR_NO_SPACE);
4448 goto done;
4450 err = write_tree(&id, &nentries, NULL, subtree_path,
4451 commitable_paths, status_cb, status_arg, repo);
4452 if (err) {
4453 free(new_te);
4454 goto done;
4456 memcpy(&new_te->id, id, sizeof(new_te->id));
4457 done:
4458 free(id);
4459 free(subtree_path);
4460 if (err == NULL)
4461 *new_tep = new_te;
4462 return err;
4465 static const struct got_error *
4466 write_tree(struct got_object_id **new_tree_id, int *nentries,
4467 struct got_tree_object *base_tree, const char *path_base_tree,
4468 struct got_pathlist_head *commitable_paths,
4469 got_worktree_status_cb status_cb, void *status_arg,
4470 struct got_repository *repo)
4472 const struct got_error *err = NULL;
4473 struct got_pathlist_head paths;
4474 struct got_tree_entry *te, *new_te = NULL;
4475 struct got_pathlist_entry *pe;
4477 TAILQ_INIT(&paths);
4478 *nentries = 0;
4480 /* Insert, and recurse into, newly added entries first. */
4481 TAILQ_FOREACH(pe, commitable_paths, entry) {
4482 struct got_commitable *ct = pe->data;
4483 char *child_path = NULL, *slash;
4485 if ((ct->status != GOT_STATUS_ADD &&
4486 ct->staged_status != GOT_STATUS_ADD) ||
4487 (ct->flags & GOT_COMMITABLE_ADDED))
4488 continue;
4490 if (!got_path_is_child(pe->path, path_base_tree,
4491 strlen(path_base_tree)))
4492 continue;
4494 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4495 pe->path);
4496 if (err)
4497 goto done;
4499 slash = strchr(child_path, '/');
4500 if (slash == NULL) {
4501 err = alloc_added_blob_tree_entry(&new_te, ct);
4502 if (err)
4503 goto done;
4504 err = report_ct_status(ct, status_cb, status_arg);
4505 if (err)
4506 goto done;
4507 ct->flags |= GOT_COMMITABLE_ADDED;
4508 err = insert_tree_entry(new_te, &paths);
4509 if (err)
4510 goto done;
4511 (*nentries)++;
4512 } else {
4513 *slash = '\0'; /* trim trailing path components */
4514 if (base_tree == NULL ||
4515 got_object_tree_find_entry(base_tree, child_path)
4516 == NULL) {
4517 err = make_subtree_for_added_blob(&new_te,
4518 child_path, path_base_tree,
4519 commitable_paths, status_cb, status_arg,
4520 repo);
4521 if (err)
4522 goto done;
4523 err = insert_tree_entry(new_te, &paths);
4524 if (err)
4525 goto done;
4526 (*nentries)++;
4531 if (base_tree) {
4532 int i, nbase_entries;
4533 /* Handle modified and deleted entries. */
4534 nbase_entries = got_object_tree_get_nentries(base_tree);
4535 for (i = 0; i < nbase_entries; i++) {
4536 struct got_commitable *ct = NULL;
4538 te = got_object_tree_get_entry(base_tree, i);
4539 if (got_object_tree_entry_is_submodule(te)) {
4540 /* Entry is a submodule; just copy it. */
4541 err = got_object_tree_entry_dup(&new_te, te);
4542 if (err)
4543 goto done;
4544 err = insert_tree_entry(new_te, &paths);
4545 if (err)
4546 goto done;
4547 (*nentries)++;
4548 continue;
4551 if (S_ISDIR(te->mode)) {
4552 int modified;
4553 err = got_object_tree_entry_dup(&new_te, te);
4554 if (err)
4555 goto done;
4556 err = match_modified_subtree(&modified, te,
4557 path_base_tree, commitable_paths);
4558 if (err)
4559 goto done;
4560 /* Avoid recursion into unmodified subtrees. */
4561 if (modified) {
4562 struct got_object_id *new_id;
4563 int nsubentries;
4564 err = write_subtree(&new_id,
4565 &nsubentries, te,
4566 path_base_tree, commitable_paths,
4567 status_cb, status_arg, repo);
4568 if (err)
4569 goto done;
4570 if (nsubentries == 0) {
4571 /* All entries were deleted. */
4572 free(new_id);
4573 continue;
4575 memcpy(&new_te->id, new_id,
4576 sizeof(new_te->id));
4577 free(new_id);
4579 err = insert_tree_entry(new_te, &paths);
4580 if (err)
4581 goto done;
4582 (*nentries)++;
4583 continue;
4586 err = match_deleted_or_modified_ct(&ct, te,
4587 path_base_tree, commitable_paths);
4588 if (err)
4589 goto done;
4590 if (ct) {
4591 /* NB: Deleted entries get dropped here. */
4592 if (ct->status == GOT_STATUS_MODIFY ||
4593 ct->status == GOT_STATUS_MODE_CHANGE ||
4594 ct->staged_status == GOT_STATUS_MODIFY) {
4595 err = alloc_modified_blob_tree_entry(
4596 &new_te, te, ct);
4597 if (err)
4598 goto done;
4599 err = insert_tree_entry(new_te, &paths);
4600 if (err)
4601 goto done;
4602 (*nentries)++;
4604 err = report_ct_status(ct, status_cb,
4605 status_arg);
4606 if (err)
4607 goto done;
4608 } else {
4609 /* Entry is unchanged; just copy it. */
4610 err = got_object_tree_entry_dup(&new_te, te);
4611 if (err)
4612 goto done;
4613 err = insert_tree_entry(new_te, &paths);
4614 if (err)
4615 goto done;
4616 (*nentries)++;
4621 /* Write new list of entries; deleted entries have been dropped. */
4622 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4623 done:
4624 got_pathlist_free(&paths);
4625 return err;
4628 static const struct got_error *
4629 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4630 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4631 int have_staged_files)
4633 const struct got_error *err = NULL;
4634 struct got_pathlist_entry *pe;
4636 TAILQ_FOREACH(pe, commitable_paths, entry) {
4637 struct got_fileindex_entry *ie;
4638 struct got_commitable *ct = pe->data;
4640 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4641 if (ie) {
4642 if (ct->status == GOT_STATUS_DELETE ||
4643 ct->staged_status == GOT_STATUS_DELETE) {
4644 got_fileindex_entry_remove(fileindex, ie);
4645 } else if (ct->staged_status == GOT_STATUS_ADD ||
4646 ct->staged_status == GOT_STATUS_MODIFY) {
4647 got_fileindex_entry_stage_set(ie,
4648 GOT_FILEIDX_STAGE_NONE);
4649 err = got_fileindex_entry_update(ie,
4650 ct->ondisk_path, ct->staged_blob_id->sha1,
4651 new_base_commit_id->sha1,
4652 !have_staged_files);
4653 } else
4654 err = got_fileindex_entry_update(ie,
4655 ct->ondisk_path, ct->blob_id->sha1,
4656 new_base_commit_id->sha1,
4657 !have_staged_files);
4658 } else {
4659 err = got_fileindex_entry_alloc(&ie, pe->path);
4660 if (err)
4661 break;
4662 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4663 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4664 if (err) {
4665 got_fileindex_entry_free(ie);
4666 break;
4668 err = got_fileindex_entry_add(fileindex, ie);
4669 if (err) {
4670 got_fileindex_entry_free(ie);
4671 break;
4675 return err;
4679 static const struct got_error *
4680 check_out_of_date(const char *in_repo_path, unsigned char status,
4681 unsigned char staged_status, struct got_object_id *base_blob_id,
4682 struct got_object_id *base_commit_id,
4683 struct got_object_id *head_commit_id, struct got_repository *repo,
4684 int ood_errcode)
4686 const struct got_error *err = NULL;
4687 struct got_object_id *id = NULL;
4689 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4690 /* Trivial case: base commit == head commit */
4691 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4692 return NULL;
4694 * Ensure file content which local changes were based
4695 * on matches file content in the branch head.
4697 err = got_object_id_by_path(&id, repo, head_commit_id,
4698 in_repo_path);
4699 if (err) {
4700 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4701 err = got_error(ood_errcode);
4702 goto done;
4703 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4704 err = got_error(ood_errcode);
4705 } else {
4706 /* Require that added files don't exist in the branch head. */
4707 err = got_object_id_by_path(&id, repo, head_commit_id,
4708 in_repo_path);
4709 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4710 goto done;
4711 err = id ? got_error(ood_errcode) : NULL;
4713 done:
4714 free(id);
4715 return err;
4718 const struct got_error *
4719 commit_worktree(struct got_object_id **new_commit_id,
4720 struct got_pathlist_head *commitable_paths,
4721 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4722 const char *author, const char *committer,
4723 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4724 got_worktree_status_cb status_cb, void *status_arg,
4725 struct got_repository *repo)
4727 const struct got_error *err = NULL, *unlockerr = NULL;
4728 struct got_pathlist_entry *pe;
4729 const char *head_ref_name = NULL;
4730 struct got_commit_object *head_commit = NULL;
4731 struct got_reference *head_ref2 = NULL;
4732 struct got_object_id *head_commit_id2 = NULL;
4733 struct got_tree_object *head_tree = NULL;
4734 struct got_object_id *new_tree_id = NULL;
4735 int nentries;
4736 struct got_object_id_queue parent_ids;
4737 struct got_object_qid *pid = NULL;
4738 char *logmsg = NULL;
4740 *new_commit_id = NULL;
4742 SIMPLEQ_INIT(&parent_ids);
4744 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4745 if (err)
4746 goto done;
4748 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4749 if (err)
4750 goto done;
4752 if (commit_msg_cb != NULL) {
4753 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4754 if (err)
4755 goto done;
4758 if (logmsg == NULL || strlen(logmsg) == 0) {
4759 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4760 goto done;
4763 /* Create blobs from added and modified files and record their IDs. */
4764 TAILQ_FOREACH(pe, commitable_paths, entry) {
4765 struct got_commitable *ct = pe->data;
4766 char *ondisk_path;
4768 /* Blobs for staged files already exist. */
4769 if (ct->staged_status == GOT_STATUS_ADD ||
4770 ct->staged_status == GOT_STATUS_MODIFY)
4771 continue;
4773 if (ct->status != GOT_STATUS_ADD &&
4774 ct->status != GOT_STATUS_MODIFY &&
4775 ct->status != GOT_STATUS_MODE_CHANGE)
4776 continue;
4778 if (asprintf(&ondisk_path, "%s/%s",
4779 worktree->root_path, pe->path) == -1) {
4780 err = got_error_from_errno("asprintf");
4781 goto done;
4783 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4784 free(ondisk_path);
4785 if (err)
4786 goto done;
4789 /* Recursively write new tree objects. */
4790 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4791 commitable_paths, status_cb, status_arg, repo);
4792 if (err)
4793 goto done;
4795 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4796 if (err)
4797 goto done;
4798 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4799 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4800 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4801 got_object_qid_free(pid);
4802 if (logmsg != NULL)
4803 free(logmsg);
4804 if (err)
4805 goto done;
4807 /* Check if a concurrent commit to our branch has occurred. */
4808 head_ref_name = got_worktree_get_head_ref_name(worktree);
4809 if (head_ref_name == NULL) {
4810 err = got_error_from_errno("got_worktree_get_head_ref_name");
4811 goto done;
4813 /* Lock the reference here to prevent concurrent modification. */
4814 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4815 if (err)
4816 goto done;
4817 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4818 if (err)
4819 goto done;
4820 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4821 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4822 goto done;
4824 /* Update branch head in repository. */
4825 err = got_ref_change_ref(head_ref2, *new_commit_id);
4826 if (err)
4827 goto done;
4828 err = got_ref_write(head_ref2, repo);
4829 if (err)
4830 goto done;
4832 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4833 if (err)
4834 goto done;
4836 err = ref_base_commit(worktree, repo);
4837 if (err)
4838 goto done;
4839 done:
4840 if (head_tree)
4841 got_object_tree_close(head_tree);
4842 if (head_commit)
4843 got_object_commit_close(head_commit);
4844 free(head_commit_id2);
4845 if (head_ref2) {
4846 unlockerr = got_ref_unlock(head_ref2);
4847 if (unlockerr && err == NULL)
4848 err = unlockerr;
4849 got_ref_close(head_ref2);
4851 return err;
4854 static const struct got_error *
4855 check_path_is_commitable(const char *path,
4856 struct got_pathlist_head *commitable_paths)
4858 struct got_pathlist_entry *cpe = NULL;
4859 size_t path_len = strlen(path);
4861 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4862 struct got_commitable *ct = cpe->data;
4863 const char *ct_path = ct->path;
4865 while (ct_path[0] == '/')
4866 ct_path++;
4868 if (strcmp(path, ct_path) == 0 ||
4869 got_path_is_child(ct_path, path, path_len))
4870 break;
4873 if (cpe == NULL)
4874 return got_error_path(path, GOT_ERR_BAD_PATH);
4876 return NULL;
4879 static const struct got_error *
4880 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4882 int *have_staged_files = arg;
4884 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4885 *have_staged_files = 1;
4886 return got_error(GOT_ERR_CANCELLED);
4889 return NULL;
4892 static const struct got_error *
4893 check_non_staged_files(struct got_fileindex *fileindex,
4894 struct got_pathlist_head *paths)
4896 struct got_pathlist_entry *pe;
4897 struct got_fileindex_entry *ie;
4899 TAILQ_FOREACH(pe, paths, entry) {
4900 if (pe->path[0] == '\0')
4901 continue;
4902 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4903 if (ie == NULL)
4904 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4905 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4906 return got_error_path(pe->path,
4907 GOT_ERR_FILE_NOT_STAGED);
4910 return NULL;
4913 const struct got_error *
4914 got_worktree_commit(struct got_object_id **new_commit_id,
4915 struct got_worktree *worktree, struct got_pathlist_head *paths,
4916 const char *author, const char *committer,
4917 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4918 got_worktree_status_cb status_cb, void *status_arg,
4919 struct got_repository *repo)
4921 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4922 struct got_fileindex *fileindex = NULL;
4923 char *fileindex_path = NULL;
4924 struct got_pathlist_head commitable_paths;
4925 struct collect_commitables_arg cc_arg;
4926 struct got_pathlist_entry *pe;
4927 struct got_reference *head_ref = NULL;
4928 struct got_object_id *head_commit_id = NULL;
4929 int have_staged_files = 0;
4931 *new_commit_id = NULL;
4933 TAILQ_INIT(&commitable_paths);
4935 err = lock_worktree(worktree, LOCK_EX);
4936 if (err)
4937 goto done;
4939 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4940 if (err)
4941 goto done;
4943 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4944 if (err)
4945 goto done;
4947 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4948 if (err)
4949 goto done;
4951 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4952 &have_staged_files);
4953 if (err && err->code != GOT_ERR_CANCELLED)
4954 goto done;
4955 if (have_staged_files) {
4956 err = check_non_staged_files(fileindex, paths);
4957 if (err)
4958 goto done;
4961 cc_arg.commitable_paths = &commitable_paths;
4962 cc_arg.worktree = worktree;
4963 cc_arg.repo = repo;
4964 cc_arg.have_staged_files = have_staged_files;
4965 TAILQ_FOREACH(pe, paths, entry) {
4966 err = worktree_status(worktree, pe->path, fileindex, repo,
4967 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4968 if (err)
4969 goto done;
4972 if (TAILQ_EMPTY(&commitable_paths)) {
4973 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4974 goto done;
4977 TAILQ_FOREACH(pe, paths, entry) {
4978 err = check_path_is_commitable(pe->path, &commitable_paths);
4979 if (err)
4980 goto done;
4983 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4984 struct got_commitable *ct = pe->data;
4985 const char *ct_path = ct->in_repo_path;
4987 while (ct_path[0] == '/')
4988 ct_path++;
4989 err = check_out_of_date(ct_path, ct->status,
4990 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4991 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4992 if (err)
4993 goto done;
4997 err = commit_worktree(new_commit_id, &commitable_paths,
4998 head_commit_id, worktree, author, committer,
4999 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5000 if (err)
5001 goto done;
5003 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5004 fileindex, have_staged_files);
5005 sync_err = sync_fileindex(fileindex, fileindex_path);
5006 if (sync_err && err == NULL)
5007 err = sync_err;
5008 done:
5009 if (fileindex)
5010 got_fileindex_free(fileindex);
5011 free(fileindex_path);
5012 unlockerr = lock_worktree(worktree, LOCK_SH);
5013 if (unlockerr && err == NULL)
5014 err = unlockerr;
5015 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5016 struct got_commitable *ct = pe->data;
5017 free_commitable(ct);
5019 got_pathlist_free(&commitable_paths);
5020 return err;
5023 const char *
5024 got_commitable_get_path(struct got_commitable *ct)
5026 return ct->path;
5029 unsigned int
5030 got_commitable_get_status(struct got_commitable *ct)
5032 return ct->status;
5035 struct check_rebase_ok_arg {
5036 struct got_worktree *worktree;
5037 struct got_repository *repo;
5040 static const struct got_error *
5041 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5043 const struct got_error *err = NULL;
5044 struct check_rebase_ok_arg *a = arg;
5045 unsigned char status;
5046 struct stat sb;
5047 char *ondisk_path;
5049 /* Reject rebase of a work tree with mixed base commits. */
5050 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5051 SHA1_DIGEST_LENGTH))
5052 return got_error(GOT_ERR_MIXED_COMMITS);
5054 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5055 == -1)
5056 return got_error_from_errno("asprintf");
5058 /* Reject rebase of a work tree with modified or staged files. */
5059 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5060 free(ondisk_path);
5061 if (err)
5062 return err;
5064 if (status != GOT_STATUS_NO_CHANGE)
5065 return got_error(GOT_ERR_MODIFIED);
5066 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5067 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5069 return NULL;
5072 const struct got_error *
5073 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5074 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5075 struct got_worktree *worktree, struct got_reference *branch,
5076 struct got_repository *repo)
5078 const struct got_error *err = NULL;
5079 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5080 char *branch_ref_name = NULL;
5081 char *fileindex_path = NULL;
5082 struct check_rebase_ok_arg ok_arg;
5083 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5084 struct got_object_id *wt_branch_tip = NULL;
5086 *new_base_branch_ref = NULL;
5087 *tmp_branch = NULL;
5088 *fileindex = NULL;
5090 err = lock_worktree(worktree, LOCK_EX);
5091 if (err)
5092 return err;
5094 err = open_fileindex(fileindex, &fileindex_path, worktree);
5095 if (err)
5096 goto done;
5098 ok_arg.worktree = worktree;
5099 ok_arg.repo = repo;
5100 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5101 &ok_arg);
5102 if (err)
5103 goto done;
5105 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5106 if (err)
5107 goto done;
5109 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5110 if (err)
5111 goto done;
5113 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5114 if (err)
5115 goto done;
5117 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5118 0);
5119 if (err)
5120 goto done;
5122 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5123 if (err)
5124 goto done;
5125 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5126 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5127 goto done;
5130 err = got_ref_alloc_symref(new_base_branch_ref,
5131 new_base_branch_ref_name, wt_branch);
5132 if (err)
5133 goto done;
5134 err = got_ref_write(*new_base_branch_ref, repo);
5135 if (err)
5136 goto done;
5138 /* TODO Lock original branch's ref while rebasing? */
5140 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5141 if (err)
5142 goto done;
5144 err = got_ref_write(branch_ref, repo);
5145 if (err)
5146 goto done;
5148 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5149 worktree->base_commit_id);
5150 if (err)
5151 goto done;
5152 err = got_ref_write(*tmp_branch, repo);
5153 if (err)
5154 goto done;
5156 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5157 if (err)
5158 goto done;
5159 done:
5160 free(fileindex_path);
5161 free(tmp_branch_name);
5162 free(new_base_branch_ref_name);
5163 free(branch_ref_name);
5164 if (branch_ref)
5165 got_ref_close(branch_ref);
5166 if (wt_branch)
5167 got_ref_close(wt_branch);
5168 free(wt_branch_tip);
5169 if (err) {
5170 if (*new_base_branch_ref) {
5171 got_ref_close(*new_base_branch_ref);
5172 *new_base_branch_ref = NULL;
5174 if (*tmp_branch) {
5175 got_ref_close(*tmp_branch);
5176 *tmp_branch = NULL;
5178 if (*fileindex) {
5179 got_fileindex_free(*fileindex);
5180 *fileindex = NULL;
5182 lock_worktree(worktree, LOCK_SH);
5184 return err;
5187 const struct got_error *
5188 got_worktree_rebase_continue(struct got_object_id **commit_id,
5189 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5190 struct got_reference **branch, struct got_fileindex **fileindex,
5191 struct got_worktree *worktree, struct got_repository *repo)
5193 const struct got_error *err;
5194 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5195 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5196 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5197 char *fileindex_path = NULL;
5198 int have_staged_files = 0;
5200 *commit_id = NULL;
5201 *new_base_branch = NULL;
5202 *tmp_branch = NULL;
5203 *branch = NULL;
5204 *fileindex = NULL;
5206 err = lock_worktree(worktree, LOCK_EX);
5207 if (err)
5208 return err;
5210 err = open_fileindex(fileindex, &fileindex_path, worktree);
5211 if (err)
5212 goto done;
5214 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5215 &have_staged_files);
5216 if (err && err->code != GOT_ERR_CANCELLED)
5217 goto done;
5218 if (have_staged_files) {
5219 err = got_error(GOT_ERR_STAGED_PATHS);
5220 goto done;
5223 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5224 if (err)
5225 goto done;
5227 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5228 if (err)
5229 goto done;
5231 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5232 if (err)
5233 goto done;
5235 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5236 if (err)
5237 goto done;
5239 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5240 if (err)
5241 goto done;
5243 err = got_ref_open(branch, repo,
5244 got_ref_get_symref_target(branch_ref), 0);
5245 if (err)
5246 goto done;
5248 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5249 if (err)
5250 goto done;
5252 err = got_ref_resolve(commit_id, repo, commit_ref);
5253 if (err)
5254 goto done;
5256 err = got_ref_open(new_base_branch, repo,
5257 new_base_branch_ref_name, 0);
5258 if (err)
5259 goto done;
5261 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5262 if (err)
5263 goto done;
5264 done:
5265 free(commit_ref_name);
5266 free(branch_ref_name);
5267 free(fileindex_path);
5268 if (commit_ref)
5269 got_ref_close(commit_ref);
5270 if (branch_ref)
5271 got_ref_close(branch_ref);
5272 if (err) {
5273 free(*commit_id);
5274 *commit_id = NULL;
5275 if (*tmp_branch) {
5276 got_ref_close(*tmp_branch);
5277 *tmp_branch = NULL;
5279 if (*new_base_branch) {
5280 got_ref_close(*new_base_branch);
5281 *new_base_branch = NULL;
5283 if (*branch) {
5284 got_ref_close(*branch);
5285 *branch = NULL;
5287 if (*fileindex) {
5288 got_fileindex_free(*fileindex);
5289 *fileindex = NULL;
5291 lock_worktree(worktree, LOCK_SH);
5293 return err;
5296 const struct got_error *
5297 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5299 const struct got_error *err;
5300 char *tmp_branch_name = NULL;
5302 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5303 if (err)
5304 return err;
5306 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5307 free(tmp_branch_name);
5308 return NULL;
5311 static const struct got_error *
5312 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5313 char **logmsg, void *arg)
5315 *logmsg = arg;
5316 return NULL;
5319 static const struct got_error *
5320 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5321 const char *path, struct got_object_id *blob_id,
5322 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5323 int dirfd, const char *de_name)
5325 return NULL;
5328 struct collect_merged_paths_arg {
5329 got_worktree_checkout_cb progress_cb;
5330 void *progress_arg;
5331 struct got_pathlist_head *merged_paths;
5334 static const struct got_error *
5335 collect_merged_paths(void *arg, unsigned char status, const char *path)
5337 const struct got_error *err;
5338 struct collect_merged_paths_arg *a = arg;
5339 char *p;
5340 struct got_pathlist_entry *new;
5342 err = (*a->progress_cb)(a->progress_arg, status, path);
5343 if (err)
5344 return err;
5346 if (status != GOT_STATUS_MERGE &&
5347 status != GOT_STATUS_ADD &&
5348 status != GOT_STATUS_DELETE &&
5349 status != GOT_STATUS_CONFLICT)
5350 return NULL;
5352 p = strdup(path);
5353 if (p == NULL)
5354 return got_error_from_errno("strdup");
5356 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5357 if (err || new == NULL)
5358 free(p);
5359 return err;
5362 void
5363 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5365 struct got_pathlist_entry *pe;
5367 TAILQ_FOREACH(pe, merged_paths, entry)
5368 free((char *)pe->path);
5370 got_pathlist_free(merged_paths);
5373 static const struct got_error *
5374 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5375 int is_rebase, struct got_repository *repo)
5377 const struct got_error *err;
5378 struct got_reference *commit_ref = NULL;
5380 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5381 if (err) {
5382 if (err->code != GOT_ERR_NOT_REF)
5383 goto done;
5384 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5385 if (err)
5386 goto done;
5387 err = got_ref_write(commit_ref, repo);
5388 if (err)
5389 goto done;
5390 } else if (is_rebase) {
5391 struct got_object_id *stored_id;
5392 int cmp;
5394 err = got_ref_resolve(&stored_id, repo, commit_ref);
5395 if (err)
5396 goto done;
5397 cmp = got_object_id_cmp(commit_id, stored_id);
5398 free(stored_id);
5399 if (cmp != 0) {
5400 err = got_error(GOT_ERR_REBASE_COMMITID);
5401 goto done;
5404 done:
5405 if (commit_ref)
5406 got_ref_close(commit_ref);
5407 return err;
5410 static const struct got_error *
5411 rebase_merge_files(struct got_pathlist_head *merged_paths,
5412 const char *commit_ref_name, struct got_worktree *worktree,
5413 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5414 struct got_object_id *commit_id, struct got_repository *repo,
5415 got_worktree_checkout_cb progress_cb, void *progress_arg,
5416 got_cancel_cb cancel_cb, void *cancel_arg)
5418 const struct got_error *err;
5419 struct got_reference *commit_ref = NULL;
5420 struct collect_merged_paths_arg cmp_arg;
5421 char *fileindex_path;
5423 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5425 err = get_fileindex_path(&fileindex_path, worktree);
5426 if (err)
5427 return err;
5429 cmp_arg.progress_cb = progress_cb;
5430 cmp_arg.progress_arg = progress_arg;
5431 cmp_arg.merged_paths = merged_paths;
5432 err = merge_files(worktree, fileindex, fileindex_path,
5433 parent_commit_id, commit_id, repo, collect_merged_paths,
5434 &cmp_arg, cancel_cb, cancel_arg);
5435 if (commit_ref)
5436 got_ref_close(commit_ref);
5437 return err;
5440 const struct got_error *
5441 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5442 struct got_worktree *worktree, struct got_fileindex *fileindex,
5443 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5444 struct got_repository *repo,
5445 got_worktree_checkout_cb progress_cb, void *progress_arg,
5446 got_cancel_cb cancel_cb, void *cancel_arg)
5448 const struct got_error *err;
5449 char *commit_ref_name;
5451 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5452 if (err)
5453 return err;
5455 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5456 if (err)
5457 goto done;
5459 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5460 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5461 progress_arg, cancel_cb, cancel_arg);
5462 done:
5463 free(commit_ref_name);
5464 return err;
5467 const struct got_error *
5468 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5469 struct got_worktree *worktree, struct got_fileindex *fileindex,
5470 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5471 struct got_repository *repo,
5472 got_worktree_checkout_cb progress_cb, void *progress_arg,
5473 got_cancel_cb cancel_cb, void *cancel_arg)
5475 const struct got_error *err;
5476 char *commit_ref_name;
5478 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5479 if (err)
5480 return err;
5482 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5483 if (err)
5484 goto done;
5486 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5487 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5488 progress_arg, cancel_cb, cancel_arg);
5489 done:
5490 free(commit_ref_name);
5491 return err;
5494 static const struct got_error *
5495 rebase_commit(struct got_object_id **new_commit_id,
5496 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5497 struct got_worktree *worktree, struct got_fileindex *fileindex,
5498 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5499 const char *new_logmsg, struct got_repository *repo)
5501 const struct got_error *err, *sync_err;
5502 struct got_pathlist_head commitable_paths;
5503 struct collect_commitables_arg cc_arg;
5504 char *fileindex_path = NULL;
5505 struct got_reference *head_ref = NULL;
5506 struct got_object_id *head_commit_id = NULL;
5507 char *logmsg = NULL;
5509 TAILQ_INIT(&commitable_paths);
5510 *new_commit_id = NULL;
5512 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5514 err = get_fileindex_path(&fileindex_path, worktree);
5515 if (err)
5516 return err;
5518 cc_arg.commitable_paths = &commitable_paths;
5519 cc_arg.worktree = worktree;
5520 cc_arg.repo = repo;
5521 cc_arg.have_staged_files = 0;
5523 * If possible get the status of individual files directly to
5524 * avoid crawling the entire work tree once per rebased commit.
5525 * TODO: Ideally, merged_paths would contain a list of commitables
5526 * we could use so we could skip worktree_status() entirely.
5528 if (merged_paths) {
5529 struct got_pathlist_entry *pe;
5530 TAILQ_FOREACH(pe, merged_paths, entry) {
5531 err = worktree_status(worktree, pe->path, fileindex,
5532 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5533 0);
5534 if (err)
5535 goto done;
5537 } else {
5538 err = worktree_status(worktree, "", fileindex, repo,
5539 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5540 if (err)
5541 goto done;
5544 if (TAILQ_EMPTY(&commitable_paths)) {
5545 /* No-op change; commit will be elided. */
5546 err = got_ref_delete(commit_ref, repo);
5547 if (err)
5548 goto done;
5549 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5550 goto done;
5553 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5554 if (err)
5555 goto done;
5557 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5558 if (err)
5559 goto done;
5561 if (new_logmsg) {
5562 logmsg = strdup(new_logmsg);
5563 if (logmsg == NULL) {
5564 err = got_error_from_errno("strdup");
5565 goto done;
5567 } else {
5568 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5569 if (err)
5570 goto done;
5573 /* NB: commit_worktree will call free(logmsg) */
5574 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5575 worktree, got_object_commit_get_author(orig_commit),
5576 got_object_commit_get_committer(orig_commit),
5577 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5578 if (err)
5579 goto done;
5581 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5582 if (err)
5583 goto done;
5585 err = got_ref_delete(commit_ref, repo);
5586 if (err)
5587 goto done;
5589 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5590 fileindex, 0);
5591 sync_err = sync_fileindex(fileindex, fileindex_path);
5592 if (sync_err && err == NULL)
5593 err = sync_err;
5594 done:
5595 free(fileindex_path);
5596 free(head_commit_id);
5597 if (head_ref)
5598 got_ref_close(head_ref);
5599 if (err) {
5600 free(*new_commit_id);
5601 *new_commit_id = NULL;
5603 return err;
5606 const struct got_error *
5607 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5608 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5609 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5610 struct got_commit_object *orig_commit,
5611 struct got_object_id *orig_commit_id, struct got_repository *repo)
5613 const struct got_error *err;
5614 char *commit_ref_name;
5615 struct got_reference *commit_ref = NULL;
5616 struct got_object_id *commit_id = NULL;
5618 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5619 if (err)
5620 return err;
5622 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5623 if (err)
5624 goto done;
5625 err = got_ref_resolve(&commit_id, repo, commit_ref);
5626 if (err)
5627 goto done;
5628 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5629 err = got_error(GOT_ERR_REBASE_COMMITID);
5630 goto done;
5633 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5634 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5635 done:
5636 if (commit_ref)
5637 got_ref_close(commit_ref);
5638 free(commit_ref_name);
5639 free(commit_id);
5640 return err;
5643 const struct got_error *
5644 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5645 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5646 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5647 struct got_commit_object *orig_commit,
5648 struct got_object_id *orig_commit_id, const char *new_logmsg,
5649 struct got_repository *repo)
5651 const struct got_error *err;
5652 char *commit_ref_name;
5653 struct got_reference *commit_ref = NULL;
5655 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5656 if (err)
5657 return err;
5659 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5660 if (err)
5661 goto done;
5663 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5664 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5665 done:
5666 if (commit_ref)
5667 got_ref_close(commit_ref);
5668 free(commit_ref_name);
5669 return err;
5672 const struct got_error *
5673 got_worktree_rebase_postpone(struct got_worktree *worktree,
5674 struct got_fileindex *fileindex)
5676 if (fileindex)
5677 got_fileindex_free(fileindex);
5678 return lock_worktree(worktree, LOCK_SH);
5681 static const struct got_error *
5682 delete_ref(const char *name, struct got_repository *repo)
5684 const struct got_error *err;
5685 struct got_reference *ref;
5687 err = got_ref_open(&ref, repo, name, 0);
5688 if (err) {
5689 if (err->code == GOT_ERR_NOT_REF)
5690 return NULL;
5691 return err;
5694 err = got_ref_delete(ref, repo);
5695 got_ref_close(ref);
5696 return err;
5699 static const struct got_error *
5700 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5702 const struct got_error *err;
5703 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5704 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5706 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5707 if (err)
5708 goto done;
5709 err = delete_ref(tmp_branch_name, repo);
5710 if (err)
5711 goto done;
5713 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5714 if (err)
5715 goto done;
5716 err = delete_ref(new_base_branch_ref_name, repo);
5717 if (err)
5718 goto done;
5720 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5721 if (err)
5722 goto done;
5723 err = delete_ref(branch_ref_name, repo);
5724 if (err)
5725 goto done;
5727 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5728 if (err)
5729 goto done;
5730 err = delete_ref(commit_ref_name, repo);
5731 if (err)
5732 goto done;
5734 done:
5735 free(tmp_branch_name);
5736 free(new_base_branch_ref_name);
5737 free(branch_ref_name);
5738 free(commit_ref_name);
5739 return err;
5742 const struct got_error *
5743 got_worktree_rebase_complete(struct got_worktree *worktree,
5744 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5745 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5746 struct got_repository *repo)
5748 const struct got_error *err, *unlockerr;
5749 struct got_object_id *new_head_commit_id = NULL;
5751 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5752 if (err)
5753 return err;
5755 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5756 if (err)
5757 goto done;
5759 err = got_ref_write(rebased_branch, repo);
5760 if (err)
5761 goto done;
5763 err = got_worktree_set_head_ref(worktree, rebased_branch);
5764 if (err)
5765 goto done;
5767 err = delete_rebase_refs(worktree, repo);
5768 done:
5769 if (fileindex)
5770 got_fileindex_free(fileindex);
5771 free(new_head_commit_id);
5772 unlockerr = lock_worktree(worktree, LOCK_SH);
5773 if (unlockerr && err == NULL)
5774 err = unlockerr;
5775 return err;
5778 const struct got_error *
5779 got_worktree_rebase_abort(struct got_worktree *worktree,
5780 struct got_fileindex *fileindex, struct got_repository *repo,
5781 struct got_reference *new_base_branch,
5782 got_worktree_checkout_cb progress_cb, void *progress_arg)
5784 const struct got_error *err, *unlockerr, *sync_err;
5785 struct got_reference *resolved = NULL;
5786 struct got_object_id *commit_id = NULL;
5787 char *fileindex_path = NULL;
5788 struct revert_file_args rfa;
5789 struct got_object_id *tree_id = NULL;
5791 err = lock_worktree(worktree, LOCK_EX);
5792 if (err)
5793 return err;
5795 err = got_ref_open(&resolved, repo,
5796 got_ref_get_symref_target(new_base_branch), 0);
5797 if (err)
5798 goto done;
5800 err = got_worktree_set_head_ref(worktree, resolved);
5801 if (err)
5802 goto done;
5805 * XXX commits to the base branch could have happened while
5806 * we were busy rebasing; should we store the original commit ID
5807 * when rebase begins and read it back here?
5809 err = got_ref_resolve(&commit_id, repo, resolved);
5810 if (err)
5811 goto done;
5813 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5814 if (err)
5815 goto done;
5817 err = got_object_id_by_path(&tree_id, repo,
5818 worktree->base_commit_id, worktree->path_prefix);
5819 if (err)
5820 goto done;
5822 err = delete_rebase_refs(worktree, repo);
5823 if (err)
5824 goto done;
5826 err = get_fileindex_path(&fileindex_path, worktree);
5827 if (err)
5828 goto done;
5830 rfa.worktree = worktree;
5831 rfa.fileindex = fileindex;
5832 rfa.progress_cb = progress_cb;
5833 rfa.progress_arg = progress_arg;
5834 rfa.patch_cb = NULL;
5835 rfa.patch_arg = NULL;
5836 rfa.repo = repo;
5837 err = worktree_status(worktree, "", fileindex, repo,
5838 revert_file, &rfa, NULL, NULL, 0, 0);
5839 if (err)
5840 goto sync;
5842 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5843 repo, progress_cb, progress_arg, NULL, NULL);
5844 sync:
5845 sync_err = sync_fileindex(fileindex, fileindex_path);
5846 if (sync_err && err == NULL)
5847 err = sync_err;
5848 done:
5849 got_ref_close(resolved);
5850 free(tree_id);
5851 free(commit_id);
5852 if (fileindex)
5853 got_fileindex_free(fileindex);
5854 free(fileindex_path);
5856 unlockerr = lock_worktree(worktree, LOCK_SH);
5857 if (unlockerr && err == NULL)
5858 err = unlockerr;
5859 return err;
5862 const struct got_error *
5863 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5864 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5865 struct got_fileindex **fileindex, struct got_worktree *worktree,
5866 struct got_repository *repo)
5868 const struct got_error *err = NULL;
5869 char *tmp_branch_name = NULL;
5870 char *branch_ref_name = NULL;
5871 char *base_commit_ref_name = NULL;
5872 char *fileindex_path = NULL;
5873 struct check_rebase_ok_arg ok_arg;
5874 struct got_reference *wt_branch = NULL;
5875 struct got_reference *base_commit_ref = NULL;
5877 *tmp_branch = NULL;
5878 *branch_ref = NULL;
5879 *base_commit_id = NULL;
5880 *fileindex = NULL;
5882 err = lock_worktree(worktree, LOCK_EX);
5883 if (err)
5884 return err;
5886 err = open_fileindex(fileindex, &fileindex_path, worktree);
5887 if (err)
5888 goto done;
5890 ok_arg.worktree = worktree;
5891 ok_arg.repo = repo;
5892 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5893 &ok_arg);
5894 if (err)
5895 goto done;
5897 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5898 if (err)
5899 goto done;
5901 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5902 if (err)
5903 goto done;
5905 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5906 worktree);
5907 if (err)
5908 goto done;
5910 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5911 0);
5912 if (err)
5913 goto done;
5915 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5916 if (err)
5917 goto done;
5919 err = got_ref_write(*branch_ref, repo);
5920 if (err)
5921 goto done;
5923 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5924 worktree->base_commit_id);
5925 if (err)
5926 goto done;
5927 err = got_ref_write(base_commit_ref, repo);
5928 if (err)
5929 goto done;
5930 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5931 if (*base_commit_id == NULL) {
5932 err = got_error_from_errno("got_object_id_dup");
5933 goto done;
5936 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5937 worktree->base_commit_id);
5938 if (err)
5939 goto done;
5940 err = got_ref_write(*tmp_branch, repo);
5941 if (err)
5942 goto done;
5944 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5945 if (err)
5946 goto done;
5947 done:
5948 free(fileindex_path);
5949 free(tmp_branch_name);
5950 free(branch_ref_name);
5951 free(base_commit_ref_name);
5952 if (wt_branch)
5953 got_ref_close(wt_branch);
5954 if (err) {
5955 if (*branch_ref) {
5956 got_ref_close(*branch_ref);
5957 *branch_ref = NULL;
5959 if (*tmp_branch) {
5960 got_ref_close(*tmp_branch);
5961 *tmp_branch = NULL;
5963 free(*base_commit_id);
5964 if (*fileindex) {
5965 got_fileindex_free(*fileindex);
5966 *fileindex = NULL;
5968 lock_worktree(worktree, LOCK_SH);
5970 return err;
5973 const struct got_error *
5974 got_worktree_histedit_postpone(struct got_worktree *worktree,
5975 struct got_fileindex *fileindex)
5977 if (fileindex)
5978 got_fileindex_free(fileindex);
5979 return lock_worktree(worktree, LOCK_SH);
5982 const struct got_error *
5983 got_worktree_histedit_in_progress(int *in_progress,
5984 struct got_worktree *worktree)
5986 const struct got_error *err;
5987 char *tmp_branch_name = NULL;
5989 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5990 if (err)
5991 return err;
5993 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5994 free(tmp_branch_name);
5995 return NULL;
5998 const struct got_error *
5999 got_worktree_histedit_continue(struct got_object_id **commit_id,
6000 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6001 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6002 struct got_worktree *worktree, struct got_repository *repo)
6004 const struct got_error *err;
6005 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6006 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6007 struct got_reference *commit_ref = NULL;
6008 struct got_reference *base_commit_ref = NULL;
6009 char *fileindex_path = NULL;
6010 int have_staged_files = 0;
6012 *commit_id = NULL;
6013 *tmp_branch = NULL;
6014 *base_commit_id = NULL;
6015 *fileindex = NULL;
6017 err = lock_worktree(worktree, LOCK_EX);
6018 if (err)
6019 return err;
6021 err = open_fileindex(fileindex, &fileindex_path, worktree);
6022 if (err)
6023 goto done;
6025 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6026 &have_staged_files);
6027 if (err && err->code != GOT_ERR_CANCELLED)
6028 goto done;
6029 if (have_staged_files) {
6030 err = got_error(GOT_ERR_STAGED_PATHS);
6031 goto done;
6034 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6035 if (err)
6036 goto done;
6038 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6039 if (err)
6040 goto done;
6042 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6043 if (err)
6044 goto done;
6046 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6047 worktree);
6048 if (err)
6049 goto done;
6051 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6052 if (err)
6053 goto done;
6055 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6056 if (err)
6057 goto done;
6058 err = got_ref_resolve(commit_id, repo, commit_ref);
6059 if (err)
6060 goto done;
6062 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6063 if (err)
6064 goto done;
6065 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6066 if (err)
6067 goto done;
6069 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6070 if (err)
6071 goto done;
6072 done:
6073 free(commit_ref_name);
6074 free(branch_ref_name);
6075 free(fileindex_path);
6076 if (commit_ref)
6077 got_ref_close(commit_ref);
6078 if (base_commit_ref)
6079 got_ref_close(base_commit_ref);
6080 if (err) {
6081 free(*commit_id);
6082 *commit_id = NULL;
6083 free(*base_commit_id);
6084 *base_commit_id = NULL;
6085 if (*tmp_branch) {
6086 got_ref_close(*tmp_branch);
6087 *tmp_branch = NULL;
6089 if (*fileindex) {
6090 got_fileindex_free(*fileindex);
6091 *fileindex = NULL;
6093 lock_worktree(worktree, LOCK_EX);
6095 return err;
6098 static const struct got_error *
6099 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6101 const struct got_error *err;
6102 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6103 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6105 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6106 if (err)
6107 goto done;
6108 err = delete_ref(tmp_branch_name, repo);
6109 if (err)
6110 goto done;
6112 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6113 worktree);
6114 if (err)
6115 goto done;
6116 err = delete_ref(base_commit_ref_name, repo);
6117 if (err)
6118 goto done;
6120 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6121 if (err)
6122 goto done;
6123 err = delete_ref(branch_ref_name, repo);
6124 if (err)
6125 goto done;
6127 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6128 if (err)
6129 goto done;
6130 err = delete_ref(commit_ref_name, repo);
6131 if (err)
6132 goto done;
6133 done:
6134 free(tmp_branch_name);
6135 free(base_commit_ref_name);
6136 free(branch_ref_name);
6137 free(commit_ref_name);
6138 return err;
6141 const struct got_error *
6142 got_worktree_histedit_abort(struct got_worktree *worktree,
6143 struct got_fileindex *fileindex, struct got_repository *repo,
6144 struct got_reference *branch, struct got_object_id *base_commit_id,
6145 got_worktree_checkout_cb progress_cb, void *progress_arg)
6147 const struct got_error *err, *unlockerr, *sync_err;
6148 struct got_reference *resolved = NULL;
6149 char *fileindex_path = NULL;
6150 struct got_object_id *tree_id = NULL;
6151 struct revert_file_args rfa;
6153 err = lock_worktree(worktree, LOCK_EX);
6154 if (err)
6155 return err;
6157 err = got_ref_open(&resolved, repo,
6158 got_ref_get_symref_target(branch), 0);
6159 if (err)
6160 goto done;
6162 err = got_worktree_set_head_ref(worktree, resolved);
6163 if (err)
6164 goto done;
6166 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6167 if (err)
6168 goto done;
6170 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6171 worktree->path_prefix);
6172 if (err)
6173 goto done;
6175 err = delete_histedit_refs(worktree, repo);
6176 if (err)
6177 goto done;
6179 err = get_fileindex_path(&fileindex_path, worktree);
6180 if (err)
6181 goto done;
6183 rfa.worktree = worktree;
6184 rfa.fileindex = fileindex;
6185 rfa.progress_cb = progress_cb;
6186 rfa.progress_arg = progress_arg;
6187 rfa.patch_cb = NULL;
6188 rfa.patch_arg = NULL;
6189 rfa.repo = repo;
6190 err = worktree_status(worktree, "", fileindex, repo,
6191 revert_file, &rfa, NULL, NULL, 0, 0);
6192 if (err)
6193 goto sync;
6195 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6196 repo, progress_cb, progress_arg, NULL, NULL);
6197 sync:
6198 sync_err = sync_fileindex(fileindex, fileindex_path);
6199 if (sync_err && err == NULL)
6200 err = sync_err;
6201 done:
6202 got_ref_close(resolved);
6203 free(tree_id);
6204 free(fileindex_path);
6206 unlockerr = lock_worktree(worktree, LOCK_SH);
6207 if (unlockerr && err == NULL)
6208 err = unlockerr;
6209 return err;
6212 const struct got_error *
6213 got_worktree_histedit_complete(struct got_worktree *worktree,
6214 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6215 struct got_reference *edited_branch, struct got_repository *repo)
6217 const struct got_error *err, *unlockerr;
6218 struct got_object_id *new_head_commit_id = NULL;
6219 struct got_reference *resolved = NULL;
6221 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6222 if (err)
6223 return err;
6225 err = got_ref_open(&resolved, repo,
6226 got_ref_get_symref_target(edited_branch), 0);
6227 if (err)
6228 goto done;
6230 err = got_ref_change_ref(resolved, new_head_commit_id);
6231 if (err)
6232 goto done;
6234 err = got_ref_write(resolved, repo);
6235 if (err)
6236 goto done;
6238 err = got_worktree_set_head_ref(worktree, resolved);
6239 if (err)
6240 goto done;
6242 err = delete_histedit_refs(worktree, repo);
6243 done:
6244 if (fileindex)
6245 got_fileindex_free(fileindex);
6246 free(new_head_commit_id);
6247 unlockerr = lock_worktree(worktree, LOCK_SH);
6248 if (unlockerr && err == NULL)
6249 err = unlockerr;
6250 return err;
6253 const struct got_error *
6254 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6255 struct got_object_id *commit_id, struct got_repository *repo)
6257 const struct got_error *err;
6258 char *commit_ref_name;
6260 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6261 if (err)
6262 return err;
6264 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6265 if (err)
6266 goto done;
6268 err = delete_ref(commit_ref_name, repo);
6269 done:
6270 free(commit_ref_name);
6271 return err;
6274 const struct got_error *
6275 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6276 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6277 struct got_worktree *worktree, const char *refname,
6278 struct got_repository *repo)
6280 const struct got_error *err = NULL;
6281 char *fileindex_path = NULL;
6282 struct check_rebase_ok_arg ok_arg;
6284 *fileindex = NULL;
6285 *branch_ref = NULL;
6286 *base_branch_ref = NULL;
6288 err = lock_worktree(worktree, LOCK_EX);
6289 if (err)
6290 return err;
6292 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6293 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6294 "cannot integrate a branch into itself; "
6295 "update -b or different branch name required");
6296 goto done;
6299 err = open_fileindex(fileindex, &fileindex_path, worktree);
6300 if (err)
6301 goto done;
6303 /* Preconditions are the same as for rebase. */
6304 ok_arg.worktree = worktree;
6305 ok_arg.repo = repo;
6306 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6307 &ok_arg);
6308 if (err)
6309 goto done;
6311 err = got_ref_open(branch_ref, repo, refname, 1);
6312 if (err)
6313 goto done;
6315 err = got_ref_open(base_branch_ref, repo,
6316 got_worktree_get_head_ref_name(worktree), 1);
6317 done:
6318 if (err) {
6319 if (*branch_ref) {
6320 got_ref_close(*branch_ref);
6321 *branch_ref = NULL;
6323 if (*base_branch_ref) {
6324 got_ref_close(*base_branch_ref);
6325 *base_branch_ref = NULL;
6327 if (*fileindex) {
6328 got_fileindex_free(*fileindex);
6329 *fileindex = NULL;
6331 lock_worktree(worktree, LOCK_SH);
6333 return err;
6336 const struct got_error *
6337 got_worktree_integrate_continue(struct got_worktree *worktree,
6338 struct got_fileindex *fileindex, struct got_repository *repo,
6339 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6340 got_worktree_checkout_cb progress_cb, void *progress_arg,
6341 got_cancel_cb cancel_cb, void *cancel_arg)
6343 const struct got_error *err = NULL, *sync_err, *unlockerr;
6344 char *fileindex_path = NULL;
6345 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6347 err = get_fileindex_path(&fileindex_path, worktree);
6348 if (err)
6349 goto done;
6351 err = got_ref_resolve(&commit_id, repo, branch_ref);
6352 if (err)
6353 goto done;
6355 err = got_object_id_by_path(&tree_id, repo, commit_id,
6356 worktree->path_prefix);
6357 if (err)
6358 goto done;
6360 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6361 if (err)
6362 goto done;
6364 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6365 progress_cb, progress_arg, cancel_cb, cancel_arg);
6366 if (err)
6367 goto sync;
6369 err = got_ref_change_ref(base_branch_ref, commit_id);
6370 if (err)
6371 goto sync;
6373 err = got_ref_write(base_branch_ref, repo);
6374 sync:
6375 sync_err = sync_fileindex(fileindex, fileindex_path);
6376 if (sync_err && err == NULL)
6377 err = sync_err;
6379 done:
6380 unlockerr = got_ref_unlock(branch_ref);
6381 if (unlockerr && err == NULL)
6382 err = unlockerr;
6383 got_ref_close(branch_ref);
6385 unlockerr = got_ref_unlock(base_branch_ref);
6386 if (unlockerr && err == NULL)
6387 err = unlockerr;
6388 got_ref_close(base_branch_ref);
6390 got_fileindex_free(fileindex);
6391 free(fileindex_path);
6392 free(tree_id);
6394 unlockerr = lock_worktree(worktree, LOCK_SH);
6395 if (unlockerr && err == NULL)
6396 err = unlockerr;
6397 return err;
6400 const struct got_error *
6401 got_worktree_integrate_abort(struct got_worktree *worktree,
6402 struct got_fileindex *fileindex, struct got_repository *repo,
6403 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6405 const struct got_error *err = NULL, *unlockerr = NULL;
6407 got_fileindex_free(fileindex);
6409 err = lock_worktree(worktree, LOCK_SH);
6411 unlockerr = got_ref_unlock(branch_ref);
6412 if (unlockerr && err == NULL)
6413 err = unlockerr;
6414 got_ref_close(branch_ref);
6416 unlockerr = got_ref_unlock(base_branch_ref);
6417 if (unlockerr && err == NULL)
6418 err = unlockerr;
6419 got_ref_close(base_branch_ref);
6421 return err;
6424 struct check_stage_ok_arg {
6425 struct got_object_id *head_commit_id;
6426 struct got_worktree *worktree;
6427 struct got_fileindex *fileindex;
6428 struct got_repository *repo;
6429 int have_changes;
6432 const struct got_error *
6433 check_stage_ok(void *arg, unsigned char status,
6434 unsigned char staged_status, const char *relpath,
6435 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6436 struct got_object_id *commit_id, int dirfd, const char *de_name)
6438 struct check_stage_ok_arg *a = arg;
6439 const struct got_error *err = NULL;
6440 struct got_fileindex_entry *ie;
6441 struct got_object_id base_commit_id;
6442 struct got_object_id *base_commit_idp = NULL;
6443 char *in_repo_path = NULL, *p;
6445 if (status == GOT_STATUS_UNVERSIONED ||
6446 status == GOT_STATUS_NO_CHANGE)
6447 return NULL;
6448 if (status == GOT_STATUS_NONEXISTENT)
6449 return got_error_set_errno(ENOENT, relpath);
6451 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6452 if (ie == NULL)
6453 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6455 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6456 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6457 relpath) == -1)
6458 return got_error_from_errno("asprintf");
6460 if (got_fileindex_entry_has_commit(ie)) {
6461 memcpy(base_commit_id.sha1, ie->commit_sha1,
6462 SHA1_DIGEST_LENGTH);
6463 base_commit_idp = &base_commit_id;
6466 if (status == GOT_STATUS_CONFLICT) {
6467 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6468 goto done;
6469 } else if (status != GOT_STATUS_ADD &&
6470 status != GOT_STATUS_MODIFY &&
6471 status != GOT_STATUS_DELETE) {
6472 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6473 goto done;
6476 a->have_changes = 1;
6478 p = in_repo_path;
6479 while (p[0] == '/')
6480 p++;
6481 err = check_out_of_date(p, status, staged_status,
6482 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6483 GOT_ERR_STAGE_OUT_OF_DATE);
6484 done:
6485 free(in_repo_path);
6486 return err;
6489 struct stage_path_arg {
6490 struct got_worktree *worktree;
6491 struct got_fileindex *fileindex;
6492 struct got_repository *repo;
6493 got_worktree_status_cb status_cb;
6494 void *status_arg;
6495 got_worktree_patch_cb patch_cb;
6496 void *patch_arg;
6497 int staged_something;
6500 static const struct got_error *
6501 stage_path(void *arg, unsigned char status,
6502 unsigned char staged_status, const char *relpath,
6503 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6504 struct got_object_id *commit_id, int dirfd, const char *de_name)
6506 struct stage_path_arg *a = arg;
6507 const struct got_error *err = NULL;
6508 struct got_fileindex_entry *ie;
6509 char *ondisk_path = NULL, *path_content = NULL;
6510 uint32_t stage;
6511 struct got_object_id *new_staged_blob_id = NULL;
6513 if (status == GOT_STATUS_UNVERSIONED)
6514 return NULL;
6516 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6517 if (ie == NULL)
6518 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6520 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6521 relpath)== -1)
6522 return got_error_from_errno("asprintf");
6524 switch (status) {
6525 case GOT_STATUS_ADD:
6526 case GOT_STATUS_MODIFY:
6527 if (a->patch_cb) {
6528 if (status == GOT_STATUS_ADD) {
6529 int choice = GOT_PATCH_CHOICE_NONE;
6530 err = (*a->patch_cb)(&choice, a->patch_arg,
6531 status, ie->path, NULL, 1, 1);
6532 if (err)
6533 break;
6534 if (choice != GOT_PATCH_CHOICE_YES)
6535 break;
6536 } else {
6537 err = create_patched_content(&path_content, 0,
6538 staged_blob_id ? staged_blob_id : blob_id,
6539 ondisk_path, dirfd, de_name, ie->path,
6540 a->repo, a->patch_cb, a->patch_arg);
6541 if (err || path_content == NULL)
6542 break;
6545 err = got_object_blob_create(&new_staged_blob_id,
6546 path_content ? path_content : ondisk_path, a->repo);
6547 if (err)
6548 break;
6549 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6550 SHA1_DIGEST_LENGTH);
6551 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6552 stage = GOT_FILEIDX_STAGE_ADD;
6553 else
6554 stage = GOT_FILEIDX_STAGE_MODIFY;
6555 got_fileindex_entry_stage_set(ie, stage);
6556 a->staged_something = 1;
6557 if (a->status_cb == NULL)
6558 break;
6559 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6560 get_staged_status(ie), relpath, blob_id,
6561 new_staged_blob_id, NULL, dirfd, de_name);
6562 break;
6563 case GOT_STATUS_DELETE:
6564 if (staged_status == GOT_STATUS_DELETE)
6565 break;
6566 if (a->patch_cb) {
6567 int choice = GOT_PATCH_CHOICE_NONE;
6568 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6569 ie->path, NULL, 1, 1);
6570 if (err)
6571 break;
6572 if (choice == GOT_PATCH_CHOICE_NO)
6573 break;
6574 if (choice != GOT_PATCH_CHOICE_YES) {
6575 err = got_error(GOT_ERR_PATCH_CHOICE);
6576 break;
6579 stage = GOT_FILEIDX_STAGE_DELETE;
6580 got_fileindex_entry_stage_set(ie, stage);
6581 a->staged_something = 1;
6582 if (a->status_cb == NULL)
6583 break;
6584 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6585 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6586 de_name);
6587 break;
6588 case GOT_STATUS_NO_CHANGE:
6589 break;
6590 case GOT_STATUS_CONFLICT:
6591 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6592 break;
6593 case GOT_STATUS_NONEXISTENT:
6594 err = got_error_set_errno(ENOENT, relpath);
6595 break;
6596 default:
6597 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6598 break;
6601 if (path_content && unlink(path_content) == -1 && err == NULL)
6602 err = got_error_from_errno2("unlink", path_content);
6603 free(path_content);
6604 free(ondisk_path);
6605 free(new_staged_blob_id);
6606 return err;
6609 const struct got_error *
6610 got_worktree_stage(struct got_worktree *worktree,
6611 struct got_pathlist_head *paths,
6612 got_worktree_status_cb status_cb, void *status_arg,
6613 got_worktree_patch_cb patch_cb, void *patch_arg,
6614 struct got_repository *repo)
6616 const struct got_error *err = NULL, *sync_err, *unlockerr;
6617 struct got_pathlist_entry *pe;
6618 struct got_fileindex *fileindex = NULL;
6619 char *fileindex_path = NULL;
6620 struct got_reference *head_ref = NULL;
6621 struct got_object_id *head_commit_id = NULL;
6622 struct check_stage_ok_arg oka;
6623 struct stage_path_arg spa;
6625 err = lock_worktree(worktree, LOCK_EX);
6626 if (err)
6627 return err;
6629 err = got_ref_open(&head_ref, repo,
6630 got_worktree_get_head_ref_name(worktree), 0);
6631 if (err)
6632 goto done;
6633 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6634 if (err)
6635 goto done;
6636 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6637 if (err)
6638 goto done;
6640 /* Check pre-conditions before staging anything. */
6641 oka.head_commit_id = head_commit_id;
6642 oka.worktree = worktree;
6643 oka.fileindex = fileindex;
6644 oka.repo = repo;
6645 oka.have_changes = 0;
6646 TAILQ_FOREACH(pe, paths, entry) {
6647 err = worktree_status(worktree, pe->path, fileindex, repo,
6648 check_stage_ok, &oka, NULL, NULL, 0, 0);
6649 if (err)
6650 goto done;
6652 if (!oka.have_changes) {
6653 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6654 goto done;
6657 spa.worktree = worktree;
6658 spa.fileindex = fileindex;
6659 spa.repo = repo;
6660 spa.patch_cb = patch_cb;
6661 spa.patch_arg = patch_arg;
6662 spa.status_cb = status_cb;
6663 spa.status_arg = status_arg;
6664 spa.staged_something = 0;
6665 TAILQ_FOREACH(pe, paths, entry) {
6666 err = worktree_status(worktree, pe->path, fileindex, repo,
6667 stage_path, &spa, NULL, NULL, 0, 0);
6668 if (err)
6669 goto done;
6671 if (!spa.staged_something) {
6672 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6673 goto done;
6676 sync_err = sync_fileindex(fileindex, fileindex_path);
6677 if (sync_err && err == NULL)
6678 err = sync_err;
6679 done:
6680 if (head_ref)
6681 got_ref_close(head_ref);
6682 free(head_commit_id);
6683 free(fileindex_path);
6684 if (fileindex)
6685 got_fileindex_free(fileindex);
6686 unlockerr = lock_worktree(worktree, LOCK_SH);
6687 if (unlockerr && err == NULL)
6688 err = unlockerr;
6689 return err;
6692 struct unstage_path_arg {
6693 struct got_worktree *worktree;
6694 struct got_fileindex *fileindex;
6695 struct got_repository *repo;
6696 got_worktree_checkout_cb progress_cb;
6697 void *progress_arg;
6698 got_worktree_patch_cb patch_cb;
6699 void *patch_arg;
6702 static const struct got_error *
6703 create_unstaged_content(char **path_unstaged_content,
6704 char **path_new_staged_content, struct got_object_id *blob_id,
6705 struct got_object_id *staged_blob_id, const char *relpath,
6706 struct got_repository *repo,
6707 got_worktree_patch_cb patch_cb, void *patch_arg)
6709 const struct got_error *err;
6710 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6711 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6712 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6713 struct stat sb1, sb2;
6714 struct got_diff_changes *changes = NULL;
6715 struct got_diff_state *ds = NULL;
6716 struct got_diff_args *args = NULL;
6717 struct got_diff_change *change;
6718 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6719 int have_content = 0, have_rejected_content = 0;
6721 *path_unstaged_content = NULL;
6722 *path_new_staged_content = NULL;
6724 err = got_object_id_str(&label1, blob_id);
6725 if (err)
6726 return err;
6727 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6728 if (err)
6729 goto done;
6731 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6732 if (err)
6733 goto done;
6735 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6736 if (err)
6737 goto done;
6739 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6740 if (err)
6741 goto done;
6743 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6744 if (err)
6745 goto done;
6747 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6748 if (err)
6749 goto done;
6751 if (stat(path1, &sb1) == -1) {
6752 err = got_error_from_errno2("stat", path1);
6753 goto done;
6756 if (stat(path2, &sb2) == -1) {
6757 err = got_error_from_errno2("stat", path2);
6758 goto done;
6761 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6762 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6763 if (err)
6764 goto done;
6766 err = got_opentemp_named(path_unstaged_content, &outfile,
6767 "got-unstaged-content");
6768 if (err)
6769 goto done;
6770 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6771 "got-new-staged-content");
6772 if (err)
6773 goto done;
6775 if (fseek(f1, 0L, SEEK_SET) == -1) {
6776 err = got_ferror(f1, GOT_ERR_IO);
6777 goto done;
6779 if (fseek(f2, 0L, SEEK_SET) == -1) {
6780 err = got_ferror(f2, GOT_ERR_IO);
6781 goto done;
6783 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6784 int choice;
6785 err = apply_or_reject_change(&choice, change, ++n,
6786 changes->nchanges, ds, args, diff_flags, relpath,
6787 f1, f2, &line_cur1, &line_cur2,
6788 outfile, rejectfile, patch_cb, patch_arg);
6789 if (err)
6790 goto done;
6791 if (choice == GOT_PATCH_CHOICE_YES)
6792 have_content = 1;
6793 else
6794 have_rejected_content = 1;
6795 if (choice == GOT_PATCH_CHOICE_QUIT)
6796 break;
6798 if (have_content || have_rejected_content)
6799 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6800 outfile, rejectfile);
6801 done:
6802 free(label1);
6803 if (blob)
6804 got_object_blob_close(blob);
6805 if (staged_blob)
6806 got_object_blob_close(staged_blob);
6807 if (f1 && fclose(f1) == EOF && err == NULL)
6808 err = got_error_from_errno2("fclose", path1);
6809 if (f2 && fclose(f2) == EOF && err == NULL)
6810 err = got_error_from_errno2("fclose", path2);
6811 if (outfile && fclose(outfile) == EOF && err == NULL)
6812 err = got_error_from_errno2("fclose", *path_unstaged_content);
6813 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6814 err = got_error_from_errno2("fclose", *path_new_staged_content);
6815 if (path1 && unlink(path1) == -1 && err == NULL)
6816 err = got_error_from_errno2("unlink", path1);
6817 if (path2 && unlink(path2) == -1 && err == NULL)
6818 err = got_error_from_errno2("unlink", path2);
6819 if (err || !have_content) {
6820 if (*path_unstaged_content &&
6821 unlink(*path_unstaged_content) == -1 && err == NULL)
6822 err = got_error_from_errno2("unlink",
6823 *path_unstaged_content);
6824 free(*path_unstaged_content);
6825 *path_unstaged_content = NULL;
6827 if (err || !have_rejected_content) {
6828 if (*path_new_staged_content &&
6829 unlink(*path_new_staged_content) == -1 && err == NULL)
6830 err = got_error_from_errno2("unlink",
6831 *path_new_staged_content);
6832 free(*path_new_staged_content);
6833 *path_new_staged_content = NULL;
6835 free(args);
6836 if (ds) {
6837 got_diff_state_free(ds);
6838 free(ds);
6840 if (changes)
6841 got_diff_free_changes(changes);
6842 free(path1);
6843 free(path2);
6844 return err;
6847 static const struct got_error *
6848 unstage_path(void *arg, unsigned char status,
6849 unsigned char staged_status, const char *relpath,
6850 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6851 struct got_object_id *commit_id, int dirfd, const char *de_name)
6853 const struct got_error *err = NULL;
6854 struct unstage_path_arg *a = arg;
6855 struct got_fileindex_entry *ie;
6856 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6857 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6858 char *path_new_staged_content = NULL;
6859 char *id_str = NULL, *label_orig = NULL;
6860 int local_changes_subsumed;
6861 struct stat sb;
6863 if (staged_status != GOT_STATUS_ADD &&
6864 staged_status != GOT_STATUS_MODIFY &&
6865 staged_status != GOT_STATUS_DELETE)
6866 return NULL;
6868 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6869 if (ie == NULL)
6870 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6872 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6873 == -1)
6874 return got_error_from_errno("asprintf");
6876 err = got_object_id_str(&id_str,
6877 commit_id ? commit_id : a->worktree->base_commit_id);
6878 if (err)
6879 goto done;
6880 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6881 id_str) == -1) {
6882 err = got_error_from_errno("asprintf");
6883 goto done;
6886 switch (staged_status) {
6887 case GOT_STATUS_MODIFY:
6888 err = got_object_open_as_blob(&blob_base, a->repo,
6889 blob_id, 8192);
6890 if (err)
6891 break;
6892 /* fall through */
6893 case GOT_STATUS_ADD:
6894 if (a->patch_cb) {
6895 if (staged_status == GOT_STATUS_ADD) {
6896 int choice = GOT_PATCH_CHOICE_NONE;
6897 err = (*a->patch_cb)(&choice, a->patch_arg,
6898 staged_status, ie->path, NULL, 1, 1);
6899 if (err)
6900 break;
6901 if (choice != GOT_PATCH_CHOICE_YES)
6902 break;
6903 } else {
6904 err = create_unstaged_content(
6905 &path_unstaged_content,
6906 &path_new_staged_content, blob_id,
6907 staged_blob_id, ie->path, a->repo,
6908 a->patch_cb, a->patch_arg);
6909 if (err || path_unstaged_content == NULL)
6910 break;
6911 if (path_new_staged_content) {
6912 err = got_object_blob_create(
6913 &staged_blob_id,
6914 path_new_staged_content,
6915 a->repo);
6916 if (err)
6917 break;
6918 memcpy(ie->staged_blob_sha1,
6919 staged_blob_id->sha1,
6920 SHA1_DIGEST_LENGTH);
6922 err = merge_file(&local_changes_subsumed,
6923 a->worktree, blob_base, ondisk_path,
6924 relpath, got_fileindex_perms_to_st(ie),
6925 path_unstaged_content, label_orig,
6926 "unstaged", a->repo, a->progress_cb,
6927 a->progress_arg);
6928 if (err == NULL &&
6929 path_new_staged_content == NULL)
6930 got_fileindex_entry_stage_set(ie,
6931 GOT_FILEIDX_STAGE_NONE);
6932 break; /* Done with this file. */
6935 err = got_object_open_as_blob(&blob_staged, a->repo,
6936 staged_blob_id, 8192);
6937 if (err)
6938 break;
6939 err = merge_blob(&local_changes_subsumed, a->worktree,
6940 blob_base, ondisk_path, relpath,
6941 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6942 commit_id ? commit_id : a->worktree->base_commit_id,
6943 a->repo, a->progress_cb, a->progress_arg);
6944 if (err == NULL)
6945 got_fileindex_entry_stage_set(ie,
6946 GOT_FILEIDX_STAGE_NONE);
6947 break;
6948 case GOT_STATUS_DELETE:
6949 if (a->patch_cb) {
6950 int choice = GOT_PATCH_CHOICE_NONE;
6951 err = (*a->patch_cb)(&choice, a->patch_arg,
6952 staged_status, ie->path, NULL, 1, 1);
6953 if (err)
6954 break;
6955 if (choice == GOT_PATCH_CHOICE_NO)
6956 break;
6957 if (choice != GOT_PATCH_CHOICE_YES) {
6958 err = got_error(GOT_ERR_PATCH_CHOICE);
6959 break;
6962 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6963 err = get_file_status(&status, &sb, ie, ondisk_path,
6964 dirfd, de_name, a->repo);
6965 if (err)
6966 break;
6967 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6968 break;
6970 done:
6971 free(ondisk_path);
6972 if (path_unstaged_content &&
6973 unlink(path_unstaged_content) == -1 && err == NULL)
6974 err = got_error_from_errno2("unlink", path_unstaged_content);
6975 if (path_new_staged_content &&
6976 unlink(path_new_staged_content) == -1 && err == NULL)
6977 err = got_error_from_errno2("unlink", path_new_staged_content);
6978 free(path_unstaged_content);
6979 free(path_new_staged_content);
6980 if (blob_base)
6981 got_object_blob_close(blob_base);
6982 if (blob_staged)
6983 got_object_blob_close(blob_staged);
6984 free(id_str);
6985 free(label_orig);
6986 return err;
6989 const struct got_error *
6990 got_worktree_unstage(struct got_worktree *worktree,
6991 struct got_pathlist_head *paths,
6992 got_worktree_checkout_cb progress_cb, void *progress_arg,
6993 got_worktree_patch_cb patch_cb, void *patch_arg,
6994 struct got_repository *repo)
6996 const struct got_error *err = NULL, *sync_err, *unlockerr;
6997 struct got_pathlist_entry *pe;
6998 struct got_fileindex *fileindex = NULL;
6999 char *fileindex_path = NULL;
7000 struct unstage_path_arg upa;
7002 err = lock_worktree(worktree, LOCK_EX);
7003 if (err)
7004 return err;
7006 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7007 if (err)
7008 goto done;
7010 upa.worktree = worktree;
7011 upa.fileindex = fileindex;
7012 upa.repo = repo;
7013 upa.progress_cb = progress_cb;
7014 upa.progress_arg = progress_arg;
7015 upa.patch_cb = patch_cb;
7016 upa.patch_arg = patch_arg;
7017 TAILQ_FOREACH(pe, paths, entry) {
7018 err = worktree_status(worktree, pe->path, fileindex, repo,
7019 unstage_path, &upa, NULL, NULL, 0, 0);
7020 if (err)
7021 goto done;
7024 sync_err = sync_fileindex(fileindex, fileindex_path);
7025 if (sync_err && err == NULL)
7026 err = sync_err;
7027 done:
7028 free(fileindex_path);
7029 if (fileindex)
7030 got_fileindex_free(fileindex);
7031 unlockerr = lock_worktree(worktree, LOCK_SH);
7032 if (unlockerr && err == NULL)
7033 err = unlockerr;
7034 return err;