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 = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
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 char *path_got = NULL;
958 const uint8_t *buf = got_object_blob_get_read_buf(blob);
959 size_t hdrlen = got_object_blob_get_hdrlen(blob);
961 /*
962 * Blob object content specifies the target path of the link.
963 * If a symbolic link cannot be installed we instead create
964 * a regular file which contains the link target path stored
965 * in the blob object.
966 */
967 do {
968 err = got_object_blob_read_block(&len, blob);
969 if (len + target_len >= sizeof(target_path)) {
970 /* Path too long; install as a regular file. */
971 got_object_blob_rewind(blob);
972 return install_blob(worktree, ondisk_path, path,
973 GOT_DEFAULT_FILE_MODE, st_mode, blob,
974 restoring_missing_file, reverting_versioned_file,
975 repo, progress_cb, progress_arg);
977 if (len > 0) {
978 /* Skip blob object header first time around. */
979 memcpy(target_path + target_len, buf + hdrlen,
980 len - hdrlen);
981 target_len += len - hdrlen;
982 hdrlen = 0;
984 } while (len != 0);
985 target_path[target_len] = '\0';
987 /*
988 * Relative symlink target lookup should begin at the directory
989 * in which the blob object is being installed.
990 */
991 if (!got_path_is_absolute(target_path)) {
992 char *parent = dirname(ondisk_path);
993 if (parent == NULL) {
994 err = got_error_from_errno2("dirname", ondisk_path);
995 goto done;
997 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
998 err = got_error_from_errno("asprintf");
999 goto done;
1004 * unveil(2) restricts our view of paths in the filesystem.
1005 * ENOENT will occur if a link target path does not exist or
1006 * if it points outside our unveiled path space.
1008 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1009 if (resolved_path == NULL) {
1010 if (errno != ENOENT) {
1011 err = got_error_from_errno2("realpath", target_path);
1012 goto done;
1016 /* Only allow symlinks pointing at paths within the work tree. */
1017 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1018 abspath : target_path), worktree->root_path,
1019 strlen(worktree->root_path))) {
1020 /* install as a regular file */
1021 got_object_blob_rewind(blob);
1022 err = install_blob(worktree, ondisk_path, path,
1023 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1024 restoring_missing_file, reverting_versioned_file,
1025 repo, progress_cb, progress_arg);
1026 goto done;
1029 /* Do not allow symlinks pointing into the .got directory. */
1030 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1031 GOT_WORKTREE_GOT_DIR) == -1) {
1032 err = got_error_from_errno("asprintf");
1033 goto done;
1035 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1036 abspath : target_path), path_got, strlen(path_got))) {
1037 /* install as a regular file */
1038 got_object_blob_rewind(blob);
1039 err = install_blob(worktree, ondisk_path, path,
1040 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1041 restoring_missing_file, reverting_versioned_file,
1042 repo, progress_cb, progress_arg);
1043 goto done;
1046 if (symlink(target_path, ondisk_path) == -1) {
1047 if (errno == EEXIST) {
1048 struct stat sb;
1049 ssize_t elen;
1050 char etarget[PATH_MAX];
1051 if (lstat(ondisk_path, &sb) == -1) {
1052 err = got_error_from_errno2("lstat",
1053 ondisk_path);
1054 goto done;
1056 if (!S_ISLNK(sb.st_mode)) {
1057 err = got_error_path(ondisk_path,
1058 GOT_ERR_FILE_OBSTRUCTED);
1059 goto done;
1061 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1062 if (elen == -1) {
1063 err = got_error_from_errno2("readlink",
1064 ondisk_path);
1065 goto done;
1067 if (elen == target_len &&
1068 memcmp(etarget, target_path, target_len) == 0) {
1069 err = NULL; /* nothing to do */
1070 goto done;
1071 } else {
1072 if (unlink(ondisk_path) == -1) {
1073 err = got_error_from_errno2("unlink",
1074 ondisk_path);
1075 goto done;
1077 if (symlink(target_path, ondisk_path) == -1) {
1078 err = got_error_from_errno3("symlink",
1079 target_path, ondisk_path);
1080 goto done;
1083 err = (*progress_cb)(progress_arg,
1084 GOT_STATUS_UPDATE, path);
1085 goto done;
1089 if (errno == ENOENT) {
1090 char *parent = dirname(ondisk_path);
1091 if (parent == NULL) {
1092 err = got_error_from_errno2("dirname",
1093 ondisk_path);
1094 goto done;
1096 err = add_dir_on_disk(worktree, parent);
1097 if (err)
1098 goto done;
1100 * Retry, and fall through to error handling
1101 * below if this second attempt fails.
1103 if (symlink(target_path, ondisk_path) != -1) {
1104 err = NULL; /* success */
1105 goto done;
1109 /* Handle errors from first or second creation attempt. */
1110 if (errno == ENAMETOOLONG) {
1111 /* bad target path; install as a regular file */
1112 got_object_blob_rewind(blob);
1113 err = install_blob(worktree, ondisk_path, path,
1114 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1115 restoring_missing_file, reverting_versioned_file,
1116 repo, progress_cb, progress_arg);
1117 } else if (errno == ENOTDIR) {
1118 err = got_error_path(ondisk_path,
1119 GOT_ERR_FILE_OBSTRUCTED);
1120 } else {
1121 err = got_error_from_errno3("symlink",
1122 target_path, ondisk_path);
1124 } else
1125 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1126 done:
1127 free(resolved_path);
1128 free(abspath);
1129 free(path_got);
1130 return err;
1133 static const struct got_error *
1134 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1135 const char *path, mode_t te_mode, mode_t st_mode,
1136 struct got_blob_object *blob, int restoring_missing_file,
1137 int reverting_versioned_file, struct got_repository *repo,
1138 got_worktree_checkout_cb progress_cb, void *progress_arg)
1140 const struct got_error *err = NULL;
1141 int fd = -1;
1142 size_t len, hdrlen;
1143 int update = 0;
1144 char *tmppath = NULL;
1146 if (S_ISLNK(te_mode))
1147 return install_symlink(worktree, ondisk_path, path, te_mode,
1148 st_mode, blob, restoring_missing_file,
1149 reverting_versioned_file, repo, progress_cb, progress_arg);
1151 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1152 GOT_DEFAULT_FILE_MODE);
1153 if (fd == -1) {
1154 if (errno == ENOENT) {
1155 char *parent = dirname(path);
1156 if (parent == NULL)
1157 return got_error_from_errno2("dirname", path);
1158 err = add_dir_on_disk(worktree, parent);
1159 if (err)
1160 return err;
1161 fd = open(ondisk_path,
1162 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1163 GOT_DEFAULT_FILE_MODE);
1164 if (fd == -1)
1165 return got_error_from_errno2("open",
1166 ondisk_path);
1167 } else if (errno == EEXIST) {
1168 if (!S_ISREG(st_mode)) {
1169 /* TODO file is obstructed; do something */
1170 err = got_error_path(ondisk_path,
1171 GOT_ERR_FILE_OBSTRUCTED);
1172 goto done;
1173 } else {
1174 err = got_opentemp_named_fd(&tmppath, &fd,
1175 ondisk_path);
1176 if (err)
1177 goto done;
1178 update = 1;
1180 } else
1181 return got_error_from_errno2("open", ondisk_path);
1184 if (restoring_missing_file)
1185 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1186 else if (reverting_versioned_file)
1187 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1188 else
1189 err = (*progress_cb)(progress_arg,
1190 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1191 if (err)
1192 goto done;
1194 hdrlen = got_object_blob_get_hdrlen(blob);
1195 do {
1196 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1197 err = got_object_blob_read_block(&len, blob);
1198 if (err)
1199 break;
1200 if (len > 0) {
1201 /* Skip blob object header first time around. */
1202 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1203 if (outlen == -1) {
1204 err = got_error_from_errno("write");
1205 goto done;
1206 } else if (outlen != len - hdrlen) {
1207 err = got_error(GOT_ERR_IO);
1208 goto done;
1210 hdrlen = 0;
1212 } while (len != 0);
1214 if (fsync(fd) != 0) {
1215 err = got_error_from_errno("fsync");
1216 goto done;
1219 if (update) {
1220 if (rename(tmppath, ondisk_path) != 0) {
1221 err = got_error_from_errno3("rename", tmppath,
1222 ondisk_path);
1223 unlink(tmppath);
1224 goto done;
1228 if (chmod(ondisk_path,
1229 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1230 err = got_error_from_errno2("chmod", ondisk_path);
1231 goto done;
1234 done:
1235 if (fd != -1 && close(fd) != 0 && err == NULL)
1236 err = got_error_from_errno("close");
1237 free(tmppath);
1238 return err;
1241 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1242 static const struct got_error *
1243 get_modified_file_content_status(unsigned char *status, FILE *f)
1245 const struct got_error *err = NULL;
1246 const char *markers[3] = {
1247 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1248 GOT_DIFF_CONFLICT_MARKER_SEP,
1249 GOT_DIFF_CONFLICT_MARKER_END
1251 int i = 0;
1252 char *line;
1253 size_t len;
1254 const char delim[3] = {'\0', '\0', '\0'};
1256 while (*status == GOT_STATUS_MODIFY) {
1257 line = fparseln(f, &len, NULL, delim, 0);
1258 if (line == NULL) {
1259 if (feof(f))
1260 break;
1261 err = got_ferror(f, GOT_ERR_IO);
1262 break;
1265 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1266 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1267 == 0)
1268 *status = GOT_STATUS_CONFLICT;
1269 else
1270 i++;
1274 return err;
1277 static int
1278 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1280 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1281 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1284 static int
1285 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1287 return !(ie->ctime_sec == sb->st_ctime &&
1288 ie->ctime_nsec == sb->st_ctimensec &&
1289 ie->mtime_sec == sb->st_mtime &&
1290 ie->mtime_nsec == sb->st_mtimensec &&
1291 ie->size == (sb->st_size & 0xffffffff) &&
1292 !xbit_differs(ie, sb->st_mode));
1295 static unsigned char
1296 get_staged_status(struct got_fileindex_entry *ie)
1298 switch (got_fileindex_entry_stage_get(ie)) {
1299 case GOT_FILEIDX_STAGE_ADD:
1300 return GOT_STATUS_ADD;
1301 case GOT_FILEIDX_STAGE_DELETE:
1302 return GOT_STATUS_DELETE;
1303 case GOT_FILEIDX_STAGE_MODIFY:
1304 return GOT_STATUS_MODIFY;
1305 default:
1306 return GOT_STATUS_NO_CHANGE;
1310 static const struct got_error *
1311 get_symlink_status(unsigned char *status, struct stat *sb,
1312 struct got_fileindex_entry *ie, const char *abspath,
1313 int dirfd, const char *de_name, struct got_blob_object *blob)
1315 const struct got_error *err = NULL;
1316 char target_path[PATH_MAX];
1317 char etarget[PATH_MAX];
1318 ssize_t elen;
1319 size_t len, target_len = 0;
1320 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1321 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1323 *status = GOT_STATUS_NO_CHANGE;
1325 /* Blob object content specifies the target path of the link. */
1326 do {
1327 err = got_object_blob_read_block(&len, blob);
1328 if (err)
1329 return err;
1330 if (len + target_len >= sizeof(target_path)) {
1332 * Should not happen. The blob contents were OK
1333 * when this symlink was installed.
1335 return got_error(GOT_ERR_NO_SPACE);
1337 if (len > 0) {
1338 /* Skip blob object header first time around. */
1339 memcpy(target_path + target_len, buf + hdrlen,
1340 len - hdrlen);
1341 target_len += len - hdrlen;
1342 hdrlen = 0;
1344 } while (len != 0);
1345 target_path[target_len] = '\0';
1347 if (dirfd != -1) {
1348 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1349 if (elen == -1)
1350 return got_error_from_errno2("readlinkat", abspath);
1351 } else {
1352 elen = readlink(abspath, etarget, sizeof(etarget));
1353 if (elen == -1)
1354 return got_error_from_errno2("readlinkat", abspath);
1357 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1358 *status = GOT_STATUS_MODIFY;
1360 return NULL;
1363 static const struct got_error *
1364 get_file_status(unsigned char *status, struct stat *sb,
1365 struct got_fileindex_entry *ie, const char *abspath,
1366 int dirfd, const char *de_name, struct got_repository *repo)
1368 const struct got_error *err = NULL;
1369 struct got_object_id id;
1370 size_t hdrlen;
1371 int fd = -1;
1372 FILE *f = NULL;
1373 uint8_t fbuf[8192];
1374 struct got_blob_object *blob = NULL;
1375 size_t flen, blen;
1376 unsigned char staged_status = get_staged_status(ie);
1378 *status = GOT_STATUS_NO_CHANGE;
1381 * Whenever the caller provides a directory descriptor and a
1382 * directory entry name for the file, use them! This prevents
1383 * race conditions if filesystem paths change beneath our feet.
1385 if (dirfd != -1) {
1386 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1387 if (errno == ENOENT) {
1388 if (got_fileindex_entry_has_file_on_disk(ie))
1389 *status = GOT_STATUS_MISSING;
1390 else
1391 *status = GOT_STATUS_DELETE;
1392 goto done;
1394 err = got_error_from_errno2("fstatat", abspath);
1395 goto done;
1397 } else {
1398 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1399 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1400 return got_error_from_errno2("open", abspath);
1401 else if (fd == -1 && errno == ELOOP) {
1402 if (lstat(abspath, sb) == -1)
1403 return got_error_from_errno2("lstat", abspath);
1404 } else if (fd == -1 || fstat(fd, sb) == -1) {
1405 if (errno == ENOENT) {
1406 if (got_fileindex_entry_has_file_on_disk(ie))
1407 *status = GOT_STATUS_MISSING;
1408 else
1409 *status = GOT_STATUS_DELETE;
1410 goto done;
1412 err = got_error_from_errno2("fstat", abspath);
1413 goto done;
1417 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1418 *status = GOT_STATUS_OBSTRUCTED;
1419 goto done;
1422 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1423 *status = GOT_STATUS_DELETE;
1424 goto done;
1425 } else if (!got_fileindex_entry_has_blob(ie) &&
1426 staged_status != GOT_STATUS_ADD) {
1427 *status = GOT_STATUS_ADD;
1428 goto done;
1431 if (!stat_info_differs(ie, sb))
1432 goto done;
1434 if (staged_status == GOT_STATUS_MODIFY ||
1435 staged_status == GOT_STATUS_ADD)
1436 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1437 else
1438 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1440 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1441 if (err)
1442 goto done;
1444 if (S_ISLNK(sb->st_mode)) {
1445 /* Staging changes to symlinks is not yet(?) supported. */
1446 if (staged_status != GOT_STATUS_NO_CHANGE) {
1447 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1448 goto done;
1450 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1451 de_name, blob);
1452 goto done;
1456 if (dirfd != -1) {
1457 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1458 if (fd == -1) {
1459 err = got_error_from_errno2("openat", abspath);
1460 goto done;
1464 f = fdopen(fd, "r");
1465 if (f == NULL) {
1466 err = got_error_from_errno2("fdopen", abspath);
1467 goto done;
1469 fd = -1;
1470 hdrlen = got_object_blob_get_hdrlen(blob);
1471 for (;;) {
1472 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1473 err = got_object_blob_read_block(&blen, blob);
1474 if (err)
1475 goto done;
1476 /* Skip length of blob object header first time around. */
1477 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1478 if (flen == 0 && ferror(f)) {
1479 err = got_error_from_errno("fread");
1480 goto done;
1482 if (blen == 0) {
1483 if (flen != 0)
1484 *status = GOT_STATUS_MODIFY;
1485 break;
1486 } else if (flen == 0) {
1487 if (blen != 0)
1488 *status = GOT_STATUS_MODIFY;
1489 break;
1490 } else if (blen - hdrlen == flen) {
1491 /* Skip blob object header first time around. */
1492 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1493 *status = GOT_STATUS_MODIFY;
1494 break;
1496 } else {
1497 *status = GOT_STATUS_MODIFY;
1498 break;
1500 hdrlen = 0;
1503 if (*status == GOT_STATUS_MODIFY) {
1504 rewind(f);
1505 err = get_modified_file_content_status(status, f);
1506 } else if (xbit_differs(ie, sb->st_mode))
1507 *status = GOT_STATUS_MODE_CHANGE;
1508 done:
1509 if (blob)
1510 got_object_blob_close(blob);
1511 if (f != NULL && fclose(f) == EOF && err == NULL)
1512 err = got_error_from_errno2("fclose", abspath);
1513 if (fd != -1 && close(fd) == -1 && err == NULL)
1514 err = got_error_from_errno2("close", abspath);
1515 return err;
1519 * Update timestamps in the file index if a file is unmodified and
1520 * we had to run a full content comparison to find out.
1522 static const struct got_error *
1523 sync_timestamps(char *ondisk_path, unsigned char status,
1524 struct got_fileindex_entry *ie, struct stat *sb)
1526 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1527 return got_fileindex_entry_update(ie, ondisk_path,
1528 ie->blob_sha1, ie->commit_sha1, 1);
1530 return NULL;
1533 static const struct got_error *
1534 update_blob(struct got_worktree *worktree,
1535 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1536 struct got_tree_entry *te, const char *path,
1537 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1538 void *progress_arg)
1540 const struct got_error *err = NULL;
1541 struct got_blob_object *blob = NULL;
1542 char *ondisk_path;
1543 unsigned char status = GOT_STATUS_NO_CHANGE;
1544 struct stat sb;
1546 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1547 return got_error_from_errno("asprintf");
1549 if (ie) {
1550 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1551 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1552 goto done;
1554 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1555 repo);
1556 if (err)
1557 goto done;
1558 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1559 sb.st_mode = got_fileindex_perms_to_st(ie);
1560 } else
1561 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1563 if (status == GOT_STATUS_OBSTRUCTED) {
1564 err = (*progress_cb)(progress_arg, status, path);
1565 goto done;
1567 if (status == GOT_STATUS_CONFLICT) {
1568 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1569 path);
1570 goto done;
1573 if (ie && status != GOT_STATUS_MISSING &&
1574 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1575 if (got_fileindex_entry_has_commit(ie) &&
1576 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1577 SHA1_DIGEST_LENGTH) == 0) {
1578 err = sync_timestamps(ondisk_path, status, ie, &sb);
1579 if (err)
1580 goto done;
1581 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1582 path);
1583 goto done;
1585 if (got_fileindex_entry_has_blob(ie) &&
1586 memcmp(ie->blob_sha1, te->id.sha1,
1587 SHA1_DIGEST_LENGTH) == 0) {
1588 err = sync_timestamps(ondisk_path, status, ie, &sb);
1589 goto done;
1593 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1594 if (err)
1595 goto done;
1597 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1598 int update_timestamps;
1599 struct got_blob_object *blob2 = NULL;
1600 char *label_orig = NULL;
1601 if (got_fileindex_entry_has_blob(ie)) {
1602 struct got_object_id id2;
1603 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1604 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1605 if (err)
1606 goto done;
1608 if (got_fileindex_entry_has_commit(ie)) {
1609 char id_str[SHA1_DIGEST_STRING_LENGTH];
1610 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1611 sizeof(id_str)) == NULL) {
1612 err = got_error_path(id_str,
1613 GOT_ERR_BAD_OBJ_ID_STR);
1614 goto done;
1616 if (asprintf(&label_orig, "%s: commit %s",
1617 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1618 err = got_error_from_errno("asprintf");
1619 goto done;
1622 err = merge_blob(&update_timestamps, worktree, blob2,
1623 ondisk_path, path, sb.st_mode, label_orig, blob,
1624 worktree->base_commit_id, repo,
1625 progress_cb, progress_arg);
1626 free(label_orig);
1627 if (blob2)
1628 got_object_blob_close(blob2);
1629 if (err)
1630 goto done;
1632 * Do not update timestamps of files with local changes.
1633 * Otherwise, a future status walk would treat them as
1634 * unmodified files again.
1636 err = got_fileindex_entry_update(ie, ondisk_path,
1637 blob->id.sha1, worktree->base_commit_id->sha1,
1638 update_timestamps);
1639 } else if (status == GOT_STATUS_MODE_CHANGE) {
1640 err = got_fileindex_entry_update(ie, ondisk_path,
1641 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1642 } else if (status == GOT_STATUS_DELETE) {
1643 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1644 if (err)
1645 goto done;
1646 err = got_fileindex_entry_update(ie, ondisk_path,
1647 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1648 if (err)
1649 goto done;
1650 } else {
1651 err = install_blob(worktree, ondisk_path, path, te->mode,
1652 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1653 repo, progress_cb, progress_arg);
1654 if (err)
1655 goto done;
1656 if (ie) {
1657 err = got_fileindex_entry_update(ie, ondisk_path,
1658 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1659 } else {
1660 err = create_fileindex_entry(fileindex,
1661 worktree->base_commit_id, ondisk_path, path,
1662 &blob->id);
1664 if (err)
1665 goto done;
1667 got_object_blob_close(blob);
1668 done:
1669 free(ondisk_path);
1670 return err;
1673 static const struct got_error *
1674 remove_ondisk_file(const char *root_path, const char *path)
1676 const struct got_error *err = NULL;
1677 char *ondisk_path = NULL;
1679 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1680 return got_error_from_errno("asprintf");
1682 if (unlink(ondisk_path) == -1) {
1683 if (errno != ENOENT)
1684 err = got_error_from_errno2("unlink", ondisk_path);
1685 } else {
1686 char *parent = dirname(ondisk_path);
1687 while (parent && strcmp(parent, root_path) != 0) {
1688 if (rmdir(parent) == -1) {
1689 if (errno != ENOTEMPTY)
1690 err = got_error_from_errno2("rmdir",
1691 parent);
1692 break;
1694 parent = dirname(parent);
1697 free(ondisk_path);
1698 return err;
1701 static const struct got_error *
1702 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1703 struct got_fileindex_entry *ie, struct got_repository *repo,
1704 got_worktree_checkout_cb progress_cb, void *progress_arg)
1706 const struct got_error *err = NULL;
1707 unsigned char status;
1708 struct stat sb;
1709 char *ondisk_path;
1711 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1712 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1714 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1715 == -1)
1716 return got_error_from_errno("asprintf");
1718 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1719 if (err)
1720 goto done;
1722 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1723 status == GOT_STATUS_ADD) {
1724 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1725 if (err)
1726 goto done;
1728 * Preserve the working file and change the deleted blob's
1729 * entry into a schedule-add entry.
1731 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1732 0);
1733 } else {
1734 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1735 if (err)
1736 goto done;
1737 if (status == GOT_STATUS_NO_CHANGE) {
1738 err = remove_ondisk_file(worktree->root_path, ie->path);
1739 if (err)
1740 goto done;
1742 got_fileindex_entry_remove(fileindex, ie);
1744 done:
1745 free(ondisk_path);
1746 return err;
1749 struct diff_cb_arg {
1750 struct got_fileindex *fileindex;
1751 struct got_worktree *worktree;
1752 struct got_repository *repo;
1753 got_worktree_checkout_cb progress_cb;
1754 void *progress_arg;
1755 got_cancel_cb cancel_cb;
1756 void *cancel_arg;
1759 static const struct got_error *
1760 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1761 struct got_tree_entry *te, const char *parent_path)
1763 struct diff_cb_arg *a = arg;
1765 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1766 return got_error(GOT_ERR_CANCELLED);
1768 return update_blob(a->worktree, a->fileindex, ie, te,
1769 ie->path, a->repo, a->progress_cb, a->progress_arg);
1772 static const struct got_error *
1773 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1775 struct diff_cb_arg *a = arg;
1777 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1778 return got_error(GOT_ERR_CANCELLED);
1780 return delete_blob(a->worktree, a->fileindex, ie,
1781 a->repo, a->progress_cb, a->progress_arg);
1784 static const struct got_error *
1785 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1787 struct diff_cb_arg *a = arg;
1788 const struct got_error *err;
1789 char *path;
1791 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1792 return got_error(GOT_ERR_CANCELLED);
1794 if (got_object_tree_entry_is_submodule(te))
1795 return NULL;
1797 if (asprintf(&path, "%s%s%s", parent_path,
1798 parent_path[0] ? "/" : "", te->name)
1799 == -1)
1800 return got_error_from_errno("asprintf");
1802 if (S_ISDIR(te->mode))
1803 err = add_dir_on_disk(a->worktree, path);
1804 else
1805 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1806 a->repo, a->progress_cb, a->progress_arg);
1808 free(path);
1809 return err;
1812 static const struct got_error *
1813 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1815 const struct got_error *err = NULL;
1816 char *uuidstr = NULL;
1817 uint32_t uuid_status;
1819 *refname = NULL;
1821 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1822 if (uuid_status != uuid_s_ok)
1823 return got_error_uuid(uuid_status, "uuid_to_string");
1825 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1826 == -1) {
1827 err = got_error_from_errno("asprintf");
1828 *refname = NULL;
1830 free(uuidstr);
1831 return err;
1834 const struct got_error *
1835 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1837 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1840 static const struct got_error *
1841 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1843 return get_ref_name(refname, worktree,
1844 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1847 static const struct got_error *
1848 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1850 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1853 static const struct got_error *
1854 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1856 return get_ref_name(refname, worktree,
1857 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1860 static const struct got_error *
1861 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1863 return get_ref_name(refname, worktree,
1864 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1867 static const struct got_error *
1868 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1870 return get_ref_name(refname, worktree,
1871 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1874 static const struct got_error *
1875 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1877 return get_ref_name(refname, worktree,
1878 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1881 static const struct got_error *
1882 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1884 return get_ref_name(refname, worktree,
1885 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1888 static const struct got_error *
1889 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1891 return get_ref_name(refname, worktree,
1892 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1895 const struct got_error *
1896 got_worktree_get_histedit_script_path(char **path,
1897 struct got_worktree *worktree)
1899 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1900 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1901 *path = NULL;
1902 return got_error_from_errno("asprintf");
1904 return NULL;
1908 * Prevent Git's garbage collector from deleting our base commit by
1909 * setting a reference to our base commit's ID.
1911 static const struct got_error *
1912 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1914 const struct got_error *err = NULL;
1915 struct got_reference *ref = NULL;
1916 char *refname;
1918 err = got_worktree_get_base_ref_name(&refname, worktree);
1919 if (err)
1920 return err;
1922 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1923 if (err)
1924 goto done;
1926 err = got_ref_write(ref, repo);
1927 done:
1928 free(refname);
1929 if (ref)
1930 got_ref_close(ref);
1931 return err;
1934 static const struct got_error *
1935 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1937 const struct got_error *err = NULL;
1939 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1940 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1941 err = got_error_from_errno("asprintf");
1942 *fileindex_path = NULL;
1944 return err;
1948 static const struct got_error *
1949 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1950 struct got_worktree *worktree)
1952 const struct got_error *err = NULL;
1953 FILE *index = NULL;
1955 *fileindex_path = NULL;
1956 *fileindex = got_fileindex_alloc();
1957 if (*fileindex == NULL)
1958 return got_error_from_errno("got_fileindex_alloc");
1960 err = get_fileindex_path(fileindex_path, worktree);
1961 if (err)
1962 goto done;
1964 index = fopen(*fileindex_path, "rb");
1965 if (index == NULL) {
1966 if (errno != ENOENT)
1967 err = got_error_from_errno2("fopen", *fileindex_path);
1968 } else {
1969 err = got_fileindex_read(*fileindex, index);
1970 if (fclose(index) != 0 && err == NULL)
1971 err = got_error_from_errno("fclose");
1973 done:
1974 if (err) {
1975 free(*fileindex_path);
1976 *fileindex_path = NULL;
1977 got_fileindex_free(*fileindex);
1978 *fileindex = NULL;
1980 return err;
1983 struct bump_base_commit_id_arg {
1984 struct got_object_id *base_commit_id;
1985 const char *path;
1986 size_t path_len;
1987 const char *entry_name;
1988 got_worktree_checkout_cb progress_cb;
1989 void *progress_arg;
1992 /* Bump base commit ID of all files within an updated part of the work tree. */
1993 static const struct got_error *
1994 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1996 const struct got_error *err;
1997 struct bump_base_commit_id_arg *a = arg;
1999 if (a->entry_name) {
2000 if (strcmp(ie->path, a->path) != 0)
2001 return NULL;
2002 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2003 return NULL;
2005 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2006 SHA1_DIGEST_LENGTH) == 0)
2007 return NULL;
2009 if (a->progress_cb) {
2010 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2011 ie->path);
2012 if (err)
2013 return err;
2015 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2016 return NULL;
2019 static const struct got_error *
2020 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2022 const struct got_error *err = NULL;
2023 char *new_fileindex_path = NULL;
2024 FILE *new_index = NULL;
2025 struct timespec timeout;
2027 err = got_opentemp_named(&new_fileindex_path, &new_index,
2028 fileindex_path);
2029 if (err)
2030 goto done;
2032 err = got_fileindex_write(fileindex, new_index);
2033 if (err)
2034 goto done;
2036 if (rename(new_fileindex_path, fileindex_path) != 0) {
2037 err = got_error_from_errno3("rename", new_fileindex_path,
2038 fileindex_path);
2039 unlink(new_fileindex_path);
2043 * Sleep for a short amount of time to ensure that files modified after
2044 * this program exits have a different time stamp from the one which
2045 * was recorded in the file index.
2047 timeout.tv_sec = 0;
2048 timeout.tv_nsec = 1;
2049 nanosleep(&timeout, NULL);
2050 done:
2051 if (new_index)
2052 fclose(new_index);
2053 free(new_fileindex_path);
2054 return err;
2057 static const struct got_error *
2058 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2059 struct got_object_id **tree_id, const char *wt_relpath,
2060 struct got_worktree *worktree, struct got_repository *repo)
2062 const struct got_error *err = NULL;
2063 struct got_object_id *id = NULL;
2064 char *in_repo_path = NULL;
2065 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2067 *entry_type = GOT_OBJ_TYPE_ANY;
2068 *tree_relpath = NULL;
2069 *tree_id = NULL;
2071 if (wt_relpath[0] == '\0') {
2072 /* Check out all files within the work tree. */
2073 *entry_type = GOT_OBJ_TYPE_TREE;
2074 *tree_relpath = strdup("");
2075 if (*tree_relpath == NULL) {
2076 err = got_error_from_errno("strdup");
2077 goto done;
2079 err = got_object_id_by_path(tree_id, repo,
2080 worktree->base_commit_id, worktree->path_prefix);
2081 if (err)
2082 goto done;
2083 return NULL;
2086 /* Check out a subset of files in the work tree. */
2088 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2089 is_root_wt ? "" : "/", wt_relpath) == -1) {
2090 err = got_error_from_errno("asprintf");
2091 goto done;
2094 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2095 in_repo_path);
2096 if (err)
2097 goto done;
2099 free(in_repo_path);
2100 in_repo_path = NULL;
2102 err = got_object_get_type(entry_type, repo, id);
2103 if (err)
2104 goto done;
2106 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2107 /* Check out a single file. */
2108 if (strchr(wt_relpath, '/') == NULL) {
2109 /* Check out a single file in work tree's root dir. */
2110 in_repo_path = strdup(worktree->path_prefix);
2111 if (in_repo_path == NULL) {
2112 err = got_error_from_errno("strdup");
2113 goto done;
2115 *tree_relpath = strdup("");
2116 if (*tree_relpath == NULL) {
2117 err = got_error_from_errno("strdup");
2118 goto done;
2120 } else {
2121 /* Check out a single file in a subdirectory. */
2122 err = got_path_dirname(tree_relpath, wt_relpath);
2123 if (err)
2124 return err;
2125 if (asprintf(&in_repo_path, "%s%s%s",
2126 worktree->path_prefix, is_root_wt ? "" : "/",
2127 *tree_relpath) == -1) {
2128 err = got_error_from_errno("asprintf");
2129 goto done;
2132 err = got_object_id_by_path(tree_id, repo,
2133 worktree->base_commit_id, in_repo_path);
2134 } else {
2135 /* Check out all files within a subdirectory. */
2136 *tree_id = got_object_id_dup(id);
2137 if (*tree_id == NULL) {
2138 err = got_error_from_errno("got_object_id_dup");
2139 goto done;
2141 *tree_relpath = strdup(wt_relpath);
2142 if (*tree_relpath == NULL) {
2143 err = got_error_from_errno("strdup");
2144 goto done;
2147 done:
2148 free(id);
2149 free(in_repo_path);
2150 if (err) {
2151 *entry_type = GOT_OBJ_TYPE_ANY;
2152 free(*tree_relpath);
2153 *tree_relpath = NULL;
2154 free(*tree_id);
2155 *tree_id = NULL;
2157 return err;
2160 static const struct got_error *
2161 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2162 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2163 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2164 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2166 const struct got_error *err = NULL;
2167 struct got_commit_object *commit = NULL;
2168 struct got_tree_object *tree = NULL;
2169 struct got_fileindex_diff_tree_cb diff_cb;
2170 struct diff_cb_arg arg;
2172 err = ref_base_commit(worktree, repo);
2173 if (err) {
2174 if (!(err->code == GOT_ERR_ERRNO &&
2175 (errno == EACCES || errno == EROFS)))
2176 goto done;
2177 err = (*progress_cb)(progress_arg,
2178 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2179 if (err)
2180 return err;
2183 err = got_object_open_as_commit(&commit, repo,
2184 worktree->base_commit_id);
2185 if (err)
2186 goto done;
2188 err = got_object_open_as_tree(&tree, repo, tree_id);
2189 if (err)
2190 goto done;
2192 if (entry_name &&
2193 got_object_tree_find_entry(tree, entry_name) == NULL) {
2194 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2195 goto done;
2198 diff_cb.diff_old_new = diff_old_new;
2199 diff_cb.diff_old = diff_old;
2200 diff_cb.diff_new = diff_new;
2201 arg.fileindex = fileindex;
2202 arg.worktree = worktree;
2203 arg.repo = repo;
2204 arg.progress_cb = progress_cb;
2205 arg.progress_arg = progress_arg;
2206 arg.cancel_cb = cancel_cb;
2207 arg.cancel_arg = cancel_arg;
2208 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2209 entry_name, repo, &diff_cb, &arg);
2210 done:
2211 if (tree)
2212 got_object_tree_close(tree);
2213 if (commit)
2214 got_object_commit_close(commit);
2215 return err;
2218 const struct got_error *
2219 got_worktree_checkout_files(struct got_worktree *worktree,
2220 struct got_pathlist_head *paths, struct got_repository *repo,
2221 got_worktree_checkout_cb progress_cb, void *progress_arg,
2222 got_cancel_cb cancel_cb, void *cancel_arg)
2224 const struct got_error *err = NULL, *sync_err, *unlockerr;
2225 struct got_commit_object *commit = NULL;
2226 struct got_tree_object *tree = NULL;
2227 struct got_fileindex *fileindex = NULL;
2228 char *fileindex_path = NULL;
2229 struct got_pathlist_entry *pe;
2230 struct tree_path_data {
2231 SIMPLEQ_ENTRY(tree_path_data) entry;
2232 struct got_object_id *tree_id;
2233 int entry_type;
2234 char *relpath;
2235 char *entry_name;
2236 } *tpd = NULL;
2237 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2239 SIMPLEQ_INIT(&tree_paths);
2241 err = lock_worktree(worktree, LOCK_EX);
2242 if (err)
2243 return err;
2245 /* Map all specified paths to in-repository trees. */
2246 TAILQ_FOREACH(pe, paths, entry) {
2247 tpd = malloc(sizeof(*tpd));
2248 if (tpd == NULL) {
2249 err = got_error_from_errno("malloc");
2250 goto done;
2253 err = find_tree_entry_for_checkout(&tpd->entry_type,
2254 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2255 if (err) {
2256 free(tpd);
2257 goto done;
2260 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2261 err = got_path_basename(&tpd->entry_name, pe->path);
2262 if (err) {
2263 free(tpd->relpath);
2264 free(tpd->tree_id);
2265 free(tpd);
2266 goto done;
2268 } else
2269 tpd->entry_name = NULL;
2271 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2275 * Read the file index.
2276 * Checking out files is supposed to be an idempotent operation.
2277 * If the on-disk file index is incomplete we will try to complete it.
2279 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2280 if (err)
2281 goto done;
2283 tpd = SIMPLEQ_FIRST(&tree_paths);
2284 TAILQ_FOREACH(pe, paths, entry) {
2285 struct bump_base_commit_id_arg bbc_arg;
2287 err = checkout_files(worktree, fileindex, tpd->relpath,
2288 tpd->tree_id, tpd->entry_name, repo,
2289 progress_cb, progress_arg, cancel_cb, cancel_arg);
2290 if (err)
2291 break;
2293 bbc_arg.base_commit_id = worktree->base_commit_id;
2294 bbc_arg.entry_name = tpd->entry_name;
2295 bbc_arg.path = pe->path;
2296 bbc_arg.path_len = pe->path_len;
2297 bbc_arg.progress_cb = progress_cb;
2298 bbc_arg.progress_arg = progress_arg;
2299 err = got_fileindex_for_each_entry_safe(fileindex,
2300 bump_base_commit_id, &bbc_arg);
2301 if (err)
2302 break;
2304 tpd = SIMPLEQ_NEXT(tpd, entry);
2306 sync_err = sync_fileindex(fileindex, fileindex_path);
2307 if (sync_err && err == NULL)
2308 err = sync_err;
2309 done:
2310 free(fileindex_path);
2311 if (tree)
2312 got_object_tree_close(tree);
2313 if (commit)
2314 got_object_commit_close(commit);
2315 if (fileindex)
2316 got_fileindex_free(fileindex);
2317 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2318 tpd = SIMPLEQ_FIRST(&tree_paths);
2319 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2320 free(tpd->relpath);
2321 free(tpd->tree_id);
2322 free(tpd);
2324 unlockerr = lock_worktree(worktree, LOCK_SH);
2325 if (unlockerr && err == NULL)
2326 err = unlockerr;
2327 return err;
2330 struct merge_file_cb_arg {
2331 struct got_worktree *worktree;
2332 struct got_fileindex *fileindex;
2333 got_worktree_checkout_cb progress_cb;
2334 void *progress_arg;
2335 got_cancel_cb cancel_cb;
2336 void *cancel_arg;
2337 const char *label_orig;
2338 struct got_object_id *commit_id2;
2341 static const struct got_error *
2342 merge_file_cb(void *arg, struct got_blob_object *blob1,
2343 struct got_blob_object *blob2, struct got_object_id *id1,
2344 struct got_object_id *id2, const char *path1, const char *path2,
2345 mode_t mode1, mode_t mode2, struct got_repository *repo)
2347 static const struct got_error *err = NULL;
2348 struct merge_file_cb_arg *a = arg;
2349 struct got_fileindex_entry *ie;
2350 char *ondisk_path = NULL;
2351 struct stat sb;
2352 unsigned char status;
2353 int local_changes_subsumed;
2355 if (blob1 && blob2) {
2356 ie = got_fileindex_entry_get(a->fileindex, path2,
2357 strlen(path2));
2358 if (ie == NULL)
2359 return (*a->progress_cb)(a->progress_arg,
2360 GOT_STATUS_MISSING, path2);
2362 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2363 path2) == -1)
2364 return got_error_from_errno("asprintf");
2366 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2367 repo);
2368 if (err)
2369 goto done;
2371 if (status == GOT_STATUS_DELETE) {
2372 err = (*a->progress_cb)(a->progress_arg,
2373 GOT_STATUS_MERGE, path2);
2374 goto done;
2376 if (status != GOT_STATUS_NO_CHANGE &&
2377 status != GOT_STATUS_MODIFY &&
2378 status != GOT_STATUS_CONFLICT &&
2379 status != GOT_STATUS_ADD) {
2380 err = (*a->progress_cb)(a->progress_arg, status, path2);
2381 goto done;
2384 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2385 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2386 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2387 } else if (blob1) {
2388 ie = got_fileindex_entry_get(a->fileindex, path1,
2389 strlen(path1));
2390 if (ie == NULL)
2391 return (*a->progress_cb)(a->progress_arg,
2392 GOT_STATUS_MISSING, path1);
2394 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2395 path1) == -1)
2396 return got_error_from_errno("asprintf");
2398 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2399 repo);
2400 if (err)
2401 goto done;
2403 switch (status) {
2404 case GOT_STATUS_NO_CHANGE:
2405 err = (*a->progress_cb)(a->progress_arg,
2406 GOT_STATUS_DELETE, path1);
2407 if (err)
2408 goto done;
2409 err = remove_ondisk_file(a->worktree->root_path, path1);
2410 if (err)
2411 goto done;
2412 if (ie)
2413 got_fileindex_entry_mark_deleted_from_disk(ie);
2414 break;
2415 case GOT_STATUS_DELETE:
2416 case GOT_STATUS_MISSING:
2417 err = (*a->progress_cb)(a->progress_arg,
2418 GOT_STATUS_DELETE, path1);
2419 if (err)
2420 goto done;
2421 if (ie)
2422 got_fileindex_entry_mark_deleted_from_disk(ie);
2423 break;
2424 case GOT_STATUS_ADD:
2425 case GOT_STATUS_MODIFY:
2426 case GOT_STATUS_CONFLICT:
2427 err = (*a->progress_cb)(a->progress_arg,
2428 GOT_STATUS_CANNOT_DELETE, path1);
2429 if (err)
2430 goto done;
2431 break;
2432 case GOT_STATUS_OBSTRUCTED:
2433 err = (*a->progress_cb)(a->progress_arg, status, path1);
2434 if (err)
2435 goto done;
2436 break;
2437 default:
2438 break;
2440 } else if (blob2) {
2441 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2442 path2) == -1)
2443 return got_error_from_errno("asprintf");
2444 ie = got_fileindex_entry_get(a->fileindex, path2,
2445 strlen(path2));
2446 if (ie) {
2447 err = get_file_status(&status, &sb, ie, ondisk_path,
2448 -1, NULL, repo);
2449 if (err)
2450 goto done;
2451 if (status != GOT_STATUS_NO_CHANGE &&
2452 status != GOT_STATUS_MODIFY &&
2453 status != GOT_STATUS_CONFLICT &&
2454 status != GOT_STATUS_ADD) {
2455 err = (*a->progress_cb)(a->progress_arg,
2456 status, path2);
2457 goto done;
2459 err = merge_blob(&local_changes_subsumed, a->worktree,
2460 NULL, ondisk_path, path2, sb.st_mode,
2461 a->label_orig, blob2, a->commit_id2, repo,
2462 a->progress_cb,
2463 a->progress_arg);
2464 if (status == GOT_STATUS_DELETE) {
2465 err = got_fileindex_entry_update(ie,
2466 ondisk_path, blob2->id.sha1,
2467 a->worktree->base_commit_id->sha1, 0);
2468 if (err)
2469 goto done;
2471 } else {
2472 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2473 err = install_blob(a->worktree, ondisk_path, path2,
2474 /* XXX get this from parent tree! */
2475 GOT_DEFAULT_FILE_MODE,
2476 sb.st_mode, blob2, 0, 0, repo,
2477 a->progress_cb, a->progress_arg);
2478 if (err)
2479 goto done;
2480 err = got_fileindex_entry_alloc(&ie, path2);
2481 if (err)
2482 goto done;
2483 err = got_fileindex_entry_update(ie, ondisk_path,
2484 NULL, NULL, 1);
2485 if (err) {
2486 got_fileindex_entry_free(ie);
2487 goto done;
2489 err = got_fileindex_entry_add(a->fileindex, ie);
2490 if (err) {
2491 got_fileindex_entry_free(ie);
2492 goto done;
2496 done:
2497 free(ondisk_path);
2498 return err;
2501 struct check_merge_ok_arg {
2502 struct got_worktree *worktree;
2503 struct got_repository *repo;
2506 static const struct got_error *
2507 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2509 const struct got_error *err = NULL;
2510 struct check_merge_ok_arg *a = arg;
2511 unsigned char status;
2512 struct stat sb;
2513 char *ondisk_path;
2515 /* Reject merges into a work tree with mixed base commits. */
2516 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2517 SHA1_DIGEST_LENGTH))
2518 return got_error(GOT_ERR_MIXED_COMMITS);
2520 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2521 == -1)
2522 return got_error_from_errno("asprintf");
2524 /* Reject merges into a work tree with conflicted files. */
2525 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2526 if (err)
2527 return err;
2528 if (status == GOT_STATUS_CONFLICT)
2529 return got_error(GOT_ERR_CONFLICTS);
2531 return NULL;
2534 static const struct got_error *
2535 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2536 const char *fileindex_path, struct got_object_id *commit_id1,
2537 struct got_object_id *commit_id2, struct got_repository *repo,
2538 got_worktree_checkout_cb progress_cb, void *progress_arg,
2539 got_cancel_cb cancel_cb, void *cancel_arg)
2541 const struct got_error *err = NULL, *sync_err;
2542 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2543 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2544 struct merge_file_cb_arg arg;
2545 char *label_orig = NULL;
2547 if (commit_id1) {
2548 char *id_str;
2550 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2551 worktree->path_prefix);
2552 if (err)
2553 goto done;
2555 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2556 if (err)
2557 goto done;
2559 err = got_object_id_str(&id_str, commit_id1);
2560 if (err)
2561 goto done;
2563 if (asprintf(&label_orig, "%s: commit %s",
2564 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2565 err = got_error_from_errno("asprintf");
2566 free(id_str);
2567 goto done;
2569 free(id_str);
2572 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2573 worktree->path_prefix);
2574 if (err)
2575 goto done;
2577 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2578 if (err)
2579 goto done;
2581 arg.worktree = worktree;
2582 arg.fileindex = fileindex;
2583 arg.progress_cb = progress_cb;
2584 arg.progress_arg = progress_arg;
2585 arg.cancel_cb = cancel_cb;
2586 arg.cancel_arg = cancel_arg;
2587 arg.label_orig = label_orig;
2588 arg.commit_id2 = commit_id2;
2589 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2590 sync_err = sync_fileindex(fileindex, fileindex_path);
2591 if (sync_err && err == NULL)
2592 err = sync_err;
2593 done:
2594 if (tree1)
2595 got_object_tree_close(tree1);
2596 if (tree2)
2597 got_object_tree_close(tree2);
2598 free(label_orig);
2599 return err;
2602 const struct got_error *
2603 got_worktree_merge_files(struct got_worktree *worktree,
2604 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2605 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2606 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2608 const struct got_error *err, *unlockerr;
2609 char *fileindex_path = NULL;
2610 struct got_fileindex *fileindex = NULL;
2611 struct check_merge_ok_arg mok_arg;
2613 err = lock_worktree(worktree, LOCK_EX);
2614 if (err)
2615 return err;
2617 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2618 if (err)
2619 goto done;
2621 mok_arg.worktree = worktree;
2622 mok_arg.repo = repo;
2623 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2624 &mok_arg);
2625 if (err)
2626 goto done;
2628 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2629 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2630 done:
2631 if (fileindex)
2632 got_fileindex_free(fileindex);
2633 free(fileindex_path);
2634 unlockerr = lock_worktree(worktree, LOCK_SH);
2635 if (unlockerr && err == NULL)
2636 err = unlockerr;
2637 return err;
2640 struct diff_dir_cb_arg {
2641 struct got_fileindex *fileindex;
2642 struct got_worktree *worktree;
2643 const char *status_path;
2644 size_t status_path_len;
2645 struct got_repository *repo;
2646 got_worktree_status_cb status_cb;
2647 void *status_arg;
2648 got_cancel_cb cancel_cb;
2649 void *cancel_arg;
2650 /* A pathlist containing per-directory pathlists of ignore patterns. */
2651 struct got_pathlist_head ignores;
2652 int report_unchanged;
2653 int no_ignores;
2656 static const struct got_error *
2657 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2658 int dirfd, const char *de_name,
2659 got_worktree_status_cb status_cb, void *status_arg,
2660 struct got_repository *repo, int report_unchanged)
2662 const struct got_error *err = NULL;
2663 unsigned char status = GOT_STATUS_NO_CHANGE;
2664 unsigned char staged_status = get_staged_status(ie);
2665 struct stat sb;
2666 struct got_object_id blob_id, commit_id, staged_blob_id;
2667 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2668 struct got_object_id *staged_blob_idp = NULL;
2670 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2671 if (err)
2672 return err;
2674 if (status == GOT_STATUS_NO_CHANGE &&
2675 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2676 return NULL;
2678 if (got_fileindex_entry_has_blob(ie)) {
2679 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2680 blob_idp = &blob_id;
2682 if (got_fileindex_entry_has_commit(ie)) {
2683 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2684 commit_idp = &commit_id;
2686 if (staged_status == GOT_STATUS_ADD ||
2687 staged_status == GOT_STATUS_MODIFY) {
2688 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2689 SHA1_DIGEST_LENGTH);
2690 staged_blob_idp = &staged_blob_id;
2693 return (*status_cb)(status_arg, status, staged_status,
2694 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2697 static const struct got_error *
2698 status_old_new(void *arg, struct got_fileindex_entry *ie,
2699 struct dirent *de, const char *parent_path, int dirfd)
2701 const struct got_error *err = NULL;
2702 struct diff_dir_cb_arg *a = arg;
2703 char *abspath;
2705 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2706 return got_error(GOT_ERR_CANCELLED);
2708 if (got_path_cmp(parent_path, a->status_path,
2709 strlen(parent_path), a->status_path_len) != 0 &&
2710 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2711 return NULL;
2713 if (parent_path[0]) {
2714 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2715 parent_path, de->d_name) == -1)
2716 return got_error_from_errno("asprintf");
2717 } else {
2718 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2719 de->d_name) == -1)
2720 return got_error_from_errno("asprintf");
2723 err = report_file_status(ie, abspath, dirfd, de->d_name,
2724 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2725 free(abspath);
2726 return err;
2729 static const struct got_error *
2730 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2732 struct diff_dir_cb_arg *a = arg;
2733 struct got_object_id blob_id, commit_id;
2734 unsigned char status;
2736 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2737 return got_error(GOT_ERR_CANCELLED);
2739 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2740 return NULL;
2742 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2743 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2744 if (got_fileindex_entry_has_file_on_disk(ie))
2745 status = GOT_STATUS_MISSING;
2746 else
2747 status = GOT_STATUS_DELETE;
2748 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2749 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2752 void
2753 free_ignorelist(struct got_pathlist_head *ignorelist)
2755 struct got_pathlist_entry *pe;
2757 TAILQ_FOREACH(pe, ignorelist, entry)
2758 free((char *)pe->path);
2759 got_pathlist_free(ignorelist);
2762 void
2763 free_ignores(struct got_pathlist_head *ignores)
2765 struct got_pathlist_entry *pe;
2767 TAILQ_FOREACH(pe, ignores, entry) {
2768 struct got_pathlist_head *ignorelist = pe->data;
2769 free_ignorelist(ignorelist);
2770 free((char *)pe->path);
2772 got_pathlist_free(ignores);
2775 static const struct got_error *
2776 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2778 const struct got_error *err = NULL;
2779 struct got_pathlist_entry *pe = NULL;
2780 struct got_pathlist_head *ignorelist;
2781 char *line = NULL, *pattern, *dirpath = NULL;
2782 size_t linesize = 0;
2783 ssize_t linelen;
2785 ignorelist = calloc(1, sizeof(*ignorelist));
2786 if (ignorelist == NULL)
2787 return got_error_from_errno("calloc");
2788 TAILQ_INIT(ignorelist);
2790 while ((linelen = getline(&line, &linesize, f)) != -1) {
2791 if (linelen > 0 && line[linelen - 1] == '\n')
2792 line[linelen - 1] = '\0';
2794 /* Git's ignores may contain comments. */
2795 if (line[0] == '#')
2796 continue;
2798 /* Git's negated patterns are not (yet?) supported. */
2799 if (line[0] == '!')
2800 continue;
2802 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2803 line) == -1) {
2804 err = got_error_from_errno("asprintf");
2805 goto done;
2807 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2808 if (err)
2809 goto done;
2811 if (ferror(f)) {
2812 err = got_error_from_errno("getline");
2813 goto done;
2816 dirpath = strdup(path);
2817 if (dirpath == NULL) {
2818 err = got_error_from_errno("strdup");
2819 goto done;
2821 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2822 done:
2823 free(line);
2824 if (err || pe == NULL) {
2825 free(dirpath);
2826 free_ignorelist(ignorelist);
2828 return err;
2831 int
2832 match_ignores(struct got_pathlist_head *ignores, const char *path)
2834 struct got_pathlist_entry *pe;
2836 /* Handle patterns which match in all directories. */
2837 TAILQ_FOREACH(pe, ignores, entry) {
2838 struct got_pathlist_head *ignorelist = pe->data;
2839 struct got_pathlist_entry *pi;
2841 TAILQ_FOREACH(pi, ignorelist, entry) {
2842 const char *p, *pattern = pi->path;
2844 if (strncmp(pattern, "**/", 3) != 0)
2845 continue;
2846 pattern += 3;
2847 p = path;
2848 while (*p) {
2849 if (fnmatch(pattern, p,
2850 FNM_PATHNAME | FNM_LEADING_DIR)) {
2851 /* Retry in next directory. */
2852 while (*p && *p != '/')
2853 p++;
2854 while (*p == '/')
2855 p++;
2856 continue;
2858 return 1;
2864 * The ignores pathlist contains ignore lists from children before
2865 * parents, so we can find the most specific ignorelist by walking
2866 * ignores backwards.
2868 pe = TAILQ_LAST(ignores, got_pathlist_head);
2869 while (pe) {
2870 if (got_path_is_child(path, pe->path, pe->path_len)) {
2871 struct got_pathlist_head *ignorelist = pe->data;
2872 struct got_pathlist_entry *pi;
2873 TAILQ_FOREACH(pi, ignorelist, entry) {
2874 const char *pattern = pi->path;
2875 int flags = FNM_LEADING_DIR;
2876 if (strstr(pattern, "/**/") == NULL)
2877 flags |= FNM_PATHNAME;
2878 if (fnmatch(pattern, path, flags))
2879 continue;
2880 return 1;
2883 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2886 return 0;
2889 static const struct got_error *
2890 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2891 const char *path, int dirfd, const char *ignores_filename)
2893 const struct got_error *err = NULL;
2894 char *ignorespath;
2895 int fd = -1;
2896 FILE *ignoresfile = NULL;
2898 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2899 path[0] ? "/" : "", ignores_filename) == -1)
2900 return got_error_from_errno("asprintf");
2902 if (dirfd != -1) {
2903 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2904 if (fd == -1) {
2905 if (errno != ENOENT && errno != EACCES)
2906 err = got_error_from_errno2("openat",
2907 ignorespath);
2908 } else {
2909 ignoresfile = fdopen(fd, "r");
2910 if (ignoresfile == NULL)
2911 err = got_error_from_errno2("fdopen",
2912 ignorespath);
2913 else {
2914 fd = -1;
2915 err = read_ignores(ignores, path, ignoresfile);
2918 } else {
2919 ignoresfile = fopen(ignorespath, "r");
2920 if (ignoresfile == NULL) {
2921 if (errno != ENOENT && errno != EACCES)
2922 err = got_error_from_errno2("fopen",
2923 ignorespath);
2924 } else
2925 err = read_ignores(ignores, path, ignoresfile);
2928 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2929 err = got_error_from_errno2("fclose", path);
2930 if (fd != -1 && close(fd) == -1 && err == NULL)
2931 err = got_error_from_errno2("close", path);
2932 free(ignorespath);
2933 return err;
2936 static const struct got_error *
2937 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2939 const struct got_error *err = NULL;
2940 struct diff_dir_cb_arg *a = arg;
2941 char *path = NULL;
2943 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2944 return got_error(GOT_ERR_CANCELLED);
2946 if (parent_path[0]) {
2947 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2948 return got_error_from_errno("asprintf");
2949 } else {
2950 path = de->d_name;
2953 if (de->d_type != DT_DIR &&
2954 got_path_is_child(path, a->status_path, a->status_path_len)
2955 && !match_ignores(&a->ignores, path))
2956 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2957 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2958 if (parent_path[0])
2959 free(path);
2960 return err;
2963 static const struct got_error *
2964 status_traverse(void *arg, const char *path, int dirfd)
2966 const struct got_error *err = NULL;
2967 struct diff_dir_cb_arg *a = arg;
2969 if (a->no_ignores)
2970 return NULL;
2972 err = add_ignores(&a->ignores, a->worktree->root_path,
2973 path, dirfd, ".cvsignore");
2974 if (err)
2975 return err;
2977 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2978 dirfd, ".gitignore");
2980 return err;
2983 static const struct got_error *
2984 report_single_file_status(const char *path, const char *ondisk_path,
2985 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2986 void *status_arg, struct got_repository *repo, int report_unchanged)
2988 struct got_fileindex_entry *ie;
2989 struct stat sb;
2991 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2992 if (ie)
2993 return report_file_status(ie, ondisk_path, -1, NULL,
2994 status_cb, status_arg, repo, report_unchanged);
2996 if (lstat(ondisk_path, &sb) == -1) {
2997 if (errno != ENOENT)
2998 return got_error_from_errno2("lstat", ondisk_path);
2999 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3000 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3001 return NULL;
3004 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3005 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3006 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3008 return NULL;
3011 static const struct got_error *
3012 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3013 const char *root_path, const char *path)
3015 const struct got_error *err;
3016 char *parent_path, *next_parent_path = NULL;
3018 err = add_ignores(ignores, root_path, "", -1,
3019 ".cvsignore");
3020 if (err)
3021 return err;
3023 err = add_ignores(ignores, root_path, "", -1,
3024 ".gitignore");
3025 if (err)
3026 return err;
3028 err = got_path_dirname(&parent_path, path);
3029 if (err) {
3030 if (err->code == GOT_ERR_BAD_PATH)
3031 return NULL; /* cannot traverse parent */
3032 return err;
3034 for (;;) {
3035 err = add_ignores(ignores, root_path, parent_path, -1,
3036 ".cvsignore");
3037 if (err)
3038 break;
3039 err = add_ignores(ignores, root_path, parent_path, -1,
3040 ".gitignore");
3041 if (err)
3042 break;
3043 err = got_path_dirname(&next_parent_path, parent_path);
3044 if (err) {
3045 if (err->code == GOT_ERR_BAD_PATH)
3046 err = NULL; /* traversed everything */
3047 break;
3049 free(parent_path);
3050 parent_path = next_parent_path;
3051 next_parent_path = NULL;
3054 free(parent_path);
3055 free(next_parent_path);
3056 return err;
3059 static const struct got_error *
3060 worktree_status(struct got_worktree *worktree, const char *path,
3061 struct got_fileindex *fileindex, struct got_repository *repo,
3062 got_worktree_status_cb status_cb, void *status_arg,
3063 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3064 int report_unchanged)
3066 const struct got_error *err = NULL;
3067 int fd = -1;
3068 struct got_fileindex_diff_dir_cb fdiff_cb;
3069 struct diff_dir_cb_arg arg;
3070 char *ondisk_path = NULL;
3072 TAILQ_INIT(&arg.ignores);
3074 if (asprintf(&ondisk_path, "%s%s%s",
3075 worktree->root_path, path[0] ? "/" : "", path) == -1)
3076 return got_error_from_errno("asprintf");
3078 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3079 if (fd == -1) {
3080 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3081 errno != ELOOP)
3082 err = got_error_from_errno2("open", ondisk_path);
3083 else
3084 err = report_single_file_status(path, ondisk_path,
3085 fileindex, status_cb, status_arg, repo,
3086 report_unchanged);
3087 } else {
3088 fdiff_cb.diff_old_new = status_old_new;
3089 fdiff_cb.diff_old = status_old;
3090 fdiff_cb.diff_new = status_new;
3091 fdiff_cb.diff_traverse = status_traverse;
3092 arg.fileindex = fileindex;
3093 arg.worktree = worktree;
3094 arg.status_path = path;
3095 arg.status_path_len = strlen(path);
3096 arg.repo = repo;
3097 arg.status_cb = status_cb;
3098 arg.status_arg = status_arg;
3099 arg.cancel_cb = cancel_cb;
3100 arg.cancel_arg = cancel_arg;
3101 arg.report_unchanged = report_unchanged;
3102 arg.no_ignores = no_ignores;
3103 if (!no_ignores) {
3104 err = add_ignores_from_parent_paths(&arg.ignores,
3105 worktree->root_path, path);
3106 if (err)
3107 goto done;
3109 err = got_fileindex_diff_dir(fileindex, fd,
3110 worktree->root_path, path, repo, &fdiff_cb, &arg);
3112 done:
3113 free_ignores(&arg.ignores);
3114 if (fd != -1 && close(fd) != 0 && err == NULL)
3115 err = got_error_from_errno("close");
3116 free(ondisk_path);
3117 return err;
3120 const struct got_error *
3121 got_worktree_status(struct got_worktree *worktree,
3122 struct got_pathlist_head *paths, struct got_repository *repo,
3123 got_worktree_status_cb status_cb, void *status_arg,
3124 got_cancel_cb cancel_cb, void *cancel_arg)
3126 const struct got_error *err = NULL;
3127 char *fileindex_path = NULL;
3128 struct got_fileindex *fileindex = NULL;
3129 struct got_pathlist_entry *pe;
3131 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3132 if (err)
3133 return err;
3135 TAILQ_FOREACH(pe, paths, entry) {
3136 err = worktree_status(worktree, pe->path, fileindex, repo,
3137 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3138 if (err)
3139 break;
3141 free(fileindex_path);
3142 got_fileindex_free(fileindex);
3143 return err;
3146 const struct got_error *
3147 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3148 const char *arg)
3150 const struct got_error *err = NULL;
3151 char *resolved = NULL, *cwd = NULL, *path = NULL;
3152 size_t len;
3153 struct stat sb;
3155 *wt_path = NULL;
3157 cwd = getcwd(NULL, 0);
3158 if (cwd == NULL)
3159 return got_error_from_errno("getcwd");
3161 if (lstat(arg, &sb) == -1) {
3162 if (errno != ENOENT) {
3163 err = got_error_from_errno2("lstat", arg);
3164 goto done;
3167 if (S_ISLNK(sb.st_mode)) {
3169 * We cannot use realpath(3) with symlinks since we want to
3170 * operate on the symlink itself.
3171 * But we can make the path absolute, assuming it is relative
3172 * to the current working directory, and then canonicalize it.
3174 char *abspath = NULL;
3175 char canonpath[PATH_MAX];
3176 if (!got_path_is_absolute(arg)) {
3177 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3178 err = got_error_from_errno("asprintf");
3179 goto done;
3183 err = got_canonpath(abspath ? abspath : arg, canonpath,
3184 sizeof(canonpath));
3185 if (err)
3186 goto done;
3187 resolved = strdup(canonpath);
3188 if (resolved == NULL) {
3189 err = got_error_from_errno("strdup");
3190 goto done;
3192 } else {
3193 resolved = realpath(arg, NULL);
3194 if (resolved == NULL) {
3195 if (errno != ENOENT) {
3196 err = got_error_from_errno2("realpath", arg);
3197 goto done;
3199 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3200 err = got_error_from_errno("asprintf");
3201 goto done;
3206 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3207 strlen(got_worktree_get_root_path(worktree)))) {
3208 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3209 goto done;
3212 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3213 err = got_path_skip_common_ancestor(&path,
3214 got_worktree_get_root_path(worktree), resolved);
3215 if (err)
3216 goto done;
3217 } else {
3218 path = strdup("");
3219 if (path == NULL) {
3220 err = got_error_from_errno("strdup");
3221 goto done;
3225 /* XXX status walk can't deal with trailing slash! */
3226 len = strlen(path);
3227 while (len > 0 && path[len - 1] == '/') {
3228 path[len - 1] = '\0';
3229 len--;
3231 done:
3232 free(resolved);
3233 free(cwd);
3234 if (err == NULL)
3235 *wt_path = path;
3236 else
3237 free(path);
3238 return err;
3241 struct schedule_addition_args {
3242 struct got_worktree *worktree;
3243 struct got_fileindex *fileindex;
3244 got_worktree_checkout_cb progress_cb;
3245 void *progress_arg;
3246 struct got_repository *repo;
3249 static const struct got_error *
3250 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3251 const char *relpath, struct got_object_id *blob_id,
3252 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3253 int dirfd, const char *de_name)
3255 struct schedule_addition_args *a = arg;
3256 const struct got_error *err = NULL;
3257 struct got_fileindex_entry *ie;
3258 struct stat sb;
3259 char *ondisk_path;
3261 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3262 relpath) == -1)
3263 return got_error_from_errno("asprintf");
3265 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3266 if (ie) {
3267 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3268 de_name, a->repo);
3269 if (err)
3270 goto done;
3271 /* Re-adding an existing entry is a no-op. */
3272 if (status == GOT_STATUS_ADD)
3273 goto done;
3274 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3275 if (err)
3276 goto done;
3279 if (status != GOT_STATUS_UNVERSIONED) {
3280 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3281 goto done;
3284 err = got_fileindex_entry_alloc(&ie, relpath);
3285 if (err)
3286 goto done;
3287 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3288 if (err) {
3289 got_fileindex_entry_free(ie);
3290 goto done;
3292 err = got_fileindex_entry_add(a->fileindex, ie);
3293 if (err) {
3294 got_fileindex_entry_free(ie);
3295 goto done;
3297 done:
3298 free(ondisk_path);
3299 if (err)
3300 return err;
3301 if (status == GOT_STATUS_ADD)
3302 return NULL;
3303 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3306 const struct got_error *
3307 got_worktree_schedule_add(struct got_worktree *worktree,
3308 struct got_pathlist_head *paths,
3309 got_worktree_checkout_cb progress_cb, void *progress_arg,
3310 struct got_repository *repo, int no_ignores)
3312 struct got_fileindex *fileindex = NULL;
3313 char *fileindex_path = NULL;
3314 const struct got_error *err = NULL, *sync_err, *unlockerr;
3315 struct got_pathlist_entry *pe;
3316 struct schedule_addition_args saa;
3318 err = lock_worktree(worktree, LOCK_EX);
3319 if (err)
3320 return err;
3322 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3323 if (err)
3324 goto done;
3326 saa.worktree = worktree;
3327 saa.fileindex = fileindex;
3328 saa.progress_cb = progress_cb;
3329 saa.progress_arg = progress_arg;
3330 saa.repo = repo;
3332 TAILQ_FOREACH(pe, paths, entry) {
3333 err = worktree_status(worktree, pe->path, fileindex, repo,
3334 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3335 if (err)
3336 break;
3338 sync_err = sync_fileindex(fileindex, fileindex_path);
3339 if (sync_err && err == NULL)
3340 err = sync_err;
3341 done:
3342 free(fileindex_path);
3343 if (fileindex)
3344 got_fileindex_free(fileindex);
3345 unlockerr = lock_worktree(worktree, LOCK_SH);
3346 if (unlockerr && err == NULL)
3347 err = unlockerr;
3348 return err;
3351 struct schedule_deletion_args {
3352 struct got_worktree *worktree;
3353 struct got_fileindex *fileindex;
3354 got_worktree_delete_cb progress_cb;
3355 void *progress_arg;
3356 struct got_repository *repo;
3357 int delete_local_mods;
3358 int keep_on_disk;
3361 static const struct got_error *
3362 schedule_for_deletion(void *arg, unsigned char status,
3363 unsigned char staged_status, const char *relpath,
3364 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3365 struct got_object_id *commit_id, int dirfd, const char *de_name)
3367 struct schedule_deletion_args *a = arg;
3368 const struct got_error *err = NULL;
3369 struct got_fileindex_entry *ie = NULL;
3370 struct stat sb;
3371 char *ondisk_path, *parent = NULL;
3373 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3374 if (ie == NULL)
3375 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3377 staged_status = get_staged_status(ie);
3378 if (staged_status != GOT_STATUS_NO_CHANGE) {
3379 if (staged_status == GOT_STATUS_DELETE)
3380 return NULL;
3381 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3384 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3385 relpath) == -1)
3386 return got_error_from_errno("asprintf");
3388 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3389 a->repo);
3390 if (err)
3391 goto done;
3393 if (status != GOT_STATUS_NO_CHANGE) {
3394 if (status == GOT_STATUS_DELETE)
3395 goto done;
3396 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3397 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3398 goto done;
3400 if (status != GOT_STATUS_MODIFY &&
3401 status != GOT_STATUS_MISSING) {
3402 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3403 goto done;
3407 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3408 if (dirfd != -1) {
3409 if (unlinkat(dirfd, de_name, 0) != 0) {
3410 err = got_error_from_errno2("unlinkat",
3411 ondisk_path);
3412 goto done;
3414 } else if (unlink(ondisk_path) != 0) {
3415 err = got_error_from_errno2("unlink", ondisk_path);
3416 goto done;
3419 parent = dirname(ondisk_path);
3421 if (parent == NULL) {
3422 err = got_error_from_errno2("dirname", ondisk_path);
3423 goto done;
3425 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3426 if (rmdir(parent) == -1) {
3427 if (errno != ENOTEMPTY)
3428 err = got_error_from_errno2("rmdir",
3429 parent);
3430 break;
3432 parent = dirname(parent);
3433 if (parent == NULL) {
3434 err = got_error_from_errno2("dirname", parent);
3435 goto done;
3440 got_fileindex_entry_mark_deleted_from_disk(ie);
3441 done:
3442 free(ondisk_path);
3443 if (err)
3444 return err;
3445 if (status == GOT_STATUS_DELETE)
3446 return NULL;
3447 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3448 staged_status, relpath);
3451 const struct got_error *
3452 got_worktree_schedule_delete(struct got_worktree *worktree,
3453 struct got_pathlist_head *paths, int delete_local_mods,
3454 got_worktree_delete_cb progress_cb, void *progress_arg,
3455 struct got_repository *repo, int keep_on_disk)
3457 struct got_fileindex *fileindex = NULL;
3458 char *fileindex_path = NULL;
3459 const struct got_error *err = NULL, *sync_err, *unlockerr;
3460 struct got_pathlist_entry *pe;
3461 struct schedule_deletion_args sda;
3463 err = lock_worktree(worktree, LOCK_EX);
3464 if (err)
3465 return err;
3467 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3468 if (err)
3469 goto done;
3471 sda.worktree = worktree;
3472 sda.fileindex = fileindex;
3473 sda.progress_cb = progress_cb;
3474 sda.progress_arg = progress_arg;
3475 sda.repo = repo;
3476 sda.delete_local_mods = delete_local_mods;
3477 sda.keep_on_disk = keep_on_disk;
3479 TAILQ_FOREACH(pe, paths, entry) {
3480 err = worktree_status(worktree, pe->path, fileindex, repo,
3481 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3482 if (err)
3483 break;
3485 sync_err = sync_fileindex(fileindex, fileindex_path);
3486 if (sync_err && err == NULL)
3487 err = sync_err;
3488 done:
3489 free(fileindex_path);
3490 if (fileindex)
3491 got_fileindex_free(fileindex);
3492 unlockerr = lock_worktree(worktree, LOCK_SH);
3493 if (unlockerr && err == NULL)
3494 err = unlockerr;
3495 return err;
3498 static const struct got_error *
3499 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3501 const struct got_error *err = NULL;
3502 char *line = NULL;
3503 size_t linesize = 0, n;
3504 ssize_t linelen;
3506 linelen = getline(&line, &linesize, infile);
3507 if (linelen == -1) {
3508 if (ferror(infile)) {
3509 err = got_error_from_errno("getline");
3510 goto done;
3512 return NULL;
3514 if (outfile) {
3515 n = fwrite(line, 1, linelen, outfile);
3516 if (n != linelen) {
3517 err = got_ferror(outfile, GOT_ERR_IO);
3518 goto done;
3521 if (rejectfile) {
3522 n = fwrite(line, 1, linelen, rejectfile);
3523 if (n != linelen)
3524 err = got_ferror(outfile, GOT_ERR_IO);
3526 done:
3527 free(line);
3528 return err;
3531 static const struct got_error *
3532 skip_one_line(FILE *f)
3534 char *line = NULL;
3535 size_t linesize = 0;
3536 ssize_t linelen;
3538 linelen = getline(&line, &linesize, f);
3539 if (linelen == -1) {
3540 if (ferror(f))
3541 return got_error_from_errno("getline");
3542 return NULL;
3544 free(line);
3545 return NULL;
3548 static const struct got_error *
3549 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3550 int start_old, int end_old, int start_new, int end_new,
3551 FILE *outfile, FILE *rejectfile)
3553 const struct got_error *err;
3555 /* Copy old file's lines leading up to patch. */
3556 while (!feof(f1) && *line_cur1 < start_old) {
3557 err = copy_one_line(f1, outfile, NULL);
3558 if (err)
3559 return err;
3560 (*line_cur1)++;
3562 /* Skip new file's lines leading up to patch. */
3563 while (!feof(f2) && *line_cur2 < start_new) {
3564 if (rejectfile)
3565 err = copy_one_line(f2, NULL, rejectfile);
3566 else
3567 err = skip_one_line(f2);
3568 if (err)
3569 return err;
3570 (*line_cur2)++;
3572 /* Copy patched lines. */
3573 while (!feof(f2) && *line_cur2 <= end_new) {
3574 err = copy_one_line(f2, outfile, NULL);
3575 if (err)
3576 return err;
3577 (*line_cur2)++;
3579 /* Skip over old file's replaced lines. */
3580 while (!feof(f1) && *line_cur1 <= end_old) {
3581 if (rejectfile)
3582 err = copy_one_line(f1, NULL, rejectfile);
3583 else
3584 err = skip_one_line(f1);
3585 if (err)
3586 return err;
3587 (*line_cur1)++;
3590 return NULL;
3593 static const struct got_error *
3594 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3595 FILE *outfile, FILE *rejectfile)
3597 const struct got_error *err;
3599 if (outfile) {
3600 /* Copy old file's lines until EOF. */
3601 while (!feof(f1)) {
3602 err = copy_one_line(f1, outfile, NULL);
3603 if (err)
3604 return err;
3605 (*line_cur1)++;
3608 if (rejectfile) {
3609 /* Copy new file's lines until EOF. */
3610 while (!feof(f2)) {
3611 err = copy_one_line(f2, NULL, rejectfile);
3612 if (err)
3613 return err;
3614 (*line_cur2)++;
3618 return NULL;
3621 static const struct got_error *
3622 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3623 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3624 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3625 int *line_cur2, FILE *outfile, FILE *rejectfile,
3626 got_worktree_patch_cb patch_cb, void *patch_arg)
3628 const struct got_error *err = NULL;
3629 int start_old = change->cv.a;
3630 int end_old = change->cv.b;
3631 int start_new = change->cv.c;
3632 int end_new = change->cv.d;
3633 long pos1, pos2;
3634 FILE *hunkfile;
3636 *choice = GOT_PATCH_CHOICE_NONE;
3638 hunkfile = got_opentemp();
3639 if (hunkfile == NULL)
3640 return got_error_from_errno("got_opentemp");
3642 pos1 = ftell(f1);
3643 pos2 = ftell(f2);
3645 /* XXX TODO needs error checking */
3646 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3648 if (fseek(f1, pos1, SEEK_SET) == -1) {
3649 err = got_ferror(f1, GOT_ERR_IO);
3650 goto done;
3652 if (fseek(f2, pos2, SEEK_SET) == -1) {
3653 err = got_ferror(f1, GOT_ERR_IO);
3654 goto done;
3656 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3657 err = got_ferror(hunkfile, GOT_ERR_IO);
3658 goto done;
3661 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3662 hunkfile, n, nchanges);
3663 if (err)
3664 goto done;
3666 switch (*choice) {
3667 case GOT_PATCH_CHOICE_YES:
3668 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3669 end_old, start_new, end_new, outfile, rejectfile);
3670 break;
3671 case GOT_PATCH_CHOICE_NO:
3672 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3673 end_old, start_new, end_new, rejectfile, outfile);
3674 break;
3675 case GOT_PATCH_CHOICE_QUIT:
3676 break;
3677 default:
3678 err = got_error(GOT_ERR_PATCH_CHOICE);
3679 break;
3681 done:
3682 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3683 err = got_error_from_errno("fclose");
3684 return err;
3687 struct revert_file_args {
3688 struct got_worktree *worktree;
3689 struct got_fileindex *fileindex;
3690 got_worktree_checkout_cb progress_cb;
3691 void *progress_arg;
3692 got_worktree_patch_cb patch_cb;
3693 void *patch_arg;
3694 struct got_repository *repo;
3697 static const struct got_error *
3698 create_patched_content(char **path_outfile, int reverse_patch,
3699 struct got_object_id *blob_id, const char *path2,
3700 int dirfd2, const char *de_name2,
3701 const char *relpath, struct got_repository *repo,
3702 got_worktree_patch_cb patch_cb, void *patch_arg)
3704 const struct got_error *err;
3705 struct got_blob_object *blob = NULL;
3706 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3707 int fd2 = -1;
3708 char *path1 = NULL, *id_str = NULL;
3709 struct stat sb1, sb2;
3710 struct got_diff_changes *changes = NULL;
3711 struct got_diff_state *ds = NULL;
3712 struct got_diff_args *args = NULL;
3713 struct got_diff_change *change;
3714 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3715 int n = 0;
3717 *path_outfile = NULL;
3719 err = got_object_id_str(&id_str, blob_id);
3720 if (err)
3721 return err;
3723 if (dirfd2 != -1) {
3724 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3725 if (fd2 == -1) {
3726 err = got_error_from_errno2("openat", path2);
3727 goto done;
3729 } else {
3730 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3731 if (fd2 == -1) {
3732 err = got_error_from_errno2("open", path2);
3733 goto done;
3736 if (fstat(fd2, &sb2) == -1) {
3737 err = got_error_from_errno2("fstat", path2);
3738 goto done;
3741 f2 = fdopen(fd2, "r");
3742 if (f2 == NULL) {
3743 err = got_error_from_errno2("fdopen", path2);
3744 goto done;
3746 fd2 = -1;
3748 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3749 if (err)
3750 goto done;
3752 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3753 if (err)
3754 goto done;
3756 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3757 if (err)
3758 goto done;
3760 if (stat(path1, &sb1) == -1) {
3761 err = got_error_from_errno2("stat", path1);
3762 goto done;
3765 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3766 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3767 if (err)
3768 goto done;
3770 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3771 if (err)
3772 goto done;
3774 if (fseek(f1, 0L, SEEK_SET) == -1)
3775 return got_ferror(f1, GOT_ERR_IO);
3776 if (fseek(f2, 0L, SEEK_SET) == -1)
3777 return got_ferror(f2, GOT_ERR_IO);
3778 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3779 int choice;
3780 err = apply_or_reject_change(&choice, change, ++n,
3781 changes->nchanges, ds, args, diff_flags, relpath,
3782 f1, f2, &line_cur1, &line_cur2,
3783 reverse_patch ? NULL : outfile,
3784 reverse_patch ? outfile : NULL,
3785 patch_cb, patch_arg);
3786 if (err)
3787 goto done;
3788 if (choice == GOT_PATCH_CHOICE_YES)
3789 have_content = 1;
3790 else if (choice == GOT_PATCH_CHOICE_QUIT)
3791 break;
3793 if (have_content) {
3794 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3795 reverse_patch ? NULL : outfile,
3796 reverse_patch ? outfile : NULL);
3797 if (err)
3798 goto done;
3800 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3801 err = got_error_from_errno2("chmod", path2);
3802 goto done;
3805 done:
3806 free(id_str);
3807 if (blob)
3808 got_object_blob_close(blob);
3809 if (f1 && fclose(f1) == EOF && err == NULL)
3810 err = got_error_from_errno2("fclose", path1);
3811 if (f2 && fclose(f2) == EOF && err == NULL)
3812 err = got_error_from_errno2("fclose", path2);
3813 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3814 err = got_error_from_errno2("close", path2);
3815 if (outfile && fclose(outfile) == EOF && err == NULL)
3816 err = got_error_from_errno2("fclose", *path_outfile);
3817 if (path1 && unlink(path1) == -1 && err == NULL)
3818 err = got_error_from_errno2("unlink", path1);
3819 if (err || !have_content) {
3820 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3821 err = got_error_from_errno2("unlink", *path_outfile);
3822 free(*path_outfile);
3823 *path_outfile = NULL;
3825 free(args);
3826 if (ds) {
3827 got_diff_state_free(ds);
3828 free(ds);
3830 if (changes)
3831 got_diff_free_changes(changes);
3832 free(path1);
3833 return err;
3836 static const struct got_error *
3837 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3838 const char *relpath, struct got_object_id *blob_id,
3839 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3840 int dirfd, const char *de_name)
3842 struct revert_file_args *a = arg;
3843 const struct got_error *err = NULL;
3844 char *parent_path = NULL;
3845 struct got_fileindex_entry *ie;
3846 struct got_tree_object *tree = NULL;
3847 struct got_object_id *tree_id = NULL;
3848 const struct got_tree_entry *te = NULL;
3849 char *tree_path = NULL, *te_name;
3850 char *ondisk_path = NULL, *path_content = NULL;
3851 struct got_blob_object *blob = NULL;
3853 /* Reverting a staged deletion is a no-op. */
3854 if (status == GOT_STATUS_DELETE &&
3855 staged_status != GOT_STATUS_NO_CHANGE)
3856 return NULL;
3858 if (status == GOT_STATUS_UNVERSIONED)
3859 return (*a->progress_cb)(a->progress_arg,
3860 GOT_STATUS_UNVERSIONED, relpath);
3862 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3863 if (ie == NULL)
3864 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3866 /* Construct in-repository path of tree which contains this blob. */
3867 err = got_path_dirname(&parent_path, ie->path);
3868 if (err) {
3869 if (err->code != GOT_ERR_BAD_PATH)
3870 goto done;
3871 parent_path = strdup("/");
3872 if (parent_path == NULL) {
3873 err = got_error_from_errno("strdup");
3874 goto done;
3877 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3878 tree_path = strdup(parent_path);
3879 if (tree_path == NULL) {
3880 err = got_error_from_errno("strdup");
3881 goto done;
3883 } else {
3884 if (got_path_is_root_dir(parent_path)) {
3885 tree_path = strdup(a->worktree->path_prefix);
3886 if (tree_path == NULL) {
3887 err = got_error_from_errno("strdup");
3888 goto done;
3890 } else {
3891 if (asprintf(&tree_path, "%s/%s",
3892 a->worktree->path_prefix, parent_path) == -1) {
3893 err = got_error_from_errno("asprintf");
3894 goto done;
3899 err = got_object_id_by_path(&tree_id, a->repo,
3900 a->worktree->base_commit_id, tree_path);
3901 if (err) {
3902 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3903 (status == GOT_STATUS_ADD ||
3904 staged_status == GOT_STATUS_ADD)))
3905 goto done;
3906 } else {
3907 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3908 if (err)
3909 goto done;
3911 te_name = basename(ie->path);
3912 if (te_name == NULL) {
3913 err = got_error_from_errno2("basename", ie->path);
3914 goto done;
3917 te = got_object_tree_find_entry(tree, te_name);
3918 if (te == NULL && status != GOT_STATUS_ADD &&
3919 staged_status != GOT_STATUS_ADD) {
3920 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3921 goto done;
3925 switch (status) {
3926 case GOT_STATUS_ADD:
3927 if (a->patch_cb) {
3928 int choice = GOT_PATCH_CHOICE_NONE;
3929 err = (*a->patch_cb)(&choice, a->patch_arg,
3930 status, ie->path, NULL, 1, 1);
3931 if (err)
3932 goto done;
3933 if (choice != GOT_PATCH_CHOICE_YES)
3934 break;
3936 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3937 ie->path);
3938 if (err)
3939 goto done;
3940 got_fileindex_entry_remove(a->fileindex, ie);
3941 break;
3942 case GOT_STATUS_DELETE:
3943 if (a->patch_cb) {
3944 int choice = GOT_PATCH_CHOICE_NONE;
3945 err = (*a->patch_cb)(&choice, a->patch_arg,
3946 status, ie->path, NULL, 1, 1);
3947 if (err)
3948 goto done;
3949 if (choice != GOT_PATCH_CHOICE_YES)
3950 break;
3952 /* fall through */
3953 case GOT_STATUS_MODIFY:
3954 case GOT_STATUS_MODE_CHANGE:
3955 case GOT_STATUS_CONFLICT:
3956 case GOT_STATUS_MISSING: {
3957 struct got_object_id id;
3958 if (staged_status == GOT_STATUS_ADD ||
3959 staged_status == GOT_STATUS_MODIFY) {
3960 memcpy(id.sha1, ie->staged_blob_sha1,
3961 SHA1_DIGEST_LENGTH);
3962 } else
3963 memcpy(id.sha1, ie->blob_sha1,
3964 SHA1_DIGEST_LENGTH);
3965 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3966 if (err)
3967 goto done;
3969 if (asprintf(&ondisk_path, "%s/%s",
3970 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3971 err = got_error_from_errno("asprintf");
3972 goto done;
3975 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3976 status == GOT_STATUS_CONFLICT)) {
3977 err = create_patched_content(&path_content, 1, &id,
3978 ondisk_path, dirfd, de_name, ie->path, a->repo,
3979 a->patch_cb, a->patch_arg);
3980 if (err || path_content == NULL)
3981 break;
3982 if (rename(path_content, ondisk_path) == -1) {
3983 err = got_error_from_errno3("rename",
3984 path_content, ondisk_path);
3985 goto done;
3987 } else {
3988 err = install_blob(a->worktree, ondisk_path, ie->path,
3989 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3990 got_fileindex_perms_to_st(ie), blob, 0, 1,
3991 a->repo, a->progress_cb, a->progress_arg);
3992 if (err)
3993 goto done;
3994 if (status == GOT_STATUS_DELETE ||
3995 status == GOT_STATUS_MODE_CHANGE) {
3996 err = got_fileindex_entry_update(ie,
3997 ondisk_path, blob->id.sha1,
3998 a->worktree->base_commit_id->sha1, 1);
3999 if (err)
4000 goto done;
4003 break;
4005 default:
4006 break;
4008 done:
4009 free(ondisk_path);
4010 free(path_content);
4011 free(parent_path);
4012 free(tree_path);
4013 if (blob)
4014 got_object_blob_close(blob);
4015 if (tree)
4016 got_object_tree_close(tree);
4017 free(tree_id);
4018 return err;
4021 const struct got_error *
4022 got_worktree_revert(struct got_worktree *worktree,
4023 struct got_pathlist_head *paths,
4024 got_worktree_checkout_cb progress_cb, void *progress_arg,
4025 got_worktree_patch_cb patch_cb, void *patch_arg,
4026 struct got_repository *repo)
4028 struct got_fileindex *fileindex = NULL;
4029 char *fileindex_path = NULL;
4030 const struct got_error *err = NULL, *unlockerr = NULL;
4031 const struct got_error *sync_err = NULL;
4032 struct got_pathlist_entry *pe;
4033 struct revert_file_args rfa;
4035 err = lock_worktree(worktree, LOCK_EX);
4036 if (err)
4037 return err;
4039 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4040 if (err)
4041 goto done;
4043 rfa.worktree = worktree;
4044 rfa.fileindex = fileindex;
4045 rfa.progress_cb = progress_cb;
4046 rfa.progress_arg = progress_arg;
4047 rfa.patch_cb = patch_cb;
4048 rfa.patch_arg = patch_arg;
4049 rfa.repo = repo;
4050 TAILQ_FOREACH(pe, paths, entry) {
4051 err = worktree_status(worktree, pe->path, fileindex, repo,
4052 revert_file, &rfa, NULL, NULL, 0, 0);
4053 if (err)
4054 break;
4056 sync_err = sync_fileindex(fileindex, fileindex_path);
4057 if (sync_err && err == NULL)
4058 err = sync_err;
4059 done:
4060 free(fileindex_path);
4061 if (fileindex)
4062 got_fileindex_free(fileindex);
4063 unlockerr = lock_worktree(worktree, LOCK_SH);
4064 if (unlockerr && err == NULL)
4065 err = unlockerr;
4066 return err;
4069 static void
4070 free_commitable(struct got_commitable *ct)
4072 free(ct->path);
4073 free(ct->in_repo_path);
4074 free(ct->ondisk_path);
4075 free(ct->blob_id);
4076 free(ct->base_blob_id);
4077 free(ct->staged_blob_id);
4078 free(ct->base_commit_id);
4079 free(ct);
4082 struct collect_commitables_arg {
4083 struct got_pathlist_head *commitable_paths;
4084 struct got_repository *repo;
4085 struct got_worktree *worktree;
4086 int have_staged_files;
4089 static const struct got_error *
4090 collect_commitables(void *arg, unsigned char status,
4091 unsigned char staged_status, const char *relpath,
4092 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4093 struct got_object_id *commit_id, int dirfd, const char *de_name)
4095 struct collect_commitables_arg *a = arg;
4096 const struct got_error *err = NULL;
4097 struct got_commitable *ct = NULL;
4098 struct got_pathlist_entry *new = NULL;
4099 char *parent_path = NULL, *path = NULL;
4100 struct stat sb;
4102 if (a->have_staged_files) {
4103 if (staged_status != GOT_STATUS_MODIFY &&
4104 staged_status != GOT_STATUS_ADD &&
4105 staged_status != GOT_STATUS_DELETE)
4106 return NULL;
4107 } else {
4108 if (status == GOT_STATUS_CONFLICT)
4109 return got_error(GOT_ERR_COMMIT_CONFLICT);
4111 if (status != GOT_STATUS_MODIFY &&
4112 status != GOT_STATUS_MODE_CHANGE &&
4113 status != GOT_STATUS_ADD &&
4114 status != GOT_STATUS_DELETE)
4115 return NULL;
4118 if (asprintf(&path, "/%s", relpath) == -1) {
4119 err = got_error_from_errno("asprintf");
4120 goto done;
4122 if (strcmp(path, "/") == 0) {
4123 parent_path = strdup("");
4124 if (parent_path == NULL)
4125 return got_error_from_errno("strdup");
4126 } else {
4127 err = got_path_dirname(&parent_path, path);
4128 if (err)
4129 return err;
4132 ct = calloc(1, sizeof(*ct));
4133 if (ct == NULL) {
4134 err = got_error_from_errno("calloc");
4135 goto done;
4138 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4139 relpath) == -1) {
4140 err = got_error_from_errno("asprintf");
4141 goto done;
4143 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4144 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4145 } else {
4146 if (dirfd != -1) {
4147 if (fstatat(dirfd, de_name, &sb,
4148 AT_SYMLINK_NOFOLLOW) == -1) {
4149 err = got_error_from_errno2("fstatat",
4150 ct->ondisk_path);
4151 goto done;
4153 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4154 err = got_error_from_errno2("lstat", ct->ondisk_path);
4155 goto done;
4157 ct->mode = sb.st_mode;
4160 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4161 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4162 relpath) == -1) {
4163 err = got_error_from_errno("asprintf");
4164 goto done;
4167 ct->status = status;
4168 ct->staged_status = staged_status;
4169 ct->blob_id = NULL; /* will be filled in when blob gets created */
4170 if (ct->status != GOT_STATUS_ADD &&
4171 ct->staged_status != GOT_STATUS_ADD) {
4172 ct->base_blob_id = got_object_id_dup(blob_id);
4173 if (ct->base_blob_id == NULL) {
4174 err = got_error_from_errno("got_object_id_dup");
4175 goto done;
4177 ct->base_commit_id = got_object_id_dup(commit_id);
4178 if (ct->base_commit_id == NULL) {
4179 err = got_error_from_errno("got_object_id_dup");
4180 goto done;
4183 if (ct->staged_status == GOT_STATUS_ADD ||
4184 ct->staged_status == GOT_STATUS_MODIFY) {
4185 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4186 if (ct->staged_blob_id == NULL) {
4187 err = got_error_from_errno("got_object_id_dup");
4188 goto done;
4191 ct->path = strdup(path);
4192 if (ct->path == NULL) {
4193 err = got_error_from_errno("strdup");
4194 goto done;
4196 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4197 done:
4198 if (ct && (err || new == NULL))
4199 free_commitable(ct);
4200 free(parent_path);
4201 free(path);
4202 return err;
4205 static const struct got_error *write_tree(struct got_object_id **, int *,
4206 struct got_tree_object *, const char *, struct got_pathlist_head *,
4207 got_worktree_status_cb status_cb, void *status_arg,
4208 struct got_repository *);
4210 static const struct got_error *
4211 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4212 struct got_tree_entry *te, const char *parent_path,
4213 struct got_pathlist_head *commitable_paths,
4214 got_worktree_status_cb status_cb, void *status_arg,
4215 struct got_repository *repo)
4217 const struct got_error *err = NULL;
4218 struct got_tree_object *subtree;
4219 char *subpath;
4221 if (asprintf(&subpath, "%s%s%s", parent_path,
4222 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4223 return got_error_from_errno("asprintf");
4225 err = got_object_open_as_tree(&subtree, repo, &te->id);
4226 if (err)
4227 return err;
4229 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4230 commitable_paths, status_cb, status_arg, repo);
4231 got_object_tree_close(subtree);
4232 free(subpath);
4233 return err;
4236 static const struct got_error *
4237 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4239 const struct got_error *err = NULL;
4240 char *ct_parent_path = NULL;
4242 *match = 0;
4244 if (strchr(ct->in_repo_path, '/') == NULL) {
4245 *match = got_path_is_root_dir(path);
4246 return NULL;
4249 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4250 if (err)
4251 return err;
4252 *match = (strcmp(path, ct_parent_path) == 0);
4253 free(ct_parent_path);
4254 return err;
4257 static mode_t
4258 get_ct_file_mode(struct got_commitable *ct)
4260 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4263 static const struct got_error *
4264 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4265 struct got_tree_entry *te, struct got_commitable *ct)
4267 const struct got_error *err = NULL;
4269 *new_te = NULL;
4271 err = got_object_tree_entry_dup(new_te, te);
4272 if (err)
4273 goto done;
4275 (*new_te)->mode = get_ct_file_mode(ct);
4277 if (ct->staged_status == GOT_STATUS_MODIFY)
4278 memcpy(&(*new_te)->id, ct->staged_blob_id,
4279 sizeof((*new_te)->id));
4280 else
4281 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4282 done:
4283 if (err && *new_te) {
4284 free(*new_te);
4285 *new_te = NULL;
4287 return err;
4290 static const struct got_error *
4291 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4292 struct got_commitable *ct)
4294 const struct got_error *err = NULL;
4295 char *ct_name;
4297 *new_te = NULL;
4299 *new_te = calloc(1, sizeof(**new_te));
4300 if (*new_te == NULL)
4301 return got_error_from_errno("calloc");
4303 ct_name = basename(ct->path);
4304 if (ct_name == NULL) {
4305 err = got_error_from_errno2("basename", ct->path);
4306 goto done;
4308 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4309 sizeof((*new_te)->name)) {
4310 err = got_error(GOT_ERR_NO_SPACE);
4311 goto done;
4314 (*new_te)->mode = get_ct_file_mode(ct);
4316 if (ct->staged_status == GOT_STATUS_ADD)
4317 memcpy(&(*new_te)->id, ct->staged_blob_id,
4318 sizeof((*new_te)->id));
4319 else
4320 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4321 done:
4322 if (err && *new_te) {
4323 free(*new_te);
4324 *new_te = NULL;
4326 return err;
4329 static const struct got_error *
4330 insert_tree_entry(struct got_tree_entry *new_te,
4331 struct got_pathlist_head *paths)
4333 const struct got_error *err = NULL;
4334 struct got_pathlist_entry *new_pe;
4336 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4337 if (err)
4338 return err;
4339 if (new_pe == NULL)
4340 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4341 return NULL;
4344 static const struct got_error *
4345 report_ct_status(struct got_commitable *ct,
4346 got_worktree_status_cb status_cb, void *status_arg)
4348 const char *ct_path = ct->path;
4349 unsigned char status;
4351 while (ct_path[0] == '/')
4352 ct_path++;
4354 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4355 status = ct->staged_status;
4356 else
4357 status = ct->status;
4359 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4360 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4363 static const struct got_error *
4364 match_modified_subtree(int *modified, struct got_tree_entry *te,
4365 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4367 const struct got_error *err = NULL;
4368 struct got_pathlist_entry *pe;
4369 char *te_path;
4371 *modified = 0;
4373 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4374 got_path_is_root_dir(base_tree_path) ? "" : "/",
4375 te->name) == -1)
4376 return got_error_from_errno("asprintf");
4378 TAILQ_FOREACH(pe, commitable_paths, entry) {
4379 struct got_commitable *ct = pe->data;
4380 *modified = got_path_is_child(ct->in_repo_path, te_path,
4381 strlen(te_path));
4382 if (*modified)
4383 break;
4386 free(te_path);
4387 return err;
4390 static const struct got_error *
4391 match_deleted_or_modified_ct(struct got_commitable **ctp,
4392 struct got_tree_entry *te, const char *base_tree_path,
4393 struct got_pathlist_head *commitable_paths)
4395 const struct got_error *err = NULL;
4396 struct got_pathlist_entry *pe;
4398 *ctp = NULL;
4400 TAILQ_FOREACH(pe, commitable_paths, entry) {
4401 struct got_commitable *ct = pe->data;
4402 char *ct_name = NULL;
4403 int path_matches;
4405 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4406 if (ct->status != GOT_STATUS_MODIFY &&
4407 ct->status != GOT_STATUS_MODE_CHANGE &&
4408 ct->status != GOT_STATUS_DELETE)
4409 continue;
4410 } else {
4411 if (ct->staged_status != GOT_STATUS_MODIFY &&
4412 ct->staged_status != GOT_STATUS_DELETE)
4413 continue;
4416 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4417 continue;
4419 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4420 if (err)
4421 return err;
4422 if (!path_matches)
4423 continue;
4425 ct_name = basename(pe->path);
4426 if (ct_name == NULL)
4427 return got_error_from_errno2("basename", pe->path);
4429 if (strcmp(te->name, ct_name) != 0)
4430 continue;
4432 *ctp = ct;
4433 break;
4436 return err;
4439 static const struct got_error *
4440 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4441 const char *child_path, const char *path_base_tree,
4442 struct got_pathlist_head *commitable_paths,
4443 got_worktree_status_cb status_cb, void *status_arg,
4444 struct got_repository *repo)
4446 const struct got_error *err = NULL;
4447 struct got_tree_entry *new_te;
4448 char *subtree_path;
4449 struct got_object_id *id = NULL;
4450 int nentries;
4452 *new_tep = NULL;
4454 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4455 got_path_is_root_dir(path_base_tree) ? "" : "/",
4456 child_path) == -1)
4457 return got_error_from_errno("asprintf");
4459 new_te = calloc(1, sizeof(*new_te));
4460 if (new_te == NULL)
4461 return got_error_from_errno("calloc");
4462 new_te->mode = S_IFDIR;
4464 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4465 sizeof(new_te->name)) {
4466 err = got_error(GOT_ERR_NO_SPACE);
4467 goto done;
4469 err = write_tree(&id, &nentries, NULL, subtree_path,
4470 commitable_paths, status_cb, status_arg, repo);
4471 if (err) {
4472 free(new_te);
4473 goto done;
4475 memcpy(&new_te->id, id, sizeof(new_te->id));
4476 done:
4477 free(id);
4478 free(subtree_path);
4479 if (err == NULL)
4480 *new_tep = new_te;
4481 return err;
4484 static const struct got_error *
4485 write_tree(struct got_object_id **new_tree_id, int *nentries,
4486 struct got_tree_object *base_tree, const char *path_base_tree,
4487 struct got_pathlist_head *commitable_paths,
4488 got_worktree_status_cb status_cb, void *status_arg,
4489 struct got_repository *repo)
4491 const struct got_error *err = NULL;
4492 struct got_pathlist_head paths;
4493 struct got_tree_entry *te, *new_te = NULL;
4494 struct got_pathlist_entry *pe;
4496 TAILQ_INIT(&paths);
4497 *nentries = 0;
4499 /* Insert, and recurse into, newly added entries first. */
4500 TAILQ_FOREACH(pe, commitable_paths, entry) {
4501 struct got_commitable *ct = pe->data;
4502 char *child_path = NULL, *slash;
4504 if ((ct->status != GOT_STATUS_ADD &&
4505 ct->staged_status != GOT_STATUS_ADD) ||
4506 (ct->flags & GOT_COMMITABLE_ADDED))
4507 continue;
4509 if (!got_path_is_child(pe->path, path_base_tree,
4510 strlen(path_base_tree)))
4511 continue;
4513 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4514 pe->path);
4515 if (err)
4516 goto done;
4518 slash = strchr(child_path, '/');
4519 if (slash == NULL) {
4520 err = alloc_added_blob_tree_entry(&new_te, ct);
4521 if (err)
4522 goto done;
4523 err = report_ct_status(ct, status_cb, status_arg);
4524 if (err)
4525 goto done;
4526 ct->flags |= GOT_COMMITABLE_ADDED;
4527 err = insert_tree_entry(new_te, &paths);
4528 if (err)
4529 goto done;
4530 (*nentries)++;
4531 } else {
4532 *slash = '\0'; /* trim trailing path components */
4533 if (base_tree == NULL ||
4534 got_object_tree_find_entry(base_tree, child_path)
4535 == NULL) {
4536 err = make_subtree_for_added_blob(&new_te,
4537 child_path, path_base_tree,
4538 commitable_paths, status_cb, status_arg,
4539 repo);
4540 if (err)
4541 goto done;
4542 err = insert_tree_entry(new_te, &paths);
4543 if (err)
4544 goto done;
4545 (*nentries)++;
4550 if (base_tree) {
4551 int i, nbase_entries;
4552 /* Handle modified and deleted entries. */
4553 nbase_entries = got_object_tree_get_nentries(base_tree);
4554 for (i = 0; i < nbase_entries; i++) {
4555 struct got_commitable *ct = NULL;
4557 te = got_object_tree_get_entry(base_tree, i);
4558 if (got_object_tree_entry_is_submodule(te)) {
4559 /* Entry is a submodule; just copy it. */
4560 err = got_object_tree_entry_dup(&new_te, te);
4561 if (err)
4562 goto done;
4563 err = insert_tree_entry(new_te, &paths);
4564 if (err)
4565 goto done;
4566 (*nentries)++;
4567 continue;
4570 if (S_ISDIR(te->mode)) {
4571 int modified;
4572 err = got_object_tree_entry_dup(&new_te, te);
4573 if (err)
4574 goto done;
4575 err = match_modified_subtree(&modified, te,
4576 path_base_tree, commitable_paths);
4577 if (err)
4578 goto done;
4579 /* Avoid recursion into unmodified subtrees. */
4580 if (modified) {
4581 struct got_object_id *new_id;
4582 int nsubentries;
4583 err = write_subtree(&new_id,
4584 &nsubentries, te,
4585 path_base_tree, commitable_paths,
4586 status_cb, status_arg, repo);
4587 if (err)
4588 goto done;
4589 if (nsubentries == 0) {
4590 /* All entries were deleted. */
4591 free(new_id);
4592 continue;
4594 memcpy(&new_te->id, new_id,
4595 sizeof(new_te->id));
4596 free(new_id);
4598 err = insert_tree_entry(new_te, &paths);
4599 if (err)
4600 goto done;
4601 (*nentries)++;
4602 continue;
4605 err = match_deleted_or_modified_ct(&ct, te,
4606 path_base_tree, commitable_paths);
4607 if (err)
4608 goto done;
4609 if (ct) {
4610 /* NB: Deleted entries get dropped here. */
4611 if (ct->status == GOT_STATUS_MODIFY ||
4612 ct->status == GOT_STATUS_MODE_CHANGE ||
4613 ct->staged_status == GOT_STATUS_MODIFY) {
4614 err = alloc_modified_blob_tree_entry(
4615 &new_te, te, ct);
4616 if (err)
4617 goto done;
4618 err = insert_tree_entry(new_te, &paths);
4619 if (err)
4620 goto done;
4621 (*nentries)++;
4623 err = report_ct_status(ct, status_cb,
4624 status_arg);
4625 if (err)
4626 goto done;
4627 } else {
4628 /* Entry is unchanged; just copy it. */
4629 err = got_object_tree_entry_dup(&new_te, te);
4630 if (err)
4631 goto done;
4632 err = insert_tree_entry(new_te, &paths);
4633 if (err)
4634 goto done;
4635 (*nentries)++;
4640 /* Write new list of entries; deleted entries have been dropped. */
4641 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4642 done:
4643 got_pathlist_free(&paths);
4644 return err;
4647 static const struct got_error *
4648 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4649 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4650 int have_staged_files)
4652 const struct got_error *err = NULL;
4653 struct got_pathlist_entry *pe;
4655 TAILQ_FOREACH(pe, commitable_paths, entry) {
4656 struct got_fileindex_entry *ie;
4657 struct got_commitable *ct = pe->data;
4659 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4660 if (ie) {
4661 if (ct->status == GOT_STATUS_DELETE ||
4662 ct->staged_status == GOT_STATUS_DELETE) {
4663 got_fileindex_entry_remove(fileindex, ie);
4664 } else if (ct->staged_status == GOT_STATUS_ADD ||
4665 ct->staged_status == GOT_STATUS_MODIFY) {
4666 got_fileindex_entry_stage_set(ie,
4667 GOT_FILEIDX_STAGE_NONE);
4668 err = got_fileindex_entry_update(ie,
4669 ct->ondisk_path, ct->staged_blob_id->sha1,
4670 new_base_commit_id->sha1,
4671 !have_staged_files);
4672 } else
4673 err = got_fileindex_entry_update(ie,
4674 ct->ondisk_path, ct->blob_id->sha1,
4675 new_base_commit_id->sha1,
4676 !have_staged_files);
4677 } else {
4678 err = got_fileindex_entry_alloc(&ie, pe->path);
4679 if (err)
4680 break;
4681 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4682 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4683 if (err) {
4684 got_fileindex_entry_free(ie);
4685 break;
4687 err = got_fileindex_entry_add(fileindex, ie);
4688 if (err) {
4689 got_fileindex_entry_free(ie);
4690 break;
4694 return err;
4698 static const struct got_error *
4699 check_out_of_date(const char *in_repo_path, unsigned char status,
4700 unsigned char staged_status, struct got_object_id *base_blob_id,
4701 struct got_object_id *base_commit_id,
4702 struct got_object_id *head_commit_id, struct got_repository *repo,
4703 int ood_errcode)
4705 const struct got_error *err = NULL;
4706 struct got_object_id *id = NULL;
4708 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4709 /* Trivial case: base commit == head commit */
4710 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4711 return NULL;
4713 * Ensure file content which local changes were based
4714 * on matches file content in the branch head.
4716 err = got_object_id_by_path(&id, repo, head_commit_id,
4717 in_repo_path);
4718 if (err) {
4719 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4720 err = got_error(ood_errcode);
4721 goto done;
4722 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4723 err = got_error(ood_errcode);
4724 } else {
4725 /* Require that added files don't exist in the branch head. */
4726 err = got_object_id_by_path(&id, repo, head_commit_id,
4727 in_repo_path);
4728 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4729 goto done;
4730 err = id ? got_error(ood_errcode) : NULL;
4732 done:
4733 free(id);
4734 return err;
4737 const struct got_error *
4738 commit_worktree(struct got_object_id **new_commit_id,
4739 struct got_pathlist_head *commitable_paths,
4740 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4741 const char *author, const char *committer,
4742 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4743 got_worktree_status_cb status_cb, void *status_arg,
4744 struct got_repository *repo)
4746 const struct got_error *err = NULL, *unlockerr = NULL;
4747 struct got_pathlist_entry *pe;
4748 const char *head_ref_name = NULL;
4749 struct got_commit_object *head_commit = NULL;
4750 struct got_reference *head_ref2 = NULL;
4751 struct got_object_id *head_commit_id2 = NULL;
4752 struct got_tree_object *head_tree = NULL;
4753 struct got_object_id *new_tree_id = NULL;
4754 int nentries;
4755 struct got_object_id_queue parent_ids;
4756 struct got_object_qid *pid = NULL;
4757 char *logmsg = NULL;
4759 *new_commit_id = NULL;
4761 SIMPLEQ_INIT(&parent_ids);
4763 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4764 if (err)
4765 goto done;
4767 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4768 if (err)
4769 goto done;
4771 if (commit_msg_cb != NULL) {
4772 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4773 if (err)
4774 goto done;
4777 if (logmsg == NULL || strlen(logmsg) == 0) {
4778 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4779 goto done;
4782 /* Create blobs from added and modified files and record their IDs. */
4783 TAILQ_FOREACH(pe, commitable_paths, entry) {
4784 struct got_commitable *ct = pe->data;
4785 char *ondisk_path;
4787 /* Blobs for staged files already exist. */
4788 if (ct->staged_status == GOT_STATUS_ADD ||
4789 ct->staged_status == GOT_STATUS_MODIFY)
4790 continue;
4792 if (ct->status != GOT_STATUS_ADD &&
4793 ct->status != GOT_STATUS_MODIFY &&
4794 ct->status != GOT_STATUS_MODE_CHANGE)
4795 continue;
4797 if (asprintf(&ondisk_path, "%s/%s",
4798 worktree->root_path, pe->path) == -1) {
4799 err = got_error_from_errno("asprintf");
4800 goto done;
4802 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4803 free(ondisk_path);
4804 if (err)
4805 goto done;
4808 /* Recursively write new tree objects. */
4809 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4810 commitable_paths, status_cb, status_arg, repo);
4811 if (err)
4812 goto done;
4814 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4815 if (err)
4816 goto done;
4817 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4818 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4819 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4820 got_object_qid_free(pid);
4821 if (logmsg != NULL)
4822 free(logmsg);
4823 if (err)
4824 goto done;
4826 /* Check if a concurrent commit to our branch has occurred. */
4827 head_ref_name = got_worktree_get_head_ref_name(worktree);
4828 if (head_ref_name == NULL) {
4829 err = got_error_from_errno("got_worktree_get_head_ref_name");
4830 goto done;
4832 /* Lock the reference here to prevent concurrent modification. */
4833 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4834 if (err)
4835 goto done;
4836 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4837 if (err)
4838 goto done;
4839 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4840 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4841 goto done;
4843 /* Update branch head in repository. */
4844 err = got_ref_change_ref(head_ref2, *new_commit_id);
4845 if (err)
4846 goto done;
4847 err = got_ref_write(head_ref2, repo);
4848 if (err)
4849 goto done;
4851 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4852 if (err)
4853 goto done;
4855 err = ref_base_commit(worktree, repo);
4856 if (err)
4857 goto done;
4858 done:
4859 if (head_tree)
4860 got_object_tree_close(head_tree);
4861 if (head_commit)
4862 got_object_commit_close(head_commit);
4863 free(head_commit_id2);
4864 if (head_ref2) {
4865 unlockerr = got_ref_unlock(head_ref2);
4866 if (unlockerr && err == NULL)
4867 err = unlockerr;
4868 got_ref_close(head_ref2);
4870 return err;
4873 static const struct got_error *
4874 check_path_is_commitable(const char *path,
4875 struct got_pathlist_head *commitable_paths)
4877 struct got_pathlist_entry *cpe = NULL;
4878 size_t path_len = strlen(path);
4880 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4881 struct got_commitable *ct = cpe->data;
4882 const char *ct_path = ct->path;
4884 while (ct_path[0] == '/')
4885 ct_path++;
4887 if (strcmp(path, ct_path) == 0 ||
4888 got_path_is_child(ct_path, path, path_len))
4889 break;
4892 if (cpe == NULL)
4893 return got_error_path(path, GOT_ERR_BAD_PATH);
4895 return NULL;
4898 static const struct got_error *
4899 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4901 int *have_staged_files = arg;
4903 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4904 *have_staged_files = 1;
4905 return got_error(GOT_ERR_CANCELLED);
4908 return NULL;
4911 static const struct got_error *
4912 check_non_staged_files(struct got_fileindex *fileindex,
4913 struct got_pathlist_head *paths)
4915 struct got_pathlist_entry *pe;
4916 struct got_fileindex_entry *ie;
4918 TAILQ_FOREACH(pe, paths, entry) {
4919 if (pe->path[0] == '\0')
4920 continue;
4921 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4922 if (ie == NULL)
4923 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4924 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4925 return got_error_path(pe->path,
4926 GOT_ERR_FILE_NOT_STAGED);
4929 return NULL;
4932 const struct got_error *
4933 got_worktree_commit(struct got_object_id **new_commit_id,
4934 struct got_worktree *worktree, struct got_pathlist_head *paths,
4935 const char *author, const char *committer,
4936 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4937 got_worktree_status_cb status_cb, void *status_arg,
4938 struct got_repository *repo)
4940 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4941 struct got_fileindex *fileindex = NULL;
4942 char *fileindex_path = NULL;
4943 struct got_pathlist_head commitable_paths;
4944 struct collect_commitables_arg cc_arg;
4945 struct got_pathlist_entry *pe;
4946 struct got_reference *head_ref = NULL;
4947 struct got_object_id *head_commit_id = NULL;
4948 int have_staged_files = 0;
4950 *new_commit_id = NULL;
4952 TAILQ_INIT(&commitable_paths);
4954 err = lock_worktree(worktree, LOCK_EX);
4955 if (err)
4956 goto done;
4958 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4959 if (err)
4960 goto done;
4962 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4963 if (err)
4964 goto done;
4966 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4967 if (err)
4968 goto done;
4970 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4971 &have_staged_files);
4972 if (err && err->code != GOT_ERR_CANCELLED)
4973 goto done;
4974 if (have_staged_files) {
4975 err = check_non_staged_files(fileindex, paths);
4976 if (err)
4977 goto done;
4980 cc_arg.commitable_paths = &commitable_paths;
4981 cc_arg.worktree = worktree;
4982 cc_arg.repo = repo;
4983 cc_arg.have_staged_files = have_staged_files;
4984 TAILQ_FOREACH(pe, paths, entry) {
4985 err = worktree_status(worktree, pe->path, fileindex, repo,
4986 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4987 if (err)
4988 goto done;
4991 if (TAILQ_EMPTY(&commitable_paths)) {
4992 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4993 goto done;
4996 TAILQ_FOREACH(pe, paths, entry) {
4997 err = check_path_is_commitable(pe->path, &commitable_paths);
4998 if (err)
4999 goto done;
5002 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5003 struct got_commitable *ct = pe->data;
5004 const char *ct_path = ct->in_repo_path;
5006 while (ct_path[0] == '/')
5007 ct_path++;
5008 err = check_out_of_date(ct_path, ct->status,
5009 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5010 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5011 if (err)
5012 goto done;
5016 err = commit_worktree(new_commit_id, &commitable_paths,
5017 head_commit_id, worktree, author, committer,
5018 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5019 if (err)
5020 goto done;
5022 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5023 fileindex, have_staged_files);
5024 sync_err = sync_fileindex(fileindex, fileindex_path);
5025 if (sync_err && err == NULL)
5026 err = sync_err;
5027 done:
5028 if (fileindex)
5029 got_fileindex_free(fileindex);
5030 free(fileindex_path);
5031 unlockerr = lock_worktree(worktree, LOCK_SH);
5032 if (unlockerr && err == NULL)
5033 err = unlockerr;
5034 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5035 struct got_commitable *ct = pe->data;
5036 free_commitable(ct);
5038 got_pathlist_free(&commitable_paths);
5039 return err;
5042 const char *
5043 got_commitable_get_path(struct got_commitable *ct)
5045 return ct->path;
5048 unsigned int
5049 got_commitable_get_status(struct got_commitable *ct)
5051 return ct->status;
5054 struct check_rebase_ok_arg {
5055 struct got_worktree *worktree;
5056 struct got_repository *repo;
5059 static const struct got_error *
5060 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5062 const struct got_error *err = NULL;
5063 struct check_rebase_ok_arg *a = arg;
5064 unsigned char status;
5065 struct stat sb;
5066 char *ondisk_path;
5068 /* Reject rebase of a work tree with mixed base commits. */
5069 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5070 SHA1_DIGEST_LENGTH))
5071 return got_error(GOT_ERR_MIXED_COMMITS);
5073 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5074 == -1)
5075 return got_error_from_errno("asprintf");
5077 /* Reject rebase of a work tree with modified or staged files. */
5078 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5079 free(ondisk_path);
5080 if (err)
5081 return err;
5083 if (status != GOT_STATUS_NO_CHANGE)
5084 return got_error(GOT_ERR_MODIFIED);
5085 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5086 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5088 return NULL;
5091 const struct got_error *
5092 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5093 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5094 struct got_worktree *worktree, struct got_reference *branch,
5095 struct got_repository *repo)
5097 const struct got_error *err = NULL;
5098 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5099 char *branch_ref_name = NULL;
5100 char *fileindex_path = NULL;
5101 struct check_rebase_ok_arg ok_arg;
5102 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5103 struct got_object_id *wt_branch_tip = NULL;
5105 *new_base_branch_ref = NULL;
5106 *tmp_branch = NULL;
5107 *fileindex = NULL;
5109 err = lock_worktree(worktree, LOCK_EX);
5110 if (err)
5111 return err;
5113 err = open_fileindex(fileindex, &fileindex_path, worktree);
5114 if (err)
5115 goto done;
5117 ok_arg.worktree = worktree;
5118 ok_arg.repo = repo;
5119 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5120 &ok_arg);
5121 if (err)
5122 goto done;
5124 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5125 if (err)
5126 goto done;
5128 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5129 if (err)
5130 goto done;
5132 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5133 if (err)
5134 goto done;
5136 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5137 0);
5138 if (err)
5139 goto done;
5141 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5142 if (err)
5143 goto done;
5144 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5145 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5146 goto done;
5149 err = got_ref_alloc_symref(new_base_branch_ref,
5150 new_base_branch_ref_name, wt_branch);
5151 if (err)
5152 goto done;
5153 err = got_ref_write(*new_base_branch_ref, repo);
5154 if (err)
5155 goto done;
5157 /* TODO Lock original branch's ref while rebasing? */
5159 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5160 if (err)
5161 goto done;
5163 err = got_ref_write(branch_ref, repo);
5164 if (err)
5165 goto done;
5167 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5168 worktree->base_commit_id);
5169 if (err)
5170 goto done;
5171 err = got_ref_write(*tmp_branch, repo);
5172 if (err)
5173 goto done;
5175 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5176 if (err)
5177 goto done;
5178 done:
5179 free(fileindex_path);
5180 free(tmp_branch_name);
5181 free(new_base_branch_ref_name);
5182 free(branch_ref_name);
5183 if (branch_ref)
5184 got_ref_close(branch_ref);
5185 if (wt_branch)
5186 got_ref_close(wt_branch);
5187 free(wt_branch_tip);
5188 if (err) {
5189 if (*new_base_branch_ref) {
5190 got_ref_close(*new_base_branch_ref);
5191 *new_base_branch_ref = NULL;
5193 if (*tmp_branch) {
5194 got_ref_close(*tmp_branch);
5195 *tmp_branch = NULL;
5197 if (*fileindex) {
5198 got_fileindex_free(*fileindex);
5199 *fileindex = NULL;
5201 lock_worktree(worktree, LOCK_SH);
5203 return err;
5206 const struct got_error *
5207 got_worktree_rebase_continue(struct got_object_id **commit_id,
5208 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5209 struct got_reference **branch, struct got_fileindex **fileindex,
5210 struct got_worktree *worktree, struct got_repository *repo)
5212 const struct got_error *err;
5213 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5214 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5215 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5216 char *fileindex_path = NULL;
5217 int have_staged_files = 0;
5219 *commit_id = NULL;
5220 *new_base_branch = NULL;
5221 *tmp_branch = NULL;
5222 *branch = NULL;
5223 *fileindex = NULL;
5225 err = lock_worktree(worktree, LOCK_EX);
5226 if (err)
5227 return err;
5229 err = open_fileindex(fileindex, &fileindex_path, worktree);
5230 if (err)
5231 goto done;
5233 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5234 &have_staged_files);
5235 if (err && err->code != GOT_ERR_CANCELLED)
5236 goto done;
5237 if (have_staged_files) {
5238 err = got_error(GOT_ERR_STAGED_PATHS);
5239 goto done;
5242 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5243 if (err)
5244 goto done;
5246 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5247 if (err)
5248 goto done;
5250 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5251 if (err)
5252 goto done;
5254 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5255 if (err)
5256 goto done;
5258 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5259 if (err)
5260 goto done;
5262 err = got_ref_open(branch, repo,
5263 got_ref_get_symref_target(branch_ref), 0);
5264 if (err)
5265 goto done;
5267 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5268 if (err)
5269 goto done;
5271 err = got_ref_resolve(commit_id, repo, commit_ref);
5272 if (err)
5273 goto done;
5275 err = got_ref_open(new_base_branch, repo,
5276 new_base_branch_ref_name, 0);
5277 if (err)
5278 goto done;
5280 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5281 if (err)
5282 goto done;
5283 done:
5284 free(commit_ref_name);
5285 free(branch_ref_name);
5286 free(fileindex_path);
5287 if (commit_ref)
5288 got_ref_close(commit_ref);
5289 if (branch_ref)
5290 got_ref_close(branch_ref);
5291 if (err) {
5292 free(*commit_id);
5293 *commit_id = NULL;
5294 if (*tmp_branch) {
5295 got_ref_close(*tmp_branch);
5296 *tmp_branch = NULL;
5298 if (*new_base_branch) {
5299 got_ref_close(*new_base_branch);
5300 *new_base_branch = NULL;
5302 if (*branch) {
5303 got_ref_close(*branch);
5304 *branch = NULL;
5306 if (*fileindex) {
5307 got_fileindex_free(*fileindex);
5308 *fileindex = NULL;
5310 lock_worktree(worktree, LOCK_SH);
5312 return err;
5315 const struct got_error *
5316 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5318 const struct got_error *err;
5319 char *tmp_branch_name = NULL;
5321 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5322 if (err)
5323 return err;
5325 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5326 free(tmp_branch_name);
5327 return NULL;
5330 static const struct got_error *
5331 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5332 char **logmsg, void *arg)
5334 *logmsg = arg;
5335 return NULL;
5338 static const struct got_error *
5339 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5340 const char *path, struct got_object_id *blob_id,
5341 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5342 int dirfd, const char *de_name)
5344 return NULL;
5347 struct collect_merged_paths_arg {
5348 got_worktree_checkout_cb progress_cb;
5349 void *progress_arg;
5350 struct got_pathlist_head *merged_paths;
5353 static const struct got_error *
5354 collect_merged_paths(void *arg, unsigned char status, const char *path)
5356 const struct got_error *err;
5357 struct collect_merged_paths_arg *a = arg;
5358 char *p;
5359 struct got_pathlist_entry *new;
5361 err = (*a->progress_cb)(a->progress_arg, status, path);
5362 if (err)
5363 return err;
5365 if (status != GOT_STATUS_MERGE &&
5366 status != GOT_STATUS_ADD &&
5367 status != GOT_STATUS_DELETE &&
5368 status != GOT_STATUS_CONFLICT)
5369 return NULL;
5371 p = strdup(path);
5372 if (p == NULL)
5373 return got_error_from_errno("strdup");
5375 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5376 if (err || new == NULL)
5377 free(p);
5378 return err;
5381 void
5382 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5384 struct got_pathlist_entry *pe;
5386 TAILQ_FOREACH(pe, merged_paths, entry)
5387 free((char *)pe->path);
5389 got_pathlist_free(merged_paths);
5392 static const struct got_error *
5393 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5394 int is_rebase, struct got_repository *repo)
5396 const struct got_error *err;
5397 struct got_reference *commit_ref = NULL;
5399 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5400 if (err) {
5401 if (err->code != GOT_ERR_NOT_REF)
5402 goto done;
5403 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5404 if (err)
5405 goto done;
5406 err = got_ref_write(commit_ref, repo);
5407 if (err)
5408 goto done;
5409 } else if (is_rebase) {
5410 struct got_object_id *stored_id;
5411 int cmp;
5413 err = got_ref_resolve(&stored_id, repo, commit_ref);
5414 if (err)
5415 goto done;
5416 cmp = got_object_id_cmp(commit_id, stored_id);
5417 free(stored_id);
5418 if (cmp != 0) {
5419 err = got_error(GOT_ERR_REBASE_COMMITID);
5420 goto done;
5423 done:
5424 if (commit_ref)
5425 got_ref_close(commit_ref);
5426 return err;
5429 static const struct got_error *
5430 rebase_merge_files(struct got_pathlist_head *merged_paths,
5431 const char *commit_ref_name, struct got_worktree *worktree,
5432 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5433 struct got_object_id *commit_id, struct got_repository *repo,
5434 got_worktree_checkout_cb progress_cb, void *progress_arg,
5435 got_cancel_cb cancel_cb, void *cancel_arg)
5437 const struct got_error *err;
5438 struct got_reference *commit_ref = NULL;
5439 struct collect_merged_paths_arg cmp_arg;
5440 char *fileindex_path;
5442 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5444 err = get_fileindex_path(&fileindex_path, worktree);
5445 if (err)
5446 return err;
5448 cmp_arg.progress_cb = progress_cb;
5449 cmp_arg.progress_arg = progress_arg;
5450 cmp_arg.merged_paths = merged_paths;
5451 err = merge_files(worktree, fileindex, fileindex_path,
5452 parent_commit_id, commit_id, repo, collect_merged_paths,
5453 &cmp_arg, cancel_cb, cancel_arg);
5454 if (commit_ref)
5455 got_ref_close(commit_ref);
5456 return err;
5459 const struct got_error *
5460 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5461 struct got_worktree *worktree, struct got_fileindex *fileindex,
5462 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5463 struct got_repository *repo,
5464 got_worktree_checkout_cb progress_cb, void *progress_arg,
5465 got_cancel_cb cancel_cb, void *cancel_arg)
5467 const struct got_error *err;
5468 char *commit_ref_name;
5470 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5471 if (err)
5472 return err;
5474 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5475 if (err)
5476 goto done;
5478 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5479 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5480 progress_arg, cancel_cb, cancel_arg);
5481 done:
5482 free(commit_ref_name);
5483 return err;
5486 const struct got_error *
5487 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5488 struct got_worktree *worktree, struct got_fileindex *fileindex,
5489 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5490 struct got_repository *repo,
5491 got_worktree_checkout_cb progress_cb, void *progress_arg,
5492 got_cancel_cb cancel_cb, void *cancel_arg)
5494 const struct got_error *err;
5495 char *commit_ref_name;
5497 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5498 if (err)
5499 return err;
5501 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5502 if (err)
5503 goto done;
5505 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5506 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5507 progress_arg, cancel_cb, cancel_arg);
5508 done:
5509 free(commit_ref_name);
5510 return err;
5513 static const struct got_error *
5514 rebase_commit(struct got_object_id **new_commit_id,
5515 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5516 struct got_worktree *worktree, struct got_fileindex *fileindex,
5517 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5518 const char *new_logmsg, struct got_repository *repo)
5520 const struct got_error *err, *sync_err;
5521 struct got_pathlist_head commitable_paths;
5522 struct collect_commitables_arg cc_arg;
5523 char *fileindex_path = NULL;
5524 struct got_reference *head_ref = NULL;
5525 struct got_object_id *head_commit_id = NULL;
5526 char *logmsg = NULL;
5528 TAILQ_INIT(&commitable_paths);
5529 *new_commit_id = NULL;
5531 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5533 err = get_fileindex_path(&fileindex_path, worktree);
5534 if (err)
5535 return err;
5537 cc_arg.commitable_paths = &commitable_paths;
5538 cc_arg.worktree = worktree;
5539 cc_arg.repo = repo;
5540 cc_arg.have_staged_files = 0;
5542 * If possible get the status of individual files directly to
5543 * avoid crawling the entire work tree once per rebased commit.
5544 * TODO: Ideally, merged_paths would contain a list of commitables
5545 * we could use so we could skip worktree_status() entirely.
5547 if (merged_paths) {
5548 struct got_pathlist_entry *pe;
5549 TAILQ_FOREACH(pe, merged_paths, entry) {
5550 err = worktree_status(worktree, pe->path, fileindex,
5551 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5552 0);
5553 if (err)
5554 goto done;
5556 } else {
5557 err = worktree_status(worktree, "", fileindex, repo,
5558 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5559 if (err)
5560 goto done;
5563 if (TAILQ_EMPTY(&commitable_paths)) {
5564 /* No-op change; commit will be elided. */
5565 err = got_ref_delete(commit_ref, repo);
5566 if (err)
5567 goto done;
5568 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5569 goto done;
5572 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5573 if (err)
5574 goto done;
5576 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5577 if (err)
5578 goto done;
5580 if (new_logmsg) {
5581 logmsg = strdup(new_logmsg);
5582 if (logmsg == NULL) {
5583 err = got_error_from_errno("strdup");
5584 goto done;
5586 } else {
5587 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5588 if (err)
5589 goto done;
5592 /* NB: commit_worktree will call free(logmsg) */
5593 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5594 worktree, got_object_commit_get_author(orig_commit),
5595 got_object_commit_get_committer(orig_commit),
5596 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5597 if (err)
5598 goto done;
5600 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5601 if (err)
5602 goto done;
5604 err = got_ref_delete(commit_ref, repo);
5605 if (err)
5606 goto done;
5608 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5609 fileindex, 0);
5610 sync_err = sync_fileindex(fileindex, fileindex_path);
5611 if (sync_err && err == NULL)
5612 err = sync_err;
5613 done:
5614 free(fileindex_path);
5615 free(head_commit_id);
5616 if (head_ref)
5617 got_ref_close(head_ref);
5618 if (err) {
5619 free(*new_commit_id);
5620 *new_commit_id = NULL;
5622 return err;
5625 const struct got_error *
5626 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5627 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5628 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5629 struct got_commit_object *orig_commit,
5630 struct got_object_id *orig_commit_id, struct got_repository *repo)
5632 const struct got_error *err;
5633 char *commit_ref_name;
5634 struct got_reference *commit_ref = NULL;
5635 struct got_object_id *commit_id = NULL;
5637 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5638 if (err)
5639 return err;
5641 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5642 if (err)
5643 goto done;
5644 err = got_ref_resolve(&commit_id, repo, commit_ref);
5645 if (err)
5646 goto done;
5647 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5648 err = got_error(GOT_ERR_REBASE_COMMITID);
5649 goto done;
5652 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5653 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5654 done:
5655 if (commit_ref)
5656 got_ref_close(commit_ref);
5657 free(commit_ref_name);
5658 free(commit_id);
5659 return err;
5662 const struct got_error *
5663 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5664 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5665 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5666 struct got_commit_object *orig_commit,
5667 struct got_object_id *orig_commit_id, const char *new_logmsg,
5668 struct got_repository *repo)
5670 const struct got_error *err;
5671 char *commit_ref_name;
5672 struct got_reference *commit_ref = NULL;
5674 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5675 if (err)
5676 return err;
5678 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5679 if (err)
5680 goto done;
5682 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5683 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5684 done:
5685 if (commit_ref)
5686 got_ref_close(commit_ref);
5687 free(commit_ref_name);
5688 return err;
5691 const struct got_error *
5692 got_worktree_rebase_postpone(struct got_worktree *worktree,
5693 struct got_fileindex *fileindex)
5695 if (fileindex)
5696 got_fileindex_free(fileindex);
5697 return lock_worktree(worktree, LOCK_SH);
5700 static const struct got_error *
5701 delete_ref(const char *name, struct got_repository *repo)
5703 const struct got_error *err;
5704 struct got_reference *ref;
5706 err = got_ref_open(&ref, repo, name, 0);
5707 if (err) {
5708 if (err->code == GOT_ERR_NOT_REF)
5709 return NULL;
5710 return err;
5713 err = got_ref_delete(ref, repo);
5714 got_ref_close(ref);
5715 return err;
5718 static const struct got_error *
5719 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5721 const struct got_error *err;
5722 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5723 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5725 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5726 if (err)
5727 goto done;
5728 err = delete_ref(tmp_branch_name, repo);
5729 if (err)
5730 goto done;
5732 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5733 if (err)
5734 goto done;
5735 err = delete_ref(new_base_branch_ref_name, repo);
5736 if (err)
5737 goto done;
5739 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5740 if (err)
5741 goto done;
5742 err = delete_ref(branch_ref_name, repo);
5743 if (err)
5744 goto done;
5746 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5747 if (err)
5748 goto done;
5749 err = delete_ref(commit_ref_name, repo);
5750 if (err)
5751 goto done;
5753 done:
5754 free(tmp_branch_name);
5755 free(new_base_branch_ref_name);
5756 free(branch_ref_name);
5757 free(commit_ref_name);
5758 return err;
5761 const struct got_error *
5762 got_worktree_rebase_complete(struct got_worktree *worktree,
5763 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5764 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5765 struct got_repository *repo)
5767 const struct got_error *err, *unlockerr;
5768 struct got_object_id *new_head_commit_id = NULL;
5770 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5771 if (err)
5772 return err;
5774 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5775 if (err)
5776 goto done;
5778 err = got_ref_write(rebased_branch, repo);
5779 if (err)
5780 goto done;
5782 err = got_worktree_set_head_ref(worktree, rebased_branch);
5783 if (err)
5784 goto done;
5786 err = delete_rebase_refs(worktree, repo);
5787 done:
5788 if (fileindex)
5789 got_fileindex_free(fileindex);
5790 free(new_head_commit_id);
5791 unlockerr = lock_worktree(worktree, LOCK_SH);
5792 if (unlockerr && err == NULL)
5793 err = unlockerr;
5794 return err;
5797 const struct got_error *
5798 got_worktree_rebase_abort(struct got_worktree *worktree,
5799 struct got_fileindex *fileindex, struct got_repository *repo,
5800 struct got_reference *new_base_branch,
5801 got_worktree_checkout_cb progress_cb, void *progress_arg)
5803 const struct got_error *err, *unlockerr, *sync_err;
5804 struct got_reference *resolved = NULL;
5805 struct got_object_id *commit_id = NULL;
5806 char *fileindex_path = NULL;
5807 struct revert_file_args rfa;
5808 struct got_object_id *tree_id = NULL;
5810 err = lock_worktree(worktree, LOCK_EX);
5811 if (err)
5812 return err;
5814 err = got_ref_open(&resolved, repo,
5815 got_ref_get_symref_target(new_base_branch), 0);
5816 if (err)
5817 goto done;
5819 err = got_worktree_set_head_ref(worktree, resolved);
5820 if (err)
5821 goto done;
5824 * XXX commits to the base branch could have happened while
5825 * we were busy rebasing; should we store the original commit ID
5826 * when rebase begins and read it back here?
5828 err = got_ref_resolve(&commit_id, repo, resolved);
5829 if (err)
5830 goto done;
5832 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5833 if (err)
5834 goto done;
5836 err = got_object_id_by_path(&tree_id, repo,
5837 worktree->base_commit_id, worktree->path_prefix);
5838 if (err)
5839 goto done;
5841 err = delete_rebase_refs(worktree, repo);
5842 if (err)
5843 goto done;
5845 err = get_fileindex_path(&fileindex_path, worktree);
5846 if (err)
5847 goto done;
5849 rfa.worktree = worktree;
5850 rfa.fileindex = fileindex;
5851 rfa.progress_cb = progress_cb;
5852 rfa.progress_arg = progress_arg;
5853 rfa.patch_cb = NULL;
5854 rfa.patch_arg = NULL;
5855 rfa.repo = repo;
5856 err = worktree_status(worktree, "", fileindex, repo,
5857 revert_file, &rfa, NULL, NULL, 0, 0);
5858 if (err)
5859 goto sync;
5861 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5862 repo, progress_cb, progress_arg, NULL, NULL);
5863 sync:
5864 sync_err = sync_fileindex(fileindex, fileindex_path);
5865 if (sync_err && err == NULL)
5866 err = sync_err;
5867 done:
5868 got_ref_close(resolved);
5869 free(tree_id);
5870 free(commit_id);
5871 if (fileindex)
5872 got_fileindex_free(fileindex);
5873 free(fileindex_path);
5875 unlockerr = lock_worktree(worktree, LOCK_SH);
5876 if (unlockerr && err == NULL)
5877 err = unlockerr;
5878 return err;
5881 const struct got_error *
5882 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5883 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5884 struct got_fileindex **fileindex, struct got_worktree *worktree,
5885 struct got_repository *repo)
5887 const struct got_error *err = NULL;
5888 char *tmp_branch_name = NULL;
5889 char *branch_ref_name = NULL;
5890 char *base_commit_ref_name = NULL;
5891 char *fileindex_path = NULL;
5892 struct check_rebase_ok_arg ok_arg;
5893 struct got_reference *wt_branch = NULL;
5894 struct got_reference *base_commit_ref = NULL;
5896 *tmp_branch = NULL;
5897 *branch_ref = NULL;
5898 *base_commit_id = NULL;
5899 *fileindex = NULL;
5901 err = lock_worktree(worktree, LOCK_EX);
5902 if (err)
5903 return err;
5905 err = open_fileindex(fileindex, &fileindex_path, worktree);
5906 if (err)
5907 goto done;
5909 ok_arg.worktree = worktree;
5910 ok_arg.repo = repo;
5911 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5912 &ok_arg);
5913 if (err)
5914 goto done;
5916 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5917 if (err)
5918 goto done;
5920 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5921 if (err)
5922 goto done;
5924 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5925 worktree);
5926 if (err)
5927 goto done;
5929 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5930 0);
5931 if (err)
5932 goto done;
5934 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5935 if (err)
5936 goto done;
5938 err = got_ref_write(*branch_ref, repo);
5939 if (err)
5940 goto done;
5942 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5943 worktree->base_commit_id);
5944 if (err)
5945 goto done;
5946 err = got_ref_write(base_commit_ref, repo);
5947 if (err)
5948 goto done;
5949 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5950 if (*base_commit_id == NULL) {
5951 err = got_error_from_errno("got_object_id_dup");
5952 goto done;
5955 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5956 worktree->base_commit_id);
5957 if (err)
5958 goto done;
5959 err = got_ref_write(*tmp_branch, repo);
5960 if (err)
5961 goto done;
5963 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5964 if (err)
5965 goto done;
5966 done:
5967 free(fileindex_path);
5968 free(tmp_branch_name);
5969 free(branch_ref_name);
5970 free(base_commit_ref_name);
5971 if (wt_branch)
5972 got_ref_close(wt_branch);
5973 if (err) {
5974 if (*branch_ref) {
5975 got_ref_close(*branch_ref);
5976 *branch_ref = NULL;
5978 if (*tmp_branch) {
5979 got_ref_close(*tmp_branch);
5980 *tmp_branch = NULL;
5982 free(*base_commit_id);
5983 if (*fileindex) {
5984 got_fileindex_free(*fileindex);
5985 *fileindex = NULL;
5987 lock_worktree(worktree, LOCK_SH);
5989 return err;
5992 const struct got_error *
5993 got_worktree_histedit_postpone(struct got_worktree *worktree,
5994 struct got_fileindex *fileindex)
5996 if (fileindex)
5997 got_fileindex_free(fileindex);
5998 return lock_worktree(worktree, LOCK_SH);
6001 const struct got_error *
6002 got_worktree_histedit_in_progress(int *in_progress,
6003 struct got_worktree *worktree)
6005 const struct got_error *err;
6006 char *tmp_branch_name = NULL;
6008 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6009 if (err)
6010 return err;
6012 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6013 free(tmp_branch_name);
6014 return NULL;
6017 const struct got_error *
6018 got_worktree_histedit_continue(struct got_object_id **commit_id,
6019 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6020 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6021 struct got_worktree *worktree, struct got_repository *repo)
6023 const struct got_error *err;
6024 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6025 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6026 struct got_reference *commit_ref = NULL;
6027 struct got_reference *base_commit_ref = NULL;
6028 char *fileindex_path = NULL;
6029 int have_staged_files = 0;
6031 *commit_id = NULL;
6032 *tmp_branch = NULL;
6033 *base_commit_id = NULL;
6034 *fileindex = NULL;
6036 err = lock_worktree(worktree, LOCK_EX);
6037 if (err)
6038 return err;
6040 err = open_fileindex(fileindex, &fileindex_path, worktree);
6041 if (err)
6042 goto done;
6044 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6045 &have_staged_files);
6046 if (err && err->code != GOT_ERR_CANCELLED)
6047 goto done;
6048 if (have_staged_files) {
6049 err = got_error(GOT_ERR_STAGED_PATHS);
6050 goto done;
6053 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6054 if (err)
6055 goto done;
6057 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6058 if (err)
6059 goto done;
6061 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6062 if (err)
6063 goto done;
6065 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6066 worktree);
6067 if (err)
6068 goto done;
6070 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6071 if (err)
6072 goto done;
6074 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6075 if (err)
6076 goto done;
6077 err = got_ref_resolve(commit_id, repo, commit_ref);
6078 if (err)
6079 goto done;
6081 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6082 if (err)
6083 goto done;
6084 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6085 if (err)
6086 goto done;
6088 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6089 if (err)
6090 goto done;
6091 done:
6092 free(commit_ref_name);
6093 free(branch_ref_name);
6094 free(fileindex_path);
6095 if (commit_ref)
6096 got_ref_close(commit_ref);
6097 if (base_commit_ref)
6098 got_ref_close(base_commit_ref);
6099 if (err) {
6100 free(*commit_id);
6101 *commit_id = NULL;
6102 free(*base_commit_id);
6103 *base_commit_id = NULL;
6104 if (*tmp_branch) {
6105 got_ref_close(*tmp_branch);
6106 *tmp_branch = NULL;
6108 if (*fileindex) {
6109 got_fileindex_free(*fileindex);
6110 *fileindex = NULL;
6112 lock_worktree(worktree, LOCK_EX);
6114 return err;
6117 static const struct got_error *
6118 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6120 const struct got_error *err;
6121 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6122 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6124 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6125 if (err)
6126 goto done;
6127 err = delete_ref(tmp_branch_name, repo);
6128 if (err)
6129 goto done;
6131 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6132 worktree);
6133 if (err)
6134 goto done;
6135 err = delete_ref(base_commit_ref_name, repo);
6136 if (err)
6137 goto done;
6139 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6140 if (err)
6141 goto done;
6142 err = delete_ref(branch_ref_name, repo);
6143 if (err)
6144 goto done;
6146 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6147 if (err)
6148 goto done;
6149 err = delete_ref(commit_ref_name, repo);
6150 if (err)
6151 goto done;
6152 done:
6153 free(tmp_branch_name);
6154 free(base_commit_ref_name);
6155 free(branch_ref_name);
6156 free(commit_ref_name);
6157 return err;
6160 const struct got_error *
6161 got_worktree_histedit_abort(struct got_worktree *worktree,
6162 struct got_fileindex *fileindex, struct got_repository *repo,
6163 struct got_reference *branch, struct got_object_id *base_commit_id,
6164 got_worktree_checkout_cb progress_cb, void *progress_arg)
6166 const struct got_error *err, *unlockerr, *sync_err;
6167 struct got_reference *resolved = NULL;
6168 char *fileindex_path = NULL;
6169 struct got_object_id *tree_id = NULL;
6170 struct revert_file_args rfa;
6172 err = lock_worktree(worktree, LOCK_EX);
6173 if (err)
6174 return err;
6176 err = got_ref_open(&resolved, repo,
6177 got_ref_get_symref_target(branch), 0);
6178 if (err)
6179 goto done;
6181 err = got_worktree_set_head_ref(worktree, resolved);
6182 if (err)
6183 goto done;
6185 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6186 if (err)
6187 goto done;
6189 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6190 worktree->path_prefix);
6191 if (err)
6192 goto done;
6194 err = delete_histedit_refs(worktree, repo);
6195 if (err)
6196 goto done;
6198 err = get_fileindex_path(&fileindex_path, worktree);
6199 if (err)
6200 goto done;
6202 rfa.worktree = worktree;
6203 rfa.fileindex = fileindex;
6204 rfa.progress_cb = progress_cb;
6205 rfa.progress_arg = progress_arg;
6206 rfa.patch_cb = NULL;
6207 rfa.patch_arg = NULL;
6208 rfa.repo = repo;
6209 err = worktree_status(worktree, "", fileindex, repo,
6210 revert_file, &rfa, NULL, NULL, 0, 0);
6211 if (err)
6212 goto sync;
6214 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6215 repo, progress_cb, progress_arg, NULL, NULL);
6216 sync:
6217 sync_err = sync_fileindex(fileindex, fileindex_path);
6218 if (sync_err && err == NULL)
6219 err = sync_err;
6220 done:
6221 got_ref_close(resolved);
6222 free(tree_id);
6223 free(fileindex_path);
6225 unlockerr = lock_worktree(worktree, LOCK_SH);
6226 if (unlockerr && err == NULL)
6227 err = unlockerr;
6228 return err;
6231 const struct got_error *
6232 got_worktree_histedit_complete(struct got_worktree *worktree,
6233 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6234 struct got_reference *edited_branch, struct got_repository *repo)
6236 const struct got_error *err, *unlockerr;
6237 struct got_object_id *new_head_commit_id = NULL;
6238 struct got_reference *resolved = NULL;
6240 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6241 if (err)
6242 return err;
6244 err = got_ref_open(&resolved, repo,
6245 got_ref_get_symref_target(edited_branch), 0);
6246 if (err)
6247 goto done;
6249 err = got_ref_change_ref(resolved, new_head_commit_id);
6250 if (err)
6251 goto done;
6253 err = got_ref_write(resolved, repo);
6254 if (err)
6255 goto done;
6257 err = got_worktree_set_head_ref(worktree, resolved);
6258 if (err)
6259 goto done;
6261 err = delete_histedit_refs(worktree, repo);
6262 done:
6263 if (fileindex)
6264 got_fileindex_free(fileindex);
6265 free(new_head_commit_id);
6266 unlockerr = lock_worktree(worktree, LOCK_SH);
6267 if (unlockerr && err == NULL)
6268 err = unlockerr;
6269 return err;
6272 const struct got_error *
6273 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6274 struct got_object_id *commit_id, struct got_repository *repo)
6276 const struct got_error *err;
6277 char *commit_ref_name;
6279 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6280 if (err)
6281 return err;
6283 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6284 if (err)
6285 goto done;
6287 err = delete_ref(commit_ref_name, repo);
6288 done:
6289 free(commit_ref_name);
6290 return err;
6293 const struct got_error *
6294 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6295 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6296 struct got_worktree *worktree, const char *refname,
6297 struct got_repository *repo)
6299 const struct got_error *err = NULL;
6300 char *fileindex_path = NULL;
6301 struct check_rebase_ok_arg ok_arg;
6303 *fileindex = NULL;
6304 *branch_ref = NULL;
6305 *base_branch_ref = NULL;
6307 err = lock_worktree(worktree, LOCK_EX);
6308 if (err)
6309 return err;
6311 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6312 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6313 "cannot integrate a branch into itself; "
6314 "update -b or different branch name required");
6315 goto done;
6318 err = open_fileindex(fileindex, &fileindex_path, worktree);
6319 if (err)
6320 goto done;
6322 /* Preconditions are the same as for rebase. */
6323 ok_arg.worktree = worktree;
6324 ok_arg.repo = repo;
6325 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6326 &ok_arg);
6327 if (err)
6328 goto done;
6330 err = got_ref_open(branch_ref, repo, refname, 1);
6331 if (err)
6332 goto done;
6334 err = got_ref_open(base_branch_ref, repo,
6335 got_worktree_get_head_ref_name(worktree), 1);
6336 done:
6337 if (err) {
6338 if (*branch_ref) {
6339 got_ref_close(*branch_ref);
6340 *branch_ref = NULL;
6342 if (*base_branch_ref) {
6343 got_ref_close(*base_branch_ref);
6344 *base_branch_ref = NULL;
6346 if (*fileindex) {
6347 got_fileindex_free(*fileindex);
6348 *fileindex = NULL;
6350 lock_worktree(worktree, LOCK_SH);
6352 return err;
6355 const struct got_error *
6356 got_worktree_integrate_continue(struct got_worktree *worktree,
6357 struct got_fileindex *fileindex, struct got_repository *repo,
6358 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6359 got_worktree_checkout_cb progress_cb, void *progress_arg,
6360 got_cancel_cb cancel_cb, void *cancel_arg)
6362 const struct got_error *err = NULL, *sync_err, *unlockerr;
6363 char *fileindex_path = NULL;
6364 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6366 err = get_fileindex_path(&fileindex_path, worktree);
6367 if (err)
6368 goto done;
6370 err = got_ref_resolve(&commit_id, repo, branch_ref);
6371 if (err)
6372 goto done;
6374 err = got_object_id_by_path(&tree_id, repo, commit_id,
6375 worktree->path_prefix);
6376 if (err)
6377 goto done;
6379 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6380 if (err)
6381 goto done;
6383 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6384 progress_cb, progress_arg, cancel_cb, cancel_arg);
6385 if (err)
6386 goto sync;
6388 err = got_ref_change_ref(base_branch_ref, commit_id);
6389 if (err)
6390 goto sync;
6392 err = got_ref_write(base_branch_ref, repo);
6393 sync:
6394 sync_err = sync_fileindex(fileindex, fileindex_path);
6395 if (sync_err && err == NULL)
6396 err = sync_err;
6398 done:
6399 unlockerr = got_ref_unlock(branch_ref);
6400 if (unlockerr && err == NULL)
6401 err = unlockerr;
6402 got_ref_close(branch_ref);
6404 unlockerr = got_ref_unlock(base_branch_ref);
6405 if (unlockerr && err == NULL)
6406 err = unlockerr;
6407 got_ref_close(base_branch_ref);
6409 got_fileindex_free(fileindex);
6410 free(fileindex_path);
6411 free(tree_id);
6413 unlockerr = lock_worktree(worktree, LOCK_SH);
6414 if (unlockerr && err == NULL)
6415 err = unlockerr;
6416 return err;
6419 const struct got_error *
6420 got_worktree_integrate_abort(struct got_worktree *worktree,
6421 struct got_fileindex *fileindex, struct got_repository *repo,
6422 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6424 const struct got_error *err = NULL, *unlockerr = NULL;
6426 got_fileindex_free(fileindex);
6428 err = lock_worktree(worktree, LOCK_SH);
6430 unlockerr = got_ref_unlock(branch_ref);
6431 if (unlockerr && err == NULL)
6432 err = unlockerr;
6433 got_ref_close(branch_ref);
6435 unlockerr = got_ref_unlock(base_branch_ref);
6436 if (unlockerr && err == NULL)
6437 err = unlockerr;
6438 got_ref_close(base_branch_ref);
6440 return err;
6443 struct check_stage_ok_arg {
6444 struct got_object_id *head_commit_id;
6445 struct got_worktree *worktree;
6446 struct got_fileindex *fileindex;
6447 struct got_repository *repo;
6448 int have_changes;
6451 const struct got_error *
6452 check_stage_ok(void *arg, unsigned char status,
6453 unsigned char staged_status, const char *relpath,
6454 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6455 struct got_object_id *commit_id, int dirfd, const char *de_name)
6457 struct check_stage_ok_arg *a = arg;
6458 const struct got_error *err = NULL;
6459 struct got_fileindex_entry *ie;
6460 struct got_object_id base_commit_id;
6461 struct got_object_id *base_commit_idp = NULL;
6462 char *in_repo_path = NULL, *p;
6464 if (status == GOT_STATUS_UNVERSIONED ||
6465 status == GOT_STATUS_NO_CHANGE)
6466 return NULL;
6467 if (status == GOT_STATUS_NONEXISTENT)
6468 return got_error_set_errno(ENOENT, relpath);
6470 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6471 if (ie == NULL)
6472 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6474 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6475 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6476 relpath) == -1)
6477 return got_error_from_errno("asprintf");
6479 if (got_fileindex_entry_has_commit(ie)) {
6480 memcpy(base_commit_id.sha1, ie->commit_sha1,
6481 SHA1_DIGEST_LENGTH);
6482 base_commit_idp = &base_commit_id;
6485 if (status == GOT_STATUS_CONFLICT) {
6486 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6487 goto done;
6488 } else if (status != GOT_STATUS_ADD &&
6489 status != GOT_STATUS_MODIFY &&
6490 status != GOT_STATUS_DELETE) {
6491 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6492 goto done;
6495 a->have_changes = 1;
6497 p = in_repo_path;
6498 while (p[0] == '/')
6499 p++;
6500 err = check_out_of_date(p, status, staged_status,
6501 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6502 GOT_ERR_STAGE_OUT_OF_DATE);
6503 done:
6504 free(in_repo_path);
6505 return err;
6508 struct stage_path_arg {
6509 struct got_worktree *worktree;
6510 struct got_fileindex *fileindex;
6511 struct got_repository *repo;
6512 got_worktree_status_cb status_cb;
6513 void *status_arg;
6514 got_worktree_patch_cb patch_cb;
6515 void *patch_arg;
6516 int staged_something;
6519 static const struct got_error *
6520 stage_path(void *arg, unsigned char status,
6521 unsigned char staged_status, const char *relpath,
6522 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6523 struct got_object_id *commit_id, int dirfd, const char *de_name)
6525 struct stage_path_arg *a = arg;
6526 const struct got_error *err = NULL;
6527 struct got_fileindex_entry *ie;
6528 char *ondisk_path = NULL, *path_content = NULL;
6529 uint32_t stage;
6530 struct got_object_id *new_staged_blob_id = NULL;
6532 if (status == GOT_STATUS_UNVERSIONED)
6533 return NULL;
6535 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6536 if (ie == NULL)
6537 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6539 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6540 relpath)== -1)
6541 return got_error_from_errno("asprintf");
6543 switch (status) {
6544 case GOT_STATUS_ADD:
6545 case GOT_STATUS_MODIFY:
6546 if (a->patch_cb) {
6547 if (status == GOT_STATUS_ADD) {
6548 int choice = GOT_PATCH_CHOICE_NONE;
6549 err = (*a->patch_cb)(&choice, a->patch_arg,
6550 status, ie->path, NULL, 1, 1);
6551 if (err)
6552 break;
6553 if (choice != GOT_PATCH_CHOICE_YES)
6554 break;
6555 } else {
6556 err = create_patched_content(&path_content, 0,
6557 staged_blob_id ? staged_blob_id : blob_id,
6558 ondisk_path, dirfd, de_name, ie->path,
6559 a->repo, a->patch_cb, a->patch_arg);
6560 if (err || path_content == NULL)
6561 break;
6564 err = got_object_blob_create(&new_staged_blob_id,
6565 path_content ? path_content : ondisk_path, a->repo);
6566 if (err)
6567 break;
6568 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6569 SHA1_DIGEST_LENGTH);
6570 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6571 stage = GOT_FILEIDX_STAGE_ADD;
6572 else
6573 stage = GOT_FILEIDX_STAGE_MODIFY;
6574 got_fileindex_entry_stage_set(ie, stage);
6575 a->staged_something = 1;
6576 if (a->status_cb == NULL)
6577 break;
6578 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6579 get_staged_status(ie), relpath, blob_id,
6580 new_staged_blob_id, NULL, dirfd, de_name);
6581 break;
6582 case GOT_STATUS_DELETE:
6583 if (staged_status == GOT_STATUS_DELETE)
6584 break;
6585 if (a->patch_cb) {
6586 int choice = GOT_PATCH_CHOICE_NONE;
6587 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6588 ie->path, NULL, 1, 1);
6589 if (err)
6590 break;
6591 if (choice == GOT_PATCH_CHOICE_NO)
6592 break;
6593 if (choice != GOT_PATCH_CHOICE_YES) {
6594 err = got_error(GOT_ERR_PATCH_CHOICE);
6595 break;
6598 stage = GOT_FILEIDX_STAGE_DELETE;
6599 got_fileindex_entry_stage_set(ie, stage);
6600 a->staged_something = 1;
6601 if (a->status_cb == NULL)
6602 break;
6603 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6604 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6605 de_name);
6606 break;
6607 case GOT_STATUS_NO_CHANGE:
6608 break;
6609 case GOT_STATUS_CONFLICT:
6610 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6611 break;
6612 case GOT_STATUS_NONEXISTENT:
6613 err = got_error_set_errno(ENOENT, relpath);
6614 break;
6615 default:
6616 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6617 break;
6620 if (path_content && unlink(path_content) == -1 && err == NULL)
6621 err = got_error_from_errno2("unlink", path_content);
6622 free(path_content);
6623 free(ondisk_path);
6624 free(new_staged_blob_id);
6625 return err;
6628 const struct got_error *
6629 got_worktree_stage(struct got_worktree *worktree,
6630 struct got_pathlist_head *paths,
6631 got_worktree_status_cb status_cb, void *status_arg,
6632 got_worktree_patch_cb patch_cb, void *patch_arg,
6633 struct got_repository *repo)
6635 const struct got_error *err = NULL, *sync_err, *unlockerr;
6636 struct got_pathlist_entry *pe;
6637 struct got_fileindex *fileindex = NULL;
6638 char *fileindex_path = NULL;
6639 struct got_reference *head_ref = NULL;
6640 struct got_object_id *head_commit_id = NULL;
6641 struct check_stage_ok_arg oka;
6642 struct stage_path_arg spa;
6644 err = lock_worktree(worktree, LOCK_EX);
6645 if (err)
6646 return err;
6648 err = got_ref_open(&head_ref, repo,
6649 got_worktree_get_head_ref_name(worktree), 0);
6650 if (err)
6651 goto done;
6652 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6653 if (err)
6654 goto done;
6655 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6656 if (err)
6657 goto done;
6659 /* Check pre-conditions before staging anything. */
6660 oka.head_commit_id = head_commit_id;
6661 oka.worktree = worktree;
6662 oka.fileindex = fileindex;
6663 oka.repo = repo;
6664 oka.have_changes = 0;
6665 TAILQ_FOREACH(pe, paths, entry) {
6666 err = worktree_status(worktree, pe->path, fileindex, repo,
6667 check_stage_ok, &oka, NULL, NULL, 0, 0);
6668 if (err)
6669 goto done;
6671 if (!oka.have_changes) {
6672 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6673 goto done;
6676 spa.worktree = worktree;
6677 spa.fileindex = fileindex;
6678 spa.repo = repo;
6679 spa.patch_cb = patch_cb;
6680 spa.patch_arg = patch_arg;
6681 spa.status_cb = status_cb;
6682 spa.status_arg = status_arg;
6683 spa.staged_something = 0;
6684 TAILQ_FOREACH(pe, paths, entry) {
6685 err = worktree_status(worktree, pe->path, fileindex, repo,
6686 stage_path, &spa, NULL, NULL, 0, 0);
6687 if (err)
6688 goto done;
6690 if (!spa.staged_something) {
6691 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6692 goto done;
6695 sync_err = sync_fileindex(fileindex, fileindex_path);
6696 if (sync_err && err == NULL)
6697 err = sync_err;
6698 done:
6699 if (head_ref)
6700 got_ref_close(head_ref);
6701 free(head_commit_id);
6702 free(fileindex_path);
6703 if (fileindex)
6704 got_fileindex_free(fileindex);
6705 unlockerr = lock_worktree(worktree, LOCK_SH);
6706 if (unlockerr && err == NULL)
6707 err = unlockerr;
6708 return err;
6711 struct unstage_path_arg {
6712 struct got_worktree *worktree;
6713 struct got_fileindex *fileindex;
6714 struct got_repository *repo;
6715 got_worktree_checkout_cb progress_cb;
6716 void *progress_arg;
6717 got_worktree_patch_cb patch_cb;
6718 void *patch_arg;
6721 static const struct got_error *
6722 create_unstaged_content(char **path_unstaged_content,
6723 char **path_new_staged_content, struct got_object_id *blob_id,
6724 struct got_object_id *staged_blob_id, const char *relpath,
6725 struct got_repository *repo,
6726 got_worktree_patch_cb patch_cb, void *patch_arg)
6728 const struct got_error *err;
6729 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6730 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6731 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6732 struct stat sb1, sb2;
6733 struct got_diff_changes *changes = NULL;
6734 struct got_diff_state *ds = NULL;
6735 struct got_diff_args *args = NULL;
6736 struct got_diff_change *change;
6737 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6738 int have_content = 0, have_rejected_content = 0;
6740 *path_unstaged_content = NULL;
6741 *path_new_staged_content = NULL;
6743 err = got_object_id_str(&label1, blob_id);
6744 if (err)
6745 return err;
6746 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6747 if (err)
6748 goto done;
6750 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6751 if (err)
6752 goto done;
6754 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6755 if (err)
6756 goto done;
6758 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6759 if (err)
6760 goto done;
6762 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6763 if (err)
6764 goto done;
6766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6767 if (err)
6768 goto done;
6770 if (stat(path1, &sb1) == -1) {
6771 err = got_error_from_errno2("stat", path1);
6772 goto done;
6775 if (stat(path2, &sb2) == -1) {
6776 err = got_error_from_errno2("stat", path2);
6777 goto done;
6780 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6781 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6782 if (err)
6783 goto done;
6785 err = got_opentemp_named(path_unstaged_content, &outfile,
6786 "got-unstaged-content");
6787 if (err)
6788 goto done;
6789 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6790 "got-new-staged-content");
6791 if (err)
6792 goto done;
6794 if (fseek(f1, 0L, SEEK_SET) == -1) {
6795 err = got_ferror(f1, GOT_ERR_IO);
6796 goto done;
6798 if (fseek(f2, 0L, SEEK_SET) == -1) {
6799 err = got_ferror(f2, GOT_ERR_IO);
6800 goto done;
6802 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6803 int choice;
6804 err = apply_or_reject_change(&choice, change, ++n,
6805 changes->nchanges, ds, args, diff_flags, relpath,
6806 f1, f2, &line_cur1, &line_cur2,
6807 outfile, rejectfile, patch_cb, patch_arg);
6808 if (err)
6809 goto done;
6810 if (choice == GOT_PATCH_CHOICE_YES)
6811 have_content = 1;
6812 else
6813 have_rejected_content = 1;
6814 if (choice == GOT_PATCH_CHOICE_QUIT)
6815 break;
6817 if (have_content || have_rejected_content)
6818 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6819 outfile, rejectfile);
6820 done:
6821 free(label1);
6822 if (blob)
6823 got_object_blob_close(blob);
6824 if (staged_blob)
6825 got_object_blob_close(staged_blob);
6826 if (f1 && fclose(f1) == EOF && err == NULL)
6827 err = got_error_from_errno2("fclose", path1);
6828 if (f2 && fclose(f2) == EOF && err == NULL)
6829 err = got_error_from_errno2("fclose", path2);
6830 if (outfile && fclose(outfile) == EOF && err == NULL)
6831 err = got_error_from_errno2("fclose", *path_unstaged_content);
6832 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6833 err = got_error_from_errno2("fclose", *path_new_staged_content);
6834 if (path1 && unlink(path1) == -1 && err == NULL)
6835 err = got_error_from_errno2("unlink", path1);
6836 if (path2 && unlink(path2) == -1 && err == NULL)
6837 err = got_error_from_errno2("unlink", path2);
6838 if (err || !have_content) {
6839 if (*path_unstaged_content &&
6840 unlink(*path_unstaged_content) == -1 && err == NULL)
6841 err = got_error_from_errno2("unlink",
6842 *path_unstaged_content);
6843 free(*path_unstaged_content);
6844 *path_unstaged_content = NULL;
6846 if (err || !have_rejected_content) {
6847 if (*path_new_staged_content &&
6848 unlink(*path_new_staged_content) == -1 && err == NULL)
6849 err = got_error_from_errno2("unlink",
6850 *path_new_staged_content);
6851 free(*path_new_staged_content);
6852 *path_new_staged_content = NULL;
6854 free(args);
6855 if (ds) {
6856 got_diff_state_free(ds);
6857 free(ds);
6859 if (changes)
6860 got_diff_free_changes(changes);
6861 free(path1);
6862 free(path2);
6863 return err;
6866 static const struct got_error *
6867 unstage_path(void *arg, unsigned char status,
6868 unsigned char staged_status, const char *relpath,
6869 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6870 struct got_object_id *commit_id, int dirfd, const char *de_name)
6872 const struct got_error *err = NULL;
6873 struct unstage_path_arg *a = arg;
6874 struct got_fileindex_entry *ie;
6875 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6876 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6877 char *path_new_staged_content = NULL;
6878 char *id_str = NULL, *label_orig = NULL;
6879 int local_changes_subsumed;
6880 struct stat sb;
6882 if (staged_status != GOT_STATUS_ADD &&
6883 staged_status != GOT_STATUS_MODIFY &&
6884 staged_status != GOT_STATUS_DELETE)
6885 return NULL;
6887 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6888 if (ie == NULL)
6889 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6891 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6892 == -1)
6893 return got_error_from_errno("asprintf");
6895 err = got_object_id_str(&id_str,
6896 commit_id ? commit_id : a->worktree->base_commit_id);
6897 if (err)
6898 goto done;
6899 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6900 id_str) == -1) {
6901 err = got_error_from_errno("asprintf");
6902 goto done;
6905 switch (staged_status) {
6906 case GOT_STATUS_MODIFY:
6907 err = got_object_open_as_blob(&blob_base, a->repo,
6908 blob_id, 8192);
6909 if (err)
6910 break;
6911 /* fall through */
6912 case GOT_STATUS_ADD:
6913 if (a->patch_cb) {
6914 if (staged_status == GOT_STATUS_ADD) {
6915 int choice = GOT_PATCH_CHOICE_NONE;
6916 err = (*a->patch_cb)(&choice, a->patch_arg,
6917 staged_status, ie->path, NULL, 1, 1);
6918 if (err)
6919 break;
6920 if (choice != GOT_PATCH_CHOICE_YES)
6921 break;
6922 } else {
6923 err = create_unstaged_content(
6924 &path_unstaged_content,
6925 &path_new_staged_content, blob_id,
6926 staged_blob_id, ie->path, a->repo,
6927 a->patch_cb, a->patch_arg);
6928 if (err || path_unstaged_content == NULL)
6929 break;
6930 if (path_new_staged_content) {
6931 err = got_object_blob_create(
6932 &staged_blob_id,
6933 path_new_staged_content,
6934 a->repo);
6935 if (err)
6936 break;
6937 memcpy(ie->staged_blob_sha1,
6938 staged_blob_id->sha1,
6939 SHA1_DIGEST_LENGTH);
6941 err = merge_file(&local_changes_subsumed,
6942 a->worktree, blob_base, ondisk_path,
6943 relpath, got_fileindex_perms_to_st(ie),
6944 path_unstaged_content, label_orig,
6945 "unstaged", a->repo, a->progress_cb,
6946 a->progress_arg);
6947 if (err == NULL &&
6948 path_new_staged_content == NULL)
6949 got_fileindex_entry_stage_set(ie,
6950 GOT_FILEIDX_STAGE_NONE);
6951 break; /* Done with this file. */
6954 err = got_object_open_as_blob(&blob_staged, a->repo,
6955 staged_blob_id, 8192);
6956 if (err)
6957 break;
6958 err = merge_blob(&local_changes_subsumed, a->worktree,
6959 blob_base, ondisk_path, relpath,
6960 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6961 commit_id ? commit_id : a->worktree->base_commit_id,
6962 a->repo, a->progress_cb, a->progress_arg);
6963 if (err == NULL)
6964 got_fileindex_entry_stage_set(ie,
6965 GOT_FILEIDX_STAGE_NONE);
6966 break;
6967 case GOT_STATUS_DELETE:
6968 if (a->patch_cb) {
6969 int choice = GOT_PATCH_CHOICE_NONE;
6970 err = (*a->patch_cb)(&choice, a->patch_arg,
6971 staged_status, ie->path, NULL, 1, 1);
6972 if (err)
6973 break;
6974 if (choice == GOT_PATCH_CHOICE_NO)
6975 break;
6976 if (choice != GOT_PATCH_CHOICE_YES) {
6977 err = got_error(GOT_ERR_PATCH_CHOICE);
6978 break;
6981 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6982 err = get_file_status(&status, &sb, ie, ondisk_path,
6983 dirfd, de_name, a->repo);
6984 if (err)
6985 break;
6986 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6987 break;
6989 done:
6990 free(ondisk_path);
6991 if (path_unstaged_content &&
6992 unlink(path_unstaged_content) == -1 && err == NULL)
6993 err = got_error_from_errno2("unlink", path_unstaged_content);
6994 if (path_new_staged_content &&
6995 unlink(path_new_staged_content) == -1 && err == NULL)
6996 err = got_error_from_errno2("unlink", path_new_staged_content);
6997 free(path_unstaged_content);
6998 free(path_new_staged_content);
6999 if (blob_base)
7000 got_object_blob_close(blob_base);
7001 if (blob_staged)
7002 got_object_blob_close(blob_staged);
7003 free(id_str);
7004 free(label_orig);
7005 return err;
7008 const struct got_error *
7009 got_worktree_unstage(struct got_worktree *worktree,
7010 struct got_pathlist_head *paths,
7011 got_worktree_checkout_cb progress_cb, void *progress_arg,
7012 got_worktree_patch_cb patch_cb, void *patch_arg,
7013 struct got_repository *repo)
7015 const struct got_error *err = NULL, *sync_err, *unlockerr;
7016 struct got_pathlist_entry *pe;
7017 struct got_fileindex *fileindex = NULL;
7018 char *fileindex_path = NULL;
7019 struct unstage_path_arg upa;
7021 err = lock_worktree(worktree, LOCK_EX);
7022 if (err)
7023 return err;
7025 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7026 if (err)
7027 goto done;
7029 upa.worktree = worktree;
7030 upa.fileindex = fileindex;
7031 upa.repo = repo;
7032 upa.progress_cb = progress_cb;
7033 upa.progress_arg = progress_arg;
7034 upa.patch_cb = patch_cb;
7035 upa.patch_arg = patch_arg;
7036 TAILQ_FOREACH(pe, paths, entry) {
7037 err = worktree_status(worktree, pe->path, fileindex, repo,
7038 unstage_path, &upa, NULL, NULL, 0, 0);
7039 if (err)
7040 goto done;
7043 sync_err = sync_fileindex(fileindex, fileindex_path);
7044 if (sync_err && err == NULL)
7045 err = sync_err;
7046 done:
7047 free(fileindex_path);
7048 if (fileindex)
7049 got_fileindex_free(fileindex);
7050 unlockerr = lock_worktree(worktree, LOCK_SH);
7051 if (unlockerr && err == NULL)
7052 err = unlockerr;
7053 return err;