Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 goto done;
815 done:
816 if (err) {
817 if (merged_path)
818 unlink(merged_path);
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
824 free(merged_path);
825 free(base_path);
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
830 return err;
833 /*
834 * Perform a 3-way merge where blob_orig acts as the common ancestor,
835 * blob_deriv acts as the first derived version, and the file on disk
836 * acts as the second derived version.
837 */
838 static const struct got_error *
839 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
840 struct got_blob_object *blob_orig, const char *ondisk_path,
841 const char *path, uint16_t st_mode, const char *label_orig,
842 struct got_blob_object *blob_deriv,
843 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
844 got_worktree_checkout_cb progress_cb, void *progress_arg)
846 const struct got_error *err = NULL;
847 FILE *f_deriv = NULL;
848 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
849 char *label_deriv = NULL, *parent;
851 *local_changes_subsumed = 0;
853 parent = dirname(ondisk_path);
854 if (parent == NULL)
855 return got_error_from_errno2("dirname", ondisk_path);
857 free(base_path);
858 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
859 err = got_error_from_errno("asprintf");
860 base_path = NULL;
861 goto done;
864 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
865 if (err)
866 goto done;
867 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
868 blob_deriv);
869 if (err)
870 goto done;
872 err = got_object_id_str(&id_str, deriv_base_commit_id);
873 if (err)
874 goto done;
875 if (asprintf(&label_deriv, "%s: commit %s",
876 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
877 err = got_error_from_errno("asprintf");
878 goto done;
881 err = merge_file(local_changes_subsumed, worktree, blob_orig,
882 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
883 label_deriv, repo, progress_cb, progress_arg);
884 done:
885 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
886 err = got_error_from_errno("fclose");
887 free(base_path);
888 if (blob_deriv_path) {
889 unlink(blob_deriv_path);
890 free(blob_deriv_path);
892 free(id_str);
893 free(label_deriv);
894 return err;
897 static const struct got_error *
898 create_fileindex_entry(struct got_fileindex *fileindex,
899 struct got_object_id *base_commit_id, const char *ondisk_path,
900 const char *path, struct got_object_id *blob_id)
902 const struct got_error *err = NULL;
903 struct got_fileindex_entry *new_ie;
905 err = got_fileindex_entry_alloc(&new_ie, path);
906 if (err)
907 return err;
909 err = got_fileindex_entry_update(new_ie, ondisk_path,
910 blob_id->sha1, base_commit_id->sha1, 1);
911 if (err)
912 goto done;
914 err = got_fileindex_entry_add(fileindex, new_ie);
915 done:
916 if (err)
917 got_fileindex_entry_free(new_ie);
918 return err;
921 static mode_t
922 get_ondisk_perms(int executable, mode_t st_mode)
924 mode_t xbits = S_IXUSR;
926 if (executable) {
927 /* Map read bits to execute bits. */
928 if (st_mode & S_IRGRP)
929 xbits |= S_IXGRP;
930 if (st_mode & S_IROTH)
931 xbits |= S_IXOTH;
932 return st_mode | xbits;
935 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
938 /* forward declaration */
939 static const struct got_error *
940 install_blob(struct got_worktree *worktree, const char *ondisk_path,
941 const char *path, mode_t te_mode, mode_t st_mode,
942 struct got_blob_object *blob, int restoring_missing_file,
943 int reverting_versioned_file, struct got_repository *repo,
944 got_worktree_checkout_cb progress_cb, void *progress_arg);
946 static const struct got_error *
947 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
948 const char *path, mode_t te_mode, mode_t st_mode,
949 struct got_blob_object *blob, int restoring_missing_file,
950 int reverting_versioned_file, struct got_repository *repo,
951 got_worktree_checkout_cb progress_cb, void *progress_arg)
953 const struct got_error *err = NULL;
954 char target_path[PATH_MAX];
955 size_t len, target_len = 0;
956 char *resolved_path = NULL, *abspath = NULL;
957 const uint8_t *buf = got_object_blob_get_read_buf(blob);
958 size_t hdrlen = got_object_blob_get_hdrlen(blob);
960 /*
961 * Blob object content specifies the target path of the link.
962 * If a symbolic link cannot be installed we instead create
963 * a regular file which contains the link target path stored
964 * in the blob object.
965 */
966 do {
967 err = got_object_blob_read_block(&len, blob);
968 if (len + target_len >= sizeof(target_path)) {
969 /* Path too long; install as a regular file. */
970 got_object_blob_rewind(blob);
971 return install_blob(worktree, ondisk_path, path,
972 GOT_DEFAULT_FILE_MODE, st_mode, blob,
973 restoring_missing_file, reverting_versioned_file,
974 repo, progress_cb, progress_arg);
976 if (len > 0) {
977 /* Skip blob object header first time around. */
978 memcpy(target_path + target_len, buf + hdrlen,
979 len - hdrlen);
980 target_len += len - hdrlen;
981 hdrlen = 0;
983 } while (len != 0);
984 target_path[target_len] = '\0';
986 /*
987 * Relative symlink target lookup should begin at the directory
988 * in which the blob object is being installed.
989 */
990 if (!got_path_is_absolute(target_path)) {
991 char *parent = dirname(ondisk_path);
992 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
993 err = got_error_from_errno("asprintf");
994 goto done;
998 /*
999 * unveil(2) restricts our view of paths in the filesystem.
1000 * ENOENT will occur if a link target path does not exist or
1001 * if it points outside our unveiled path space.
1003 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1004 if (resolved_path == NULL) {
1005 if (errno != ENOENT)
1006 return got_error_from_errno2("realpath", target_path);
1009 /* Only allow symlinks pointing at paths within the work tree. */
1010 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1011 abspath : target_path), worktree->root_path,
1012 strlen(worktree->root_path))) {
1013 /* install as a regular file */
1014 got_object_blob_rewind(blob);
1015 err = install_blob(worktree, ondisk_path, path,
1016 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1017 restoring_missing_file, reverting_versioned_file,
1018 repo, progress_cb, progress_arg);
1019 goto done;
1022 if (symlink(target_path, ondisk_path) == -1) {
1023 if (errno == EEXIST) {
1024 struct stat sb;
1025 ssize_t elen;
1026 char etarget[PATH_MAX];
1027 if (lstat(ondisk_path, &sb) == -1) {
1028 err = got_error_from_errno2("lstat",
1029 ondisk_path);
1030 goto done;
1032 if (!S_ISLNK(sb.st_mode)) {
1033 err = got_error_path(ondisk_path,
1034 GOT_ERR_FILE_OBSTRUCTED);
1035 goto done;
1037 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1038 if (elen == -1) {
1039 err = got_error_from_errno2("readlink",
1040 ondisk_path);
1041 goto done;
1043 if (elen == target_len &&
1044 memcmp(etarget, target_path, target_len) == 0) {
1045 err = NULL; /* nothing to do */
1046 goto done;
1047 } else {
1048 if (unlink(ondisk_path) == -1) {
1049 err = got_error_from_errno2("unlink",
1050 ondisk_path);
1051 goto done;
1053 if (symlink(target_path, ondisk_path) == -1) {
1054 err = got_error_from_errno3("symlink",
1055 target_path, ondisk_path);
1056 goto done;
1059 err = (*progress_cb)(progress_arg,
1060 GOT_STATUS_UPDATE, path);
1061 goto done;
1065 if (errno == ENOENT) {
1066 char *parent = dirname(ondisk_path);
1067 if (parent == NULL) {
1068 err = got_error_from_errno2("dirname",
1069 ondisk_path);
1070 goto done;
1072 err = add_dir_on_disk(worktree, parent);
1073 if (err)
1074 goto done;
1076 * Retry, and fall through to error handling
1077 * below if this second attempt fails.
1079 if (symlink(target_path, ondisk_path) != -1) {
1080 err = NULL; /* success */
1081 goto done;
1085 /* Handle errors from first or second creation attempt. */
1086 if (errno == ENAMETOOLONG) {
1087 /* bad target path; install as a regular file */
1088 got_object_blob_rewind(blob);
1089 err = install_blob(worktree, ondisk_path, path,
1090 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1091 restoring_missing_file, reverting_versioned_file,
1092 repo, progress_cb, progress_arg);
1093 } else if (errno == ENOTDIR) {
1094 err = got_error_path(ondisk_path,
1095 GOT_ERR_FILE_OBSTRUCTED);
1096 } else {
1097 err = got_error_from_errno3("symlink",
1098 target_path, ondisk_path);
1100 } else
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1102 done:
1103 free(resolved_path);
1104 free(abspath);
1105 return err;
1108 static const struct got_error *
1109 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1110 const char *path, mode_t te_mode, mode_t st_mode,
1111 struct got_blob_object *blob, int restoring_missing_file,
1112 int reverting_versioned_file, struct got_repository *repo,
1113 got_worktree_checkout_cb progress_cb, void *progress_arg)
1115 const struct got_error *err = NULL;
1116 int fd = -1;
1117 size_t len, hdrlen;
1118 int update = 0;
1119 char *tmppath = NULL;
1121 if (S_ISLNK(te_mode))
1122 return install_symlink(worktree, ondisk_path, path, te_mode,
1123 st_mode, blob, restoring_missing_file,
1124 reverting_versioned_file, repo, progress_cb, progress_arg);
1126 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1127 GOT_DEFAULT_FILE_MODE);
1128 if (fd == -1) {
1129 if (errno == ENOENT) {
1130 char *parent = dirname(path);
1131 if (parent == NULL)
1132 return got_error_from_errno2("dirname", path);
1133 err = add_dir_on_disk(worktree, parent);
1134 if (err)
1135 return err;
1136 fd = open(ondisk_path,
1137 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1138 GOT_DEFAULT_FILE_MODE);
1139 if (fd == -1)
1140 return got_error_from_errno2("open",
1141 ondisk_path);
1142 } else if (errno == EEXIST) {
1143 if (!S_ISREG(st_mode)) {
1144 /* TODO file is obstructed; do something */
1145 err = got_error_path(ondisk_path,
1146 GOT_ERR_FILE_OBSTRUCTED);
1147 goto done;
1148 } else {
1149 err = got_opentemp_named_fd(&tmppath, &fd,
1150 ondisk_path);
1151 if (err)
1152 goto done;
1153 update = 1;
1155 } else
1156 return got_error_from_errno2("open", ondisk_path);
1159 if (restoring_missing_file)
1160 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1161 else if (reverting_versioned_file)
1162 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1163 else
1164 err = (*progress_cb)(progress_arg,
1165 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1166 if (err)
1167 goto done;
1169 hdrlen = got_object_blob_get_hdrlen(blob);
1170 do {
1171 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1172 err = got_object_blob_read_block(&len, blob);
1173 if (err)
1174 break;
1175 if (len > 0) {
1176 /* Skip blob object header first time around. */
1177 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1178 if (outlen == -1) {
1179 err = got_error_from_errno("write");
1180 goto done;
1181 } else if (outlen != len - hdrlen) {
1182 err = got_error(GOT_ERR_IO);
1183 goto done;
1185 hdrlen = 0;
1187 } while (len != 0);
1189 if (fsync(fd) != 0) {
1190 err = got_error_from_errno("fsync");
1191 goto done;
1194 if (update) {
1195 if (rename(tmppath, ondisk_path) != 0) {
1196 err = got_error_from_errno3("rename", tmppath,
1197 ondisk_path);
1198 unlink(tmppath);
1199 goto done;
1203 if (chmod(ondisk_path,
1204 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1205 err = got_error_from_errno2("chmod", ondisk_path);
1206 goto done;
1209 done:
1210 if (fd != -1 && close(fd) != 0 && err == NULL)
1211 err = got_error_from_errno("close");
1212 free(tmppath);
1213 return err;
1216 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1217 static const struct got_error *
1218 get_modified_file_content_status(unsigned char *status, FILE *f)
1220 const struct got_error *err = NULL;
1221 const char *markers[3] = {
1222 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1223 GOT_DIFF_CONFLICT_MARKER_SEP,
1224 GOT_DIFF_CONFLICT_MARKER_END
1226 int i = 0;
1227 char *line;
1228 size_t len;
1229 const char delim[3] = {'\0', '\0', '\0'};
1231 while (*status == GOT_STATUS_MODIFY) {
1232 line = fparseln(f, &len, NULL, delim, 0);
1233 if (line == NULL) {
1234 if (feof(f))
1235 break;
1236 err = got_ferror(f, GOT_ERR_IO);
1237 break;
1240 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1241 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1242 == 0)
1243 *status = GOT_STATUS_CONFLICT;
1244 else
1245 i++;
1249 return err;
1252 static int
1253 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1255 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1256 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1259 static int
1260 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1262 return !(ie->ctime_sec == sb->st_ctime &&
1263 ie->ctime_nsec == sb->st_ctimensec &&
1264 ie->mtime_sec == sb->st_mtime &&
1265 ie->mtime_nsec == sb->st_mtimensec &&
1266 ie->size == (sb->st_size & 0xffffffff) &&
1267 !xbit_differs(ie, sb->st_mode));
1270 static unsigned char
1271 get_staged_status(struct got_fileindex_entry *ie)
1273 switch (got_fileindex_entry_stage_get(ie)) {
1274 case GOT_FILEIDX_STAGE_ADD:
1275 return GOT_STATUS_ADD;
1276 case GOT_FILEIDX_STAGE_DELETE:
1277 return GOT_STATUS_DELETE;
1278 case GOT_FILEIDX_STAGE_MODIFY:
1279 return GOT_STATUS_MODIFY;
1280 default:
1281 return GOT_STATUS_NO_CHANGE;
1285 static const struct got_error *
1286 get_file_status(unsigned char *status, struct stat *sb,
1287 struct got_fileindex_entry *ie, const char *abspath,
1288 int dirfd, const char *de_name, struct got_repository *repo)
1290 const struct got_error *err = NULL;
1291 struct got_object_id id;
1292 size_t hdrlen;
1293 int fd = -1;
1294 FILE *f = NULL;
1295 uint8_t fbuf[8192];
1296 struct got_blob_object *blob = NULL;
1297 size_t flen, blen;
1298 unsigned char staged_status = get_staged_status(ie);
1300 *status = GOT_STATUS_NO_CHANGE;
1303 * Whenever the caller provides a directory descriptor and a
1304 * directory entry name for the file, use them! This prevents
1305 * race conditions if filesystem paths change beneath our feet.
1307 if (dirfd != -1) {
1308 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1309 if (errno == ENOENT) {
1310 if (got_fileindex_entry_has_file_on_disk(ie))
1311 *status = GOT_STATUS_MISSING;
1312 else
1313 *status = GOT_STATUS_DELETE;
1314 goto done;
1316 err = got_error_from_errno2("fstatat", abspath);
1317 goto done;
1319 } else {
1320 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1321 if (fd == -1 && errno != ENOENT)
1322 return got_error_from_errno2("open", abspath);
1323 if (fd == -1 || fstat(fd, sb) == -1) {
1324 if (errno == ENOENT) {
1325 if (got_fileindex_entry_has_file_on_disk(ie))
1326 *status = GOT_STATUS_MISSING;
1327 else
1328 *status = GOT_STATUS_DELETE;
1329 goto done;
1331 err = got_error_from_errno2("fstat", abspath);
1332 goto done;
1336 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1337 *status = GOT_STATUS_OBSTRUCTED;
1338 goto done;
1341 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1342 *status = GOT_STATUS_DELETE;
1343 goto done;
1344 } else if (!got_fileindex_entry_has_blob(ie) &&
1345 staged_status != GOT_STATUS_ADD) {
1346 *status = GOT_STATUS_ADD;
1347 goto done;
1350 if (!stat_info_differs(ie, sb))
1351 goto done;
1353 if (staged_status == GOT_STATUS_MODIFY ||
1354 staged_status == GOT_STATUS_ADD)
1355 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1356 else
1357 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1359 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1360 if (err)
1361 goto done;
1363 if (dirfd != -1) {
1364 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1365 if (fd == -1) {
1366 err = got_error_from_errno2("openat", abspath);
1367 goto done;
1371 f = fdopen(fd, "r");
1372 if (f == NULL) {
1373 err = got_error_from_errno2("fdopen", abspath);
1374 goto done;
1376 fd = -1;
1377 hdrlen = got_object_blob_get_hdrlen(blob);
1378 for (;;) {
1379 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1380 err = got_object_blob_read_block(&blen, blob);
1381 if (err)
1382 goto done;
1383 /* Skip length of blob object header first time around. */
1384 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1385 if (flen == 0 && ferror(f)) {
1386 err = got_error_from_errno("fread");
1387 goto done;
1389 if (blen == 0) {
1390 if (flen != 0)
1391 *status = GOT_STATUS_MODIFY;
1392 break;
1393 } else if (flen == 0) {
1394 if (blen != 0)
1395 *status = GOT_STATUS_MODIFY;
1396 break;
1397 } else if (blen - hdrlen == flen) {
1398 /* Skip blob object header first time around. */
1399 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1400 *status = GOT_STATUS_MODIFY;
1401 break;
1403 } else {
1404 *status = GOT_STATUS_MODIFY;
1405 break;
1407 hdrlen = 0;
1410 if (*status == GOT_STATUS_MODIFY) {
1411 rewind(f);
1412 err = get_modified_file_content_status(status, f);
1413 } else if (xbit_differs(ie, sb->st_mode))
1414 *status = GOT_STATUS_MODE_CHANGE;
1415 done:
1416 if (blob)
1417 got_object_blob_close(blob);
1418 if (f != NULL && fclose(f) == EOF && err == NULL)
1419 err = got_error_from_errno2("fclose", abspath);
1420 if (fd != -1 && close(fd) == -1 && err == NULL)
1421 err = got_error_from_errno2("close", abspath);
1422 return err;
1426 * Update timestamps in the file index if a file is unmodified and
1427 * we had to run a full content comparison to find out.
1429 static const struct got_error *
1430 sync_timestamps(char *ondisk_path, unsigned char status,
1431 struct got_fileindex_entry *ie, struct stat *sb)
1433 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1434 return got_fileindex_entry_update(ie, ondisk_path,
1435 ie->blob_sha1, ie->commit_sha1, 1);
1437 return NULL;
1440 static const struct got_error *
1441 update_blob(struct got_worktree *worktree,
1442 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1443 struct got_tree_entry *te, const char *path,
1444 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1445 void *progress_arg)
1447 const struct got_error *err = NULL;
1448 struct got_blob_object *blob = NULL;
1449 char *ondisk_path;
1450 unsigned char status = GOT_STATUS_NO_CHANGE;
1451 struct stat sb;
1453 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1454 return got_error_from_errno("asprintf");
1456 if (ie) {
1457 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1458 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1459 goto done;
1461 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1462 repo);
1463 if (err)
1464 goto done;
1465 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1466 sb.st_mode = got_fileindex_perms_to_st(ie);
1467 } else
1468 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1470 if (status == GOT_STATUS_OBSTRUCTED) {
1471 err = (*progress_cb)(progress_arg, status, path);
1472 goto done;
1474 if (status == GOT_STATUS_CONFLICT) {
1475 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1476 path);
1477 goto done;
1480 if (ie && status != GOT_STATUS_MISSING &&
1481 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1482 if (got_fileindex_entry_has_commit(ie) &&
1483 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1484 SHA1_DIGEST_LENGTH) == 0) {
1485 err = sync_timestamps(ondisk_path, status, ie, &sb);
1486 if (err)
1487 goto done;
1488 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1489 path);
1490 goto done;
1492 if (got_fileindex_entry_has_blob(ie) &&
1493 memcmp(ie->blob_sha1, te->id.sha1,
1494 SHA1_DIGEST_LENGTH) == 0) {
1495 err = sync_timestamps(ondisk_path, status, ie, &sb);
1496 goto done;
1500 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1501 if (err)
1502 goto done;
1504 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1505 int update_timestamps;
1506 struct got_blob_object *blob2 = NULL;
1507 char *label_orig = NULL;
1508 if (got_fileindex_entry_has_blob(ie)) {
1509 struct got_object_id id2;
1510 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1511 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1512 if (err)
1513 goto done;
1515 if (got_fileindex_entry_has_commit(ie)) {
1516 char id_str[SHA1_DIGEST_STRING_LENGTH];
1517 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1518 sizeof(id_str)) == NULL) {
1519 err = got_error_path(id_str,
1520 GOT_ERR_BAD_OBJ_ID_STR);
1521 goto done;
1523 if (asprintf(&label_orig, "%s: commit %s",
1524 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1525 err = got_error_from_errno("asprintf");
1526 goto done;
1529 err = merge_blob(&update_timestamps, worktree, blob2,
1530 ondisk_path, path, sb.st_mode, label_orig, blob,
1531 worktree->base_commit_id, repo,
1532 progress_cb, progress_arg);
1533 free(label_orig);
1534 if (blob2)
1535 got_object_blob_close(blob2);
1536 if (err)
1537 goto done;
1539 * Do not update timestamps of files with local changes.
1540 * Otherwise, a future status walk would treat them as
1541 * unmodified files again.
1543 err = got_fileindex_entry_update(ie, ondisk_path,
1544 blob->id.sha1, worktree->base_commit_id->sha1,
1545 update_timestamps);
1546 } else if (status == GOT_STATUS_MODE_CHANGE) {
1547 err = got_fileindex_entry_update(ie, ondisk_path,
1548 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1549 } else if (status == GOT_STATUS_DELETE) {
1550 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1551 if (err)
1552 goto done;
1553 err = got_fileindex_entry_update(ie, ondisk_path,
1554 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1555 if (err)
1556 goto done;
1557 } else {
1558 err = install_blob(worktree, ondisk_path, path, te->mode,
1559 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1560 repo, progress_cb, progress_arg);
1561 if (err)
1562 goto done;
1563 if (ie) {
1564 err = got_fileindex_entry_update(ie, ondisk_path,
1565 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1566 } else {
1567 err = create_fileindex_entry(fileindex,
1568 worktree->base_commit_id, ondisk_path, path,
1569 &blob->id);
1571 if (err)
1572 goto done;
1574 got_object_blob_close(blob);
1575 done:
1576 free(ondisk_path);
1577 return err;
1580 static const struct got_error *
1581 remove_ondisk_file(const char *root_path, const char *path)
1583 const struct got_error *err = NULL;
1584 char *ondisk_path = NULL;
1586 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1587 return got_error_from_errno("asprintf");
1589 if (unlink(ondisk_path) == -1) {
1590 if (errno != ENOENT)
1591 err = got_error_from_errno2("unlink", ondisk_path);
1592 } else {
1593 char *parent = dirname(ondisk_path);
1594 while (parent && strcmp(parent, root_path) != 0) {
1595 if (rmdir(parent) == -1) {
1596 if (errno != ENOTEMPTY)
1597 err = got_error_from_errno2("rmdir",
1598 parent);
1599 break;
1601 parent = dirname(parent);
1604 free(ondisk_path);
1605 return err;
1608 static const struct got_error *
1609 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1610 struct got_fileindex_entry *ie, struct got_repository *repo,
1611 got_worktree_checkout_cb progress_cb, void *progress_arg)
1613 const struct got_error *err = NULL;
1614 unsigned char status;
1615 struct stat sb;
1616 char *ondisk_path;
1618 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1619 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1621 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1622 == -1)
1623 return got_error_from_errno("asprintf");
1625 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1626 if (err)
1627 goto done;
1629 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1630 status == GOT_STATUS_ADD) {
1631 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1632 if (err)
1633 goto done;
1635 * Preserve the working file and change the deleted blob's
1636 * entry into a schedule-add entry.
1638 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1639 0);
1640 } else {
1641 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1642 if (err)
1643 goto done;
1644 if (status == GOT_STATUS_NO_CHANGE) {
1645 err = remove_ondisk_file(worktree->root_path, ie->path);
1646 if (err)
1647 goto done;
1649 got_fileindex_entry_remove(fileindex, ie);
1651 done:
1652 free(ondisk_path);
1653 return err;
1656 struct diff_cb_arg {
1657 struct got_fileindex *fileindex;
1658 struct got_worktree *worktree;
1659 struct got_repository *repo;
1660 got_worktree_checkout_cb progress_cb;
1661 void *progress_arg;
1662 got_cancel_cb cancel_cb;
1663 void *cancel_arg;
1666 static const struct got_error *
1667 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1668 struct got_tree_entry *te, const char *parent_path)
1670 struct diff_cb_arg *a = arg;
1672 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1673 return got_error(GOT_ERR_CANCELLED);
1675 return update_blob(a->worktree, a->fileindex, ie, te,
1676 ie->path, a->repo, a->progress_cb, a->progress_arg);
1679 static const struct got_error *
1680 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1682 struct diff_cb_arg *a = arg;
1684 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1685 return got_error(GOT_ERR_CANCELLED);
1687 return delete_blob(a->worktree, a->fileindex, ie,
1688 a->repo, a->progress_cb, a->progress_arg);
1691 static const struct got_error *
1692 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1694 struct diff_cb_arg *a = arg;
1695 const struct got_error *err;
1696 char *path;
1698 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1699 return got_error(GOT_ERR_CANCELLED);
1701 if (got_object_tree_entry_is_submodule(te))
1702 return NULL;
1704 if (asprintf(&path, "%s%s%s", parent_path,
1705 parent_path[0] ? "/" : "", te->name)
1706 == -1)
1707 return got_error_from_errno("asprintf");
1709 if (S_ISDIR(te->mode))
1710 err = add_dir_on_disk(a->worktree, path);
1711 else
1712 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1713 a->repo, a->progress_cb, a->progress_arg);
1715 free(path);
1716 return err;
1719 static const struct got_error *
1720 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1722 const struct got_error *err = NULL;
1723 char *uuidstr = NULL;
1724 uint32_t uuid_status;
1726 *refname = NULL;
1728 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1729 if (uuid_status != uuid_s_ok)
1730 return got_error_uuid(uuid_status, "uuid_to_string");
1732 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1733 == -1) {
1734 err = got_error_from_errno("asprintf");
1735 *refname = NULL;
1737 free(uuidstr);
1738 return err;
1741 const struct got_error *
1742 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1744 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1747 static const struct got_error *
1748 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1750 return get_ref_name(refname, worktree,
1751 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1754 static const struct got_error *
1755 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1757 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1760 static const struct got_error *
1761 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1763 return get_ref_name(refname, worktree,
1764 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1767 static const struct got_error *
1768 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1770 return get_ref_name(refname, worktree,
1771 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1774 static const struct got_error *
1775 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1777 return get_ref_name(refname, worktree,
1778 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1781 static const struct got_error *
1782 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1784 return get_ref_name(refname, worktree,
1785 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1788 static const struct got_error *
1789 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1791 return get_ref_name(refname, worktree,
1792 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1795 static const struct got_error *
1796 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1798 return get_ref_name(refname, worktree,
1799 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1802 const struct got_error *
1803 got_worktree_get_histedit_script_path(char **path,
1804 struct got_worktree *worktree)
1806 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1807 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1808 *path = NULL;
1809 return got_error_from_errno("asprintf");
1811 return NULL;
1815 * Prevent Git's garbage collector from deleting our base commit by
1816 * setting a reference to our base commit's ID.
1818 static const struct got_error *
1819 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1821 const struct got_error *err = NULL;
1822 struct got_reference *ref = NULL;
1823 char *refname;
1825 err = got_worktree_get_base_ref_name(&refname, worktree);
1826 if (err)
1827 return err;
1829 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1830 if (err)
1831 goto done;
1833 err = got_ref_write(ref, repo);
1834 done:
1835 free(refname);
1836 if (ref)
1837 got_ref_close(ref);
1838 return err;
1841 static const struct got_error *
1842 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1844 const struct got_error *err = NULL;
1846 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1847 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1848 err = got_error_from_errno("asprintf");
1849 *fileindex_path = NULL;
1851 return err;
1855 static const struct got_error *
1856 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1857 struct got_worktree *worktree)
1859 const struct got_error *err = NULL;
1860 FILE *index = NULL;
1862 *fileindex_path = NULL;
1863 *fileindex = got_fileindex_alloc();
1864 if (*fileindex == NULL)
1865 return got_error_from_errno("got_fileindex_alloc");
1867 err = get_fileindex_path(fileindex_path, worktree);
1868 if (err)
1869 goto done;
1871 index = fopen(*fileindex_path, "rb");
1872 if (index == NULL) {
1873 if (errno != ENOENT)
1874 err = got_error_from_errno2("fopen", *fileindex_path);
1875 } else {
1876 err = got_fileindex_read(*fileindex, index);
1877 if (fclose(index) != 0 && err == NULL)
1878 err = got_error_from_errno("fclose");
1880 done:
1881 if (err) {
1882 free(*fileindex_path);
1883 *fileindex_path = NULL;
1884 got_fileindex_free(*fileindex);
1885 *fileindex = NULL;
1887 return err;
1890 struct bump_base_commit_id_arg {
1891 struct got_object_id *base_commit_id;
1892 const char *path;
1893 size_t path_len;
1894 const char *entry_name;
1895 got_worktree_checkout_cb progress_cb;
1896 void *progress_arg;
1899 /* Bump base commit ID of all files within an updated part of the work tree. */
1900 static const struct got_error *
1901 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1903 const struct got_error *err;
1904 struct bump_base_commit_id_arg *a = arg;
1906 if (a->entry_name) {
1907 if (strcmp(ie->path, a->path) != 0)
1908 return NULL;
1909 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1910 return NULL;
1912 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1913 SHA1_DIGEST_LENGTH) == 0)
1914 return NULL;
1916 if (a->progress_cb) {
1917 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1918 ie->path);
1919 if (err)
1920 return err;
1922 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1923 return NULL;
1926 static const struct got_error *
1927 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1929 const struct got_error *err = NULL;
1930 char *new_fileindex_path = NULL;
1931 FILE *new_index = NULL;
1932 struct timespec timeout;
1934 err = got_opentemp_named(&new_fileindex_path, &new_index,
1935 fileindex_path);
1936 if (err)
1937 goto done;
1939 err = got_fileindex_write(fileindex, new_index);
1940 if (err)
1941 goto done;
1943 if (rename(new_fileindex_path, fileindex_path) != 0) {
1944 err = got_error_from_errno3("rename", new_fileindex_path,
1945 fileindex_path);
1946 unlink(new_fileindex_path);
1950 * Sleep for a short amount of time to ensure that files modified after
1951 * this program exits have a different time stamp from the one which
1952 * was recorded in the file index.
1954 timeout.tv_sec = 0;
1955 timeout.tv_nsec = 1;
1956 nanosleep(&timeout, NULL);
1957 done:
1958 if (new_index)
1959 fclose(new_index);
1960 free(new_fileindex_path);
1961 return err;
1964 static const struct got_error *
1965 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1966 struct got_object_id **tree_id, const char *wt_relpath,
1967 struct got_worktree *worktree, struct got_repository *repo)
1969 const struct got_error *err = NULL;
1970 struct got_object_id *id = NULL;
1971 char *in_repo_path = NULL;
1972 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1974 *entry_type = GOT_OBJ_TYPE_ANY;
1975 *tree_relpath = NULL;
1976 *tree_id = NULL;
1978 if (wt_relpath[0] == '\0') {
1979 /* Check out all files within the work tree. */
1980 *entry_type = GOT_OBJ_TYPE_TREE;
1981 *tree_relpath = strdup("");
1982 if (*tree_relpath == NULL) {
1983 err = got_error_from_errno("strdup");
1984 goto done;
1986 err = got_object_id_by_path(tree_id, repo,
1987 worktree->base_commit_id, worktree->path_prefix);
1988 if (err)
1989 goto done;
1990 return NULL;
1993 /* Check out a subset of files in the work tree. */
1995 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1996 is_root_wt ? "" : "/", wt_relpath) == -1) {
1997 err = got_error_from_errno("asprintf");
1998 goto done;
2001 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2002 in_repo_path);
2003 if (err)
2004 goto done;
2006 free(in_repo_path);
2007 in_repo_path = NULL;
2009 err = got_object_get_type(entry_type, repo, id);
2010 if (err)
2011 goto done;
2013 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2014 /* Check out a single file. */
2015 if (strchr(wt_relpath, '/') == NULL) {
2016 /* Check out a single file in work tree's root dir. */
2017 in_repo_path = strdup(worktree->path_prefix);
2018 if (in_repo_path == NULL) {
2019 err = got_error_from_errno("strdup");
2020 goto done;
2022 *tree_relpath = strdup("");
2023 if (*tree_relpath == NULL) {
2024 err = got_error_from_errno("strdup");
2025 goto done;
2027 } else {
2028 /* Check out a single file in a subdirectory. */
2029 err = got_path_dirname(tree_relpath, wt_relpath);
2030 if (err)
2031 return err;
2032 if (asprintf(&in_repo_path, "%s%s%s",
2033 worktree->path_prefix, is_root_wt ? "" : "/",
2034 *tree_relpath) == -1) {
2035 err = got_error_from_errno("asprintf");
2036 goto done;
2039 err = got_object_id_by_path(tree_id, repo,
2040 worktree->base_commit_id, in_repo_path);
2041 } else {
2042 /* Check out all files within a subdirectory. */
2043 *tree_id = got_object_id_dup(id);
2044 if (*tree_id == NULL) {
2045 err = got_error_from_errno("got_object_id_dup");
2046 goto done;
2048 *tree_relpath = strdup(wt_relpath);
2049 if (*tree_relpath == NULL) {
2050 err = got_error_from_errno("strdup");
2051 goto done;
2054 done:
2055 free(id);
2056 free(in_repo_path);
2057 if (err) {
2058 *entry_type = GOT_OBJ_TYPE_ANY;
2059 free(*tree_relpath);
2060 *tree_relpath = NULL;
2061 free(*tree_id);
2062 *tree_id = NULL;
2064 return err;
2067 static const struct got_error *
2068 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2069 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2070 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2071 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2073 const struct got_error *err = NULL;
2074 struct got_commit_object *commit = NULL;
2075 struct got_tree_object *tree = NULL;
2076 struct got_fileindex_diff_tree_cb diff_cb;
2077 struct diff_cb_arg arg;
2079 err = ref_base_commit(worktree, repo);
2080 if (err) {
2081 if (!(err->code == GOT_ERR_ERRNO &&
2082 (errno == EACCES || errno == EROFS)))
2083 goto done;
2084 err = (*progress_cb)(progress_arg,
2085 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2086 if (err)
2087 return err;
2090 err = got_object_open_as_commit(&commit, repo,
2091 worktree->base_commit_id);
2092 if (err)
2093 goto done;
2095 err = got_object_open_as_tree(&tree, repo, tree_id);
2096 if (err)
2097 goto done;
2099 if (entry_name &&
2100 got_object_tree_find_entry(tree, entry_name) == NULL) {
2101 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2102 goto done;
2105 diff_cb.diff_old_new = diff_old_new;
2106 diff_cb.diff_old = diff_old;
2107 diff_cb.diff_new = diff_new;
2108 arg.fileindex = fileindex;
2109 arg.worktree = worktree;
2110 arg.repo = repo;
2111 arg.progress_cb = progress_cb;
2112 arg.progress_arg = progress_arg;
2113 arg.cancel_cb = cancel_cb;
2114 arg.cancel_arg = cancel_arg;
2115 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2116 entry_name, repo, &diff_cb, &arg);
2117 done:
2118 if (tree)
2119 got_object_tree_close(tree);
2120 if (commit)
2121 got_object_commit_close(commit);
2122 return err;
2125 const struct got_error *
2126 got_worktree_checkout_files(struct got_worktree *worktree,
2127 struct got_pathlist_head *paths, struct got_repository *repo,
2128 got_worktree_checkout_cb progress_cb, void *progress_arg,
2129 got_cancel_cb cancel_cb, void *cancel_arg)
2131 const struct got_error *err = NULL, *sync_err, *unlockerr;
2132 struct got_commit_object *commit = NULL;
2133 struct got_tree_object *tree = NULL;
2134 struct got_fileindex *fileindex = NULL;
2135 char *fileindex_path = NULL;
2136 struct got_pathlist_entry *pe;
2137 struct tree_path_data {
2138 SIMPLEQ_ENTRY(tree_path_data) entry;
2139 struct got_object_id *tree_id;
2140 int entry_type;
2141 char *relpath;
2142 char *entry_name;
2143 } *tpd = NULL;
2144 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2146 SIMPLEQ_INIT(&tree_paths);
2148 err = lock_worktree(worktree, LOCK_EX);
2149 if (err)
2150 return err;
2152 /* Map all specified paths to in-repository trees. */
2153 TAILQ_FOREACH(pe, paths, entry) {
2154 tpd = malloc(sizeof(*tpd));
2155 if (tpd == NULL) {
2156 err = got_error_from_errno("malloc");
2157 goto done;
2160 err = find_tree_entry_for_checkout(&tpd->entry_type,
2161 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2162 if (err) {
2163 free(tpd);
2164 goto done;
2167 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2168 err = got_path_basename(&tpd->entry_name, pe->path);
2169 if (err) {
2170 free(tpd->relpath);
2171 free(tpd->tree_id);
2172 free(tpd);
2173 goto done;
2175 } else
2176 tpd->entry_name = NULL;
2178 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2182 * Read the file index.
2183 * Checking out files is supposed to be an idempotent operation.
2184 * If the on-disk file index is incomplete we will try to complete it.
2186 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2187 if (err)
2188 goto done;
2190 tpd = SIMPLEQ_FIRST(&tree_paths);
2191 TAILQ_FOREACH(pe, paths, entry) {
2192 struct bump_base_commit_id_arg bbc_arg;
2194 err = checkout_files(worktree, fileindex, tpd->relpath,
2195 tpd->tree_id, tpd->entry_name, repo,
2196 progress_cb, progress_arg, cancel_cb, cancel_arg);
2197 if (err)
2198 break;
2200 bbc_arg.base_commit_id = worktree->base_commit_id;
2201 bbc_arg.entry_name = tpd->entry_name;
2202 bbc_arg.path = pe->path;
2203 bbc_arg.path_len = pe->path_len;
2204 bbc_arg.progress_cb = progress_cb;
2205 bbc_arg.progress_arg = progress_arg;
2206 err = got_fileindex_for_each_entry_safe(fileindex,
2207 bump_base_commit_id, &bbc_arg);
2208 if (err)
2209 break;
2211 tpd = SIMPLEQ_NEXT(tpd, entry);
2213 sync_err = sync_fileindex(fileindex, fileindex_path);
2214 if (sync_err && err == NULL)
2215 err = sync_err;
2216 done:
2217 free(fileindex_path);
2218 if (tree)
2219 got_object_tree_close(tree);
2220 if (commit)
2221 got_object_commit_close(commit);
2222 if (fileindex)
2223 got_fileindex_free(fileindex);
2224 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2225 tpd = SIMPLEQ_FIRST(&tree_paths);
2226 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2227 free(tpd->relpath);
2228 free(tpd->tree_id);
2229 free(tpd);
2231 unlockerr = lock_worktree(worktree, LOCK_SH);
2232 if (unlockerr && err == NULL)
2233 err = unlockerr;
2234 return err;
2237 struct merge_file_cb_arg {
2238 struct got_worktree *worktree;
2239 struct got_fileindex *fileindex;
2240 got_worktree_checkout_cb progress_cb;
2241 void *progress_arg;
2242 got_cancel_cb cancel_cb;
2243 void *cancel_arg;
2244 const char *label_orig;
2245 struct got_object_id *commit_id2;
2248 static const struct got_error *
2249 merge_file_cb(void *arg, struct got_blob_object *blob1,
2250 struct got_blob_object *blob2, struct got_object_id *id1,
2251 struct got_object_id *id2, const char *path1, const char *path2,
2252 mode_t mode1, mode_t mode2, struct got_repository *repo)
2254 static const struct got_error *err = NULL;
2255 struct merge_file_cb_arg *a = arg;
2256 struct got_fileindex_entry *ie;
2257 char *ondisk_path = NULL;
2258 struct stat sb;
2259 unsigned char status;
2260 int local_changes_subsumed;
2262 if (blob1 && blob2) {
2263 ie = got_fileindex_entry_get(a->fileindex, path2,
2264 strlen(path2));
2265 if (ie == NULL)
2266 return (*a->progress_cb)(a->progress_arg,
2267 GOT_STATUS_MISSING, path2);
2269 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2270 path2) == -1)
2271 return got_error_from_errno("asprintf");
2273 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2274 repo);
2275 if (err)
2276 goto done;
2278 if (status == GOT_STATUS_DELETE) {
2279 err = (*a->progress_cb)(a->progress_arg,
2280 GOT_STATUS_MERGE, path2);
2281 goto done;
2283 if (status != GOT_STATUS_NO_CHANGE &&
2284 status != GOT_STATUS_MODIFY &&
2285 status != GOT_STATUS_CONFLICT &&
2286 status != GOT_STATUS_ADD) {
2287 err = (*a->progress_cb)(a->progress_arg, status, path2);
2288 goto done;
2291 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2292 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2293 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2294 } else if (blob1) {
2295 ie = got_fileindex_entry_get(a->fileindex, path1,
2296 strlen(path1));
2297 if (ie == NULL)
2298 return (*a->progress_cb)(a->progress_arg,
2299 GOT_STATUS_MISSING, path1);
2301 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2302 path1) == -1)
2303 return got_error_from_errno("asprintf");
2305 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2306 repo);
2307 if (err)
2308 goto done;
2310 switch (status) {
2311 case GOT_STATUS_NO_CHANGE:
2312 err = (*a->progress_cb)(a->progress_arg,
2313 GOT_STATUS_DELETE, path1);
2314 if (err)
2315 goto done;
2316 err = remove_ondisk_file(a->worktree->root_path, path1);
2317 if (err)
2318 goto done;
2319 if (ie)
2320 got_fileindex_entry_mark_deleted_from_disk(ie);
2321 break;
2322 case GOT_STATUS_DELETE:
2323 case GOT_STATUS_MISSING:
2324 err = (*a->progress_cb)(a->progress_arg,
2325 GOT_STATUS_DELETE, path1);
2326 if (err)
2327 goto done;
2328 if (ie)
2329 got_fileindex_entry_mark_deleted_from_disk(ie);
2330 break;
2331 case GOT_STATUS_ADD:
2332 case GOT_STATUS_MODIFY:
2333 case GOT_STATUS_CONFLICT:
2334 err = (*a->progress_cb)(a->progress_arg,
2335 GOT_STATUS_CANNOT_DELETE, path1);
2336 if (err)
2337 goto done;
2338 break;
2339 case GOT_STATUS_OBSTRUCTED:
2340 err = (*a->progress_cb)(a->progress_arg, status, path1);
2341 if (err)
2342 goto done;
2343 break;
2344 default:
2345 break;
2347 } else if (blob2) {
2348 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2349 path2) == -1)
2350 return got_error_from_errno("asprintf");
2351 ie = got_fileindex_entry_get(a->fileindex, path2,
2352 strlen(path2));
2353 if (ie) {
2354 err = get_file_status(&status, &sb, ie, ondisk_path,
2355 -1, NULL, repo);
2356 if (err)
2357 goto done;
2358 if (status != GOT_STATUS_NO_CHANGE &&
2359 status != GOT_STATUS_MODIFY &&
2360 status != GOT_STATUS_CONFLICT &&
2361 status != GOT_STATUS_ADD) {
2362 err = (*a->progress_cb)(a->progress_arg,
2363 status, path2);
2364 goto done;
2366 err = merge_blob(&local_changes_subsumed, a->worktree,
2367 NULL, ondisk_path, path2, sb.st_mode,
2368 a->label_orig, blob2, a->commit_id2, repo,
2369 a->progress_cb,
2370 a->progress_arg);
2371 if (status == GOT_STATUS_DELETE) {
2372 err = got_fileindex_entry_update(ie,
2373 ondisk_path, blob2->id.sha1,
2374 a->worktree->base_commit_id->sha1, 0);
2375 if (err)
2376 goto done;
2378 } else {
2379 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2380 err = install_blob(a->worktree, ondisk_path, path2,
2381 /* XXX get this from parent tree! */
2382 GOT_DEFAULT_FILE_MODE,
2383 sb.st_mode, blob2, 0, 0, repo,
2384 a->progress_cb, a->progress_arg);
2385 if (err)
2386 goto done;
2387 err = got_fileindex_entry_alloc(&ie, path2);
2388 if (err)
2389 goto done;
2390 err = got_fileindex_entry_update(ie, ondisk_path,
2391 NULL, NULL, 1);
2392 if (err) {
2393 got_fileindex_entry_free(ie);
2394 goto done;
2396 err = got_fileindex_entry_add(a->fileindex, ie);
2397 if (err) {
2398 got_fileindex_entry_free(ie);
2399 goto done;
2403 done:
2404 free(ondisk_path);
2405 return err;
2408 struct check_merge_ok_arg {
2409 struct got_worktree *worktree;
2410 struct got_repository *repo;
2413 static const struct got_error *
2414 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2416 const struct got_error *err = NULL;
2417 struct check_merge_ok_arg *a = arg;
2418 unsigned char status;
2419 struct stat sb;
2420 char *ondisk_path;
2422 /* Reject merges into a work tree with mixed base commits. */
2423 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2424 SHA1_DIGEST_LENGTH))
2425 return got_error(GOT_ERR_MIXED_COMMITS);
2427 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2428 == -1)
2429 return got_error_from_errno("asprintf");
2431 /* Reject merges into a work tree with conflicted files. */
2432 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2433 if (err)
2434 return err;
2435 if (status == GOT_STATUS_CONFLICT)
2436 return got_error(GOT_ERR_CONFLICTS);
2438 return NULL;
2441 static const struct got_error *
2442 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2443 const char *fileindex_path, struct got_object_id *commit_id1,
2444 struct got_object_id *commit_id2, struct got_repository *repo,
2445 got_worktree_checkout_cb progress_cb, void *progress_arg,
2446 got_cancel_cb cancel_cb, void *cancel_arg)
2448 const struct got_error *err = NULL, *sync_err;
2449 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2450 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2451 struct merge_file_cb_arg arg;
2452 char *label_orig = NULL;
2454 if (commit_id1) {
2455 char *id_str;
2457 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2458 worktree->path_prefix);
2459 if (err)
2460 goto done;
2462 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2463 if (err)
2464 goto done;
2466 err = got_object_id_str(&id_str, commit_id1);
2467 if (err)
2468 goto done;
2470 if (asprintf(&label_orig, "%s: commit %s",
2471 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2472 err = got_error_from_errno("asprintf");
2473 free(id_str);
2474 goto done;
2476 free(id_str);
2479 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2480 worktree->path_prefix);
2481 if (err)
2482 goto done;
2484 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2485 if (err)
2486 goto done;
2488 arg.worktree = worktree;
2489 arg.fileindex = fileindex;
2490 arg.progress_cb = progress_cb;
2491 arg.progress_arg = progress_arg;
2492 arg.cancel_cb = cancel_cb;
2493 arg.cancel_arg = cancel_arg;
2494 arg.label_orig = label_orig;
2495 arg.commit_id2 = commit_id2;
2496 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2497 sync_err = sync_fileindex(fileindex, fileindex_path);
2498 if (sync_err && err == NULL)
2499 err = sync_err;
2500 done:
2501 if (tree1)
2502 got_object_tree_close(tree1);
2503 if (tree2)
2504 got_object_tree_close(tree2);
2505 free(label_orig);
2506 return err;
2509 const struct got_error *
2510 got_worktree_merge_files(struct got_worktree *worktree,
2511 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2512 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2513 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2515 const struct got_error *err, *unlockerr;
2516 char *fileindex_path = NULL;
2517 struct got_fileindex *fileindex = NULL;
2518 struct check_merge_ok_arg mok_arg;
2520 err = lock_worktree(worktree, LOCK_EX);
2521 if (err)
2522 return err;
2524 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2525 if (err)
2526 goto done;
2528 mok_arg.worktree = worktree;
2529 mok_arg.repo = repo;
2530 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2531 &mok_arg);
2532 if (err)
2533 goto done;
2535 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2536 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2537 done:
2538 if (fileindex)
2539 got_fileindex_free(fileindex);
2540 free(fileindex_path);
2541 unlockerr = lock_worktree(worktree, LOCK_SH);
2542 if (unlockerr && err == NULL)
2543 err = unlockerr;
2544 return err;
2547 struct diff_dir_cb_arg {
2548 struct got_fileindex *fileindex;
2549 struct got_worktree *worktree;
2550 const char *status_path;
2551 size_t status_path_len;
2552 struct got_repository *repo;
2553 got_worktree_status_cb status_cb;
2554 void *status_arg;
2555 got_cancel_cb cancel_cb;
2556 void *cancel_arg;
2557 /* A pathlist containing per-directory pathlists of ignore patterns. */
2558 struct got_pathlist_head ignores;
2559 int report_unchanged;
2560 int no_ignores;
2563 static const struct got_error *
2564 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2565 int dirfd, const char *de_name,
2566 got_worktree_status_cb status_cb, void *status_arg,
2567 struct got_repository *repo, int report_unchanged)
2569 const struct got_error *err = NULL;
2570 unsigned char status = GOT_STATUS_NO_CHANGE;
2571 unsigned char staged_status = get_staged_status(ie);
2572 struct stat sb;
2573 struct got_object_id blob_id, commit_id, staged_blob_id;
2574 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2575 struct got_object_id *staged_blob_idp = NULL;
2577 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2578 if (err)
2579 return err;
2581 if (status == GOT_STATUS_NO_CHANGE &&
2582 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2583 return NULL;
2585 if (got_fileindex_entry_has_blob(ie)) {
2586 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2587 blob_idp = &blob_id;
2589 if (got_fileindex_entry_has_commit(ie)) {
2590 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2591 commit_idp = &commit_id;
2593 if (staged_status == GOT_STATUS_ADD ||
2594 staged_status == GOT_STATUS_MODIFY) {
2595 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2596 SHA1_DIGEST_LENGTH);
2597 staged_blob_idp = &staged_blob_id;
2600 return (*status_cb)(status_arg, status, staged_status,
2601 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2604 static const struct got_error *
2605 status_old_new(void *arg, struct got_fileindex_entry *ie,
2606 struct dirent *de, const char *parent_path, int dirfd)
2608 const struct got_error *err = NULL;
2609 struct diff_dir_cb_arg *a = arg;
2610 char *abspath;
2612 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2613 return got_error(GOT_ERR_CANCELLED);
2615 if (got_path_cmp(parent_path, a->status_path,
2616 strlen(parent_path), a->status_path_len) != 0 &&
2617 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2618 return NULL;
2620 if (parent_path[0]) {
2621 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2622 parent_path, de->d_name) == -1)
2623 return got_error_from_errno("asprintf");
2624 } else {
2625 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2626 de->d_name) == -1)
2627 return got_error_from_errno("asprintf");
2630 err = report_file_status(ie, abspath, dirfd, de->d_name,
2631 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2632 free(abspath);
2633 return err;
2636 static const struct got_error *
2637 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2639 struct diff_dir_cb_arg *a = arg;
2640 struct got_object_id blob_id, commit_id;
2641 unsigned char status;
2643 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2644 return got_error(GOT_ERR_CANCELLED);
2646 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2647 return NULL;
2649 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2650 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2651 if (got_fileindex_entry_has_file_on_disk(ie))
2652 status = GOT_STATUS_MISSING;
2653 else
2654 status = GOT_STATUS_DELETE;
2655 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2656 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2659 void
2660 free_ignorelist(struct got_pathlist_head *ignorelist)
2662 struct got_pathlist_entry *pe;
2664 TAILQ_FOREACH(pe, ignorelist, entry)
2665 free((char *)pe->path);
2666 got_pathlist_free(ignorelist);
2669 void
2670 free_ignores(struct got_pathlist_head *ignores)
2672 struct got_pathlist_entry *pe;
2674 TAILQ_FOREACH(pe, ignores, entry) {
2675 struct got_pathlist_head *ignorelist = pe->data;
2676 free_ignorelist(ignorelist);
2677 free((char *)pe->path);
2679 got_pathlist_free(ignores);
2682 static const struct got_error *
2683 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2685 const struct got_error *err = NULL;
2686 struct got_pathlist_entry *pe = NULL;
2687 struct got_pathlist_head *ignorelist;
2688 char *line = NULL, *pattern, *dirpath = NULL;
2689 size_t linesize = 0;
2690 ssize_t linelen;
2692 ignorelist = calloc(1, sizeof(*ignorelist));
2693 if (ignorelist == NULL)
2694 return got_error_from_errno("calloc");
2695 TAILQ_INIT(ignorelist);
2697 while ((linelen = getline(&line, &linesize, f)) != -1) {
2698 if (linelen > 0 && line[linelen - 1] == '\n')
2699 line[linelen - 1] = '\0';
2701 /* Git's ignores may contain comments. */
2702 if (line[0] == '#')
2703 continue;
2705 /* Git's negated patterns are not (yet?) supported. */
2706 if (line[0] == '!')
2707 continue;
2709 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2710 line) == -1) {
2711 err = got_error_from_errno("asprintf");
2712 goto done;
2714 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2715 if (err)
2716 goto done;
2718 if (ferror(f)) {
2719 err = got_error_from_errno("getline");
2720 goto done;
2723 dirpath = strdup(path);
2724 if (dirpath == NULL) {
2725 err = got_error_from_errno("strdup");
2726 goto done;
2728 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2729 done:
2730 free(line);
2731 if (err || pe == NULL) {
2732 free(dirpath);
2733 free_ignorelist(ignorelist);
2735 return err;
2738 int
2739 match_ignores(struct got_pathlist_head *ignores, const char *path)
2741 struct got_pathlist_entry *pe;
2743 /* Handle patterns which match in all directories. */
2744 TAILQ_FOREACH(pe, ignores, entry) {
2745 struct got_pathlist_head *ignorelist = pe->data;
2746 struct got_pathlist_entry *pi;
2748 TAILQ_FOREACH(pi, ignorelist, entry) {
2749 const char *p, *pattern = pi->path;
2751 if (strncmp(pattern, "**/", 3) != 0)
2752 continue;
2753 pattern += 3;
2754 p = path;
2755 while (*p) {
2756 if (fnmatch(pattern, p,
2757 FNM_PATHNAME | FNM_LEADING_DIR)) {
2758 /* Retry in next directory. */
2759 while (*p && *p != '/')
2760 p++;
2761 while (*p == '/')
2762 p++;
2763 continue;
2765 return 1;
2771 * The ignores pathlist contains ignore lists from children before
2772 * parents, so we can find the most specific ignorelist by walking
2773 * ignores backwards.
2775 pe = TAILQ_LAST(ignores, got_pathlist_head);
2776 while (pe) {
2777 if (got_path_is_child(path, pe->path, pe->path_len)) {
2778 struct got_pathlist_head *ignorelist = pe->data;
2779 struct got_pathlist_entry *pi;
2780 TAILQ_FOREACH(pi, ignorelist, entry) {
2781 const char *pattern = pi->path;
2782 int flags = FNM_LEADING_DIR;
2783 if (strstr(pattern, "/**/") == NULL)
2784 flags |= FNM_PATHNAME;
2785 if (fnmatch(pattern, path, flags))
2786 continue;
2787 return 1;
2790 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2793 return 0;
2796 static const struct got_error *
2797 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2798 const char *path, int dirfd, const char *ignores_filename)
2800 const struct got_error *err = NULL;
2801 char *ignorespath;
2802 int fd = -1;
2803 FILE *ignoresfile = NULL;
2805 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2806 path[0] ? "/" : "", ignores_filename) == -1)
2807 return got_error_from_errno("asprintf");
2809 if (dirfd != -1) {
2810 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2811 if (fd == -1) {
2812 if (errno != ENOENT && errno != EACCES)
2813 err = got_error_from_errno2("openat",
2814 ignorespath);
2815 } else {
2816 ignoresfile = fdopen(fd, "r");
2817 if (ignoresfile == NULL)
2818 err = got_error_from_errno2("fdopen",
2819 ignorespath);
2820 else {
2821 fd = -1;
2822 err = read_ignores(ignores, path, ignoresfile);
2825 } else {
2826 ignoresfile = fopen(ignorespath, "r");
2827 if (ignoresfile == NULL) {
2828 if (errno != ENOENT && errno != EACCES)
2829 err = got_error_from_errno2("fopen",
2830 ignorespath);
2831 } else
2832 err = read_ignores(ignores, path, ignoresfile);
2835 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2836 err = got_error_from_errno2("fclose", path);
2837 if (fd != -1 && close(fd) == -1 && err == NULL)
2838 err = got_error_from_errno2("close", path);
2839 free(ignorespath);
2840 return err;
2843 static const struct got_error *
2844 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2846 const struct got_error *err = NULL;
2847 struct diff_dir_cb_arg *a = arg;
2848 char *path = NULL;
2850 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2851 return got_error(GOT_ERR_CANCELLED);
2853 if (parent_path[0]) {
2854 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2855 return got_error_from_errno("asprintf");
2856 } else {
2857 path = de->d_name;
2860 if (de->d_type != DT_DIR &&
2861 got_path_is_child(path, a->status_path, a->status_path_len)
2862 && !match_ignores(&a->ignores, path))
2863 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2864 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2865 if (parent_path[0])
2866 free(path);
2867 return err;
2870 static const struct got_error *
2871 status_traverse(void *arg, const char *path, int dirfd)
2873 const struct got_error *err = NULL;
2874 struct diff_dir_cb_arg *a = arg;
2876 if (a->no_ignores)
2877 return NULL;
2879 err = add_ignores(&a->ignores, a->worktree->root_path,
2880 path, dirfd, ".cvsignore");
2881 if (err)
2882 return err;
2884 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2885 dirfd, ".gitignore");
2887 return err;
2890 static const struct got_error *
2891 report_single_file_status(const char *path, const char *ondisk_path,
2892 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2893 void *status_arg, struct got_repository *repo, int report_unchanged)
2895 struct got_fileindex_entry *ie;
2896 struct stat sb;
2898 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2899 if (ie)
2900 return report_file_status(ie, ondisk_path, -1, NULL,
2901 status_cb, status_arg, repo, report_unchanged);
2903 if (lstat(ondisk_path, &sb) == -1) {
2904 if (errno != ENOENT)
2905 return got_error_from_errno2("lstat", ondisk_path);
2906 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2907 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2908 return NULL;
2911 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
2912 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2913 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2915 return NULL;
2918 static const struct got_error *
2919 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2920 const char *root_path, const char *path)
2922 const struct got_error *err;
2923 char *parent_path, *next_parent_path = NULL;
2925 err = add_ignores(ignores, root_path, "", -1,
2926 ".cvsignore");
2927 if (err)
2928 return err;
2930 err = add_ignores(ignores, root_path, "", -1,
2931 ".gitignore");
2932 if (err)
2933 return err;
2935 err = got_path_dirname(&parent_path, path);
2936 if (err) {
2937 if (err->code == GOT_ERR_BAD_PATH)
2938 return NULL; /* cannot traverse parent */
2939 return err;
2941 for (;;) {
2942 err = add_ignores(ignores, root_path, parent_path, -1,
2943 ".cvsignore");
2944 if (err)
2945 break;
2946 err = add_ignores(ignores, root_path, parent_path, -1,
2947 ".gitignore");
2948 if (err)
2949 break;
2950 err = got_path_dirname(&next_parent_path, parent_path);
2951 if (err) {
2952 if (err->code == GOT_ERR_BAD_PATH)
2953 err = NULL; /* traversed everything */
2954 break;
2956 free(parent_path);
2957 parent_path = next_parent_path;
2958 next_parent_path = NULL;
2961 free(parent_path);
2962 free(next_parent_path);
2963 return err;
2966 static const struct got_error *
2967 worktree_status(struct got_worktree *worktree, const char *path,
2968 struct got_fileindex *fileindex, struct got_repository *repo,
2969 got_worktree_status_cb status_cb, void *status_arg,
2970 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2971 int report_unchanged)
2973 const struct got_error *err = NULL;
2974 int fd = -1;
2975 struct got_fileindex_diff_dir_cb fdiff_cb;
2976 struct diff_dir_cb_arg arg;
2977 char *ondisk_path = NULL;
2979 TAILQ_INIT(&arg.ignores);
2981 if (asprintf(&ondisk_path, "%s%s%s",
2982 worktree->root_path, path[0] ? "/" : "", path) == -1)
2983 return got_error_from_errno("asprintf");
2985 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2986 if (fd == -1) {
2987 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
2988 errno != ELOOP)
2989 err = got_error_from_errno2("open", ondisk_path);
2990 else
2991 err = report_single_file_status(path, ondisk_path,
2992 fileindex, status_cb, status_arg, repo,
2993 report_unchanged);
2994 } else {
2995 fdiff_cb.diff_old_new = status_old_new;
2996 fdiff_cb.diff_old = status_old;
2997 fdiff_cb.diff_new = status_new;
2998 fdiff_cb.diff_traverse = status_traverse;
2999 arg.fileindex = fileindex;
3000 arg.worktree = worktree;
3001 arg.status_path = path;
3002 arg.status_path_len = strlen(path);
3003 arg.repo = repo;
3004 arg.status_cb = status_cb;
3005 arg.status_arg = status_arg;
3006 arg.cancel_cb = cancel_cb;
3007 arg.cancel_arg = cancel_arg;
3008 arg.report_unchanged = report_unchanged;
3009 arg.no_ignores = no_ignores;
3010 if (!no_ignores) {
3011 err = add_ignores_from_parent_paths(&arg.ignores,
3012 worktree->root_path, path);
3013 if (err)
3014 goto done;
3016 err = got_fileindex_diff_dir(fileindex, fd,
3017 worktree->root_path, path, repo, &fdiff_cb, &arg);
3019 done:
3020 free_ignores(&arg.ignores);
3021 if (fd != -1 && close(fd) != 0 && err == NULL)
3022 err = got_error_from_errno("close");
3023 free(ondisk_path);
3024 return err;
3027 const struct got_error *
3028 got_worktree_status(struct got_worktree *worktree,
3029 struct got_pathlist_head *paths, struct got_repository *repo,
3030 got_worktree_status_cb status_cb, void *status_arg,
3031 got_cancel_cb cancel_cb, void *cancel_arg)
3033 const struct got_error *err = NULL;
3034 char *fileindex_path = NULL;
3035 struct got_fileindex *fileindex = NULL;
3036 struct got_pathlist_entry *pe;
3038 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3039 if (err)
3040 return err;
3042 TAILQ_FOREACH(pe, paths, entry) {
3043 err = worktree_status(worktree, pe->path, fileindex, repo,
3044 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3045 if (err)
3046 break;
3048 free(fileindex_path);
3049 got_fileindex_free(fileindex);
3050 return err;
3053 const struct got_error *
3054 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3055 const char *arg)
3057 const struct got_error *err = NULL;
3058 char *resolved = NULL, *cwd = NULL, *path = NULL;
3059 size_t len;
3060 struct stat sb;
3062 *wt_path = NULL;
3064 cwd = getcwd(NULL, 0);
3065 if (cwd == NULL)
3066 return got_error_from_errno("getcwd");
3068 if (lstat(arg, &sb) == -1) {
3069 if (errno != ENOENT) {
3070 err = got_error_from_errno2("lstat", arg);
3071 goto done;
3074 if (S_ISLNK(sb.st_mode)) {
3076 * We cannot use realpath(3) with symlinks since we want to
3077 * operate on the symlink itself.
3078 * But we can make the path absolute, assuming it is relative
3079 * to the current working directory, and then canonicalize it.
3081 char *abspath = NULL;
3082 char canonpath[PATH_MAX];
3083 if (!got_path_is_absolute(arg)) {
3084 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3085 err = got_error_from_errno("asprintf");
3086 goto done;
3090 err = got_canonpath(abspath ? abspath : arg, canonpath,
3091 sizeof(canonpath));
3092 if (err)
3093 goto done;
3094 resolved = strdup(canonpath);
3095 if (resolved == NULL) {
3096 err = got_error_from_errno("strdup");
3097 goto done;
3099 } else {
3100 resolved = realpath(arg, NULL);
3101 if (resolved == NULL) {
3102 if (errno != ENOENT) {
3103 err = got_error_from_errno2("realpath", arg);
3104 goto done;
3106 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3107 err = got_error_from_errno("asprintf");
3108 goto done;
3113 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3114 strlen(got_worktree_get_root_path(worktree)))) {
3115 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3116 goto done;
3119 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3120 err = got_path_skip_common_ancestor(&path,
3121 got_worktree_get_root_path(worktree), resolved);
3122 if (err)
3123 goto done;
3124 } else {
3125 path = strdup("");
3126 if (path == NULL) {
3127 err = got_error_from_errno("strdup");
3128 goto done;
3132 /* XXX status walk can't deal with trailing slash! */
3133 len = strlen(path);
3134 while (len > 0 && path[len - 1] == '/') {
3135 path[len - 1] = '\0';
3136 len--;
3138 done:
3139 free(resolved);
3140 free(cwd);
3141 if (err == NULL)
3142 *wt_path = path;
3143 else
3144 free(path);
3145 return err;
3148 struct schedule_addition_args {
3149 struct got_worktree *worktree;
3150 struct got_fileindex *fileindex;
3151 got_worktree_checkout_cb progress_cb;
3152 void *progress_arg;
3153 struct got_repository *repo;
3156 static const struct got_error *
3157 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3158 const char *relpath, struct got_object_id *blob_id,
3159 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3160 int dirfd, const char *de_name)
3162 struct schedule_addition_args *a = arg;
3163 const struct got_error *err = NULL;
3164 struct got_fileindex_entry *ie;
3165 struct stat sb;
3166 char *ondisk_path;
3168 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3169 relpath) == -1)
3170 return got_error_from_errno("asprintf");
3172 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3173 if (ie) {
3174 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3175 de_name, a->repo);
3176 if (err)
3177 goto done;
3178 /* Re-adding an existing entry is a no-op. */
3179 if (status == GOT_STATUS_ADD)
3180 goto done;
3181 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3182 if (err)
3183 goto done;
3186 if (status != GOT_STATUS_UNVERSIONED) {
3187 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3188 goto done;
3191 err = got_fileindex_entry_alloc(&ie, relpath);
3192 if (err)
3193 goto done;
3194 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3195 if (err) {
3196 got_fileindex_entry_free(ie);
3197 goto done;
3199 err = got_fileindex_entry_add(a->fileindex, ie);
3200 if (err) {
3201 got_fileindex_entry_free(ie);
3202 goto done;
3204 done:
3205 free(ondisk_path);
3206 if (err)
3207 return err;
3208 if (status == GOT_STATUS_ADD)
3209 return NULL;
3210 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3213 const struct got_error *
3214 got_worktree_schedule_add(struct got_worktree *worktree,
3215 struct got_pathlist_head *paths,
3216 got_worktree_checkout_cb progress_cb, void *progress_arg,
3217 struct got_repository *repo, int no_ignores)
3219 struct got_fileindex *fileindex = NULL;
3220 char *fileindex_path = NULL;
3221 const struct got_error *err = NULL, *sync_err, *unlockerr;
3222 struct got_pathlist_entry *pe;
3223 struct schedule_addition_args saa;
3225 err = lock_worktree(worktree, LOCK_EX);
3226 if (err)
3227 return err;
3229 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3230 if (err)
3231 goto done;
3233 saa.worktree = worktree;
3234 saa.fileindex = fileindex;
3235 saa.progress_cb = progress_cb;
3236 saa.progress_arg = progress_arg;
3237 saa.repo = repo;
3239 TAILQ_FOREACH(pe, paths, entry) {
3240 err = worktree_status(worktree, pe->path, fileindex, repo,
3241 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3242 if (err)
3243 break;
3245 sync_err = sync_fileindex(fileindex, fileindex_path);
3246 if (sync_err && err == NULL)
3247 err = sync_err;
3248 done:
3249 free(fileindex_path);
3250 if (fileindex)
3251 got_fileindex_free(fileindex);
3252 unlockerr = lock_worktree(worktree, LOCK_SH);
3253 if (unlockerr && err == NULL)
3254 err = unlockerr;
3255 return err;
3258 struct schedule_deletion_args {
3259 struct got_worktree *worktree;
3260 struct got_fileindex *fileindex;
3261 got_worktree_delete_cb progress_cb;
3262 void *progress_arg;
3263 struct got_repository *repo;
3264 int delete_local_mods;
3265 int keep_on_disk;
3268 static const struct got_error *
3269 schedule_for_deletion(void *arg, unsigned char status,
3270 unsigned char staged_status, const char *relpath,
3271 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3272 struct got_object_id *commit_id, int dirfd, const char *de_name)
3274 struct schedule_deletion_args *a = arg;
3275 const struct got_error *err = NULL;
3276 struct got_fileindex_entry *ie = NULL;
3277 struct stat sb;
3278 char *ondisk_path, *parent = NULL;
3280 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3281 if (ie == NULL)
3282 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3284 staged_status = get_staged_status(ie);
3285 if (staged_status != GOT_STATUS_NO_CHANGE) {
3286 if (staged_status == GOT_STATUS_DELETE)
3287 return NULL;
3288 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3291 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3292 relpath) == -1)
3293 return got_error_from_errno("asprintf");
3295 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3296 a->repo);
3297 if (err)
3298 goto done;
3300 if (status != GOT_STATUS_NO_CHANGE) {
3301 if (status == GOT_STATUS_DELETE)
3302 goto done;
3303 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3304 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3305 goto done;
3307 if (status != GOT_STATUS_MODIFY &&
3308 status != GOT_STATUS_MISSING) {
3309 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3310 goto done;
3314 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3315 if (dirfd != -1) {
3316 if (unlinkat(dirfd, de_name, 0) != 0) {
3317 err = got_error_from_errno2("unlinkat",
3318 ondisk_path);
3319 goto done;
3321 } else if (unlink(ondisk_path) != 0) {
3322 err = got_error_from_errno2("unlink", ondisk_path);
3323 goto done;
3326 parent = dirname(ondisk_path);
3328 if (parent == NULL) {
3329 err = got_error_from_errno2("dirname", ondisk_path);
3330 goto done;
3332 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3333 if (rmdir(parent) == -1) {
3334 if (errno != ENOTEMPTY)
3335 err = got_error_from_errno2("rmdir",
3336 parent);
3337 break;
3339 parent = dirname(parent);
3340 if (parent == NULL) {
3341 err = got_error_from_errno2("dirname", parent);
3342 goto done;
3347 got_fileindex_entry_mark_deleted_from_disk(ie);
3348 done:
3349 free(ondisk_path);
3350 if (err)
3351 return err;
3352 if (status == GOT_STATUS_DELETE)
3353 return NULL;
3354 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3355 staged_status, relpath);
3358 const struct got_error *
3359 got_worktree_schedule_delete(struct got_worktree *worktree,
3360 struct got_pathlist_head *paths, int delete_local_mods,
3361 got_worktree_delete_cb progress_cb, void *progress_arg,
3362 struct got_repository *repo, int keep_on_disk)
3364 struct got_fileindex *fileindex = NULL;
3365 char *fileindex_path = NULL;
3366 const struct got_error *err = NULL, *sync_err, *unlockerr;
3367 struct got_pathlist_entry *pe;
3368 struct schedule_deletion_args sda;
3370 err = lock_worktree(worktree, LOCK_EX);
3371 if (err)
3372 return err;
3374 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3375 if (err)
3376 goto done;
3378 sda.worktree = worktree;
3379 sda.fileindex = fileindex;
3380 sda.progress_cb = progress_cb;
3381 sda.progress_arg = progress_arg;
3382 sda.repo = repo;
3383 sda.delete_local_mods = delete_local_mods;
3384 sda.keep_on_disk = keep_on_disk;
3386 TAILQ_FOREACH(pe, paths, entry) {
3387 err = worktree_status(worktree, pe->path, fileindex, repo,
3388 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3389 if (err)
3390 break;
3392 sync_err = sync_fileindex(fileindex, fileindex_path);
3393 if (sync_err && err == NULL)
3394 err = sync_err;
3395 done:
3396 free(fileindex_path);
3397 if (fileindex)
3398 got_fileindex_free(fileindex);
3399 unlockerr = lock_worktree(worktree, LOCK_SH);
3400 if (unlockerr && err == NULL)
3401 err = unlockerr;
3402 return err;
3405 static const struct got_error *
3406 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3408 const struct got_error *err = NULL;
3409 char *line = NULL;
3410 size_t linesize = 0, n;
3411 ssize_t linelen;
3413 linelen = getline(&line, &linesize, infile);
3414 if (linelen == -1) {
3415 if (ferror(infile)) {
3416 err = got_error_from_errno("getline");
3417 goto done;
3419 return NULL;
3421 if (outfile) {
3422 n = fwrite(line, 1, linelen, outfile);
3423 if (n != linelen) {
3424 err = got_ferror(outfile, GOT_ERR_IO);
3425 goto done;
3428 if (rejectfile) {
3429 n = fwrite(line, 1, linelen, rejectfile);
3430 if (n != linelen)
3431 err = got_ferror(outfile, GOT_ERR_IO);
3433 done:
3434 free(line);
3435 return err;
3438 static const struct got_error *
3439 skip_one_line(FILE *f)
3441 char *line = NULL;
3442 size_t linesize = 0;
3443 ssize_t linelen;
3445 linelen = getline(&line, &linesize, f);
3446 if (linelen == -1) {
3447 if (ferror(f))
3448 return got_error_from_errno("getline");
3449 return NULL;
3451 free(line);
3452 return NULL;
3455 static const struct got_error *
3456 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3457 int start_old, int end_old, int start_new, int end_new,
3458 FILE *outfile, FILE *rejectfile)
3460 const struct got_error *err;
3462 /* Copy old file's lines leading up to patch. */
3463 while (!feof(f1) && *line_cur1 < start_old) {
3464 err = copy_one_line(f1, outfile, NULL);
3465 if (err)
3466 return err;
3467 (*line_cur1)++;
3469 /* Skip new file's lines leading up to patch. */
3470 while (!feof(f2) && *line_cur2 < start_new) {
3471 if (rejectfile)
3472 err = copy_one_line(f2, NULL, rejectfile);
3473 else
3474 err = skip_one_line(f2);
3475 if (err)
3476 return err;
3477 (*line_cur2)++;
3479 /* Copy patched lines. */
3480 while (!feof(f2) && *line_cur2 <= end_new) {
3481 err = copy_one_line(f2, outfile, NULL);
3482 if (err)
3483 return err;
3484 (*line_cur2)++;
3486 /* Skip over old file's replaced lines. */
3487 while (!feof(f1) && *line_cur1 <= end_old) {
3488 if (rejectfile)
3489 err = copy_one_line(f1, NULL, rejectfile);
3490 else
3491 err = skip_one_line(f1);
3492 if (err)
3493 return err;
3494 (*line_cur1)++;
3497 return NULL;
3500 static const struct got_error *
3501 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3502 FILE *outfile, FILE *rejectfile)
3504 const struct got_error *err;
3506 if (outfile) {
3507 /* Copy old file's lines until EOF. */
3508 while (!feof(f1)) {
3509 err = copy_one_line(f1, outfile, NULL);
3510 if (err)
3511 return err;
3512 (*line_cur1)++;
3515 if (rejectfile) {
3516 /* Copy new file's lines until EOF. */
3517 while (!feof(f2)) {
3518 err = copy_one_line(f2, NULL, rejectfile);
3519 if (err)
3520 return err;
3521 (*line_cur2)++;
3525 return NULL;
3528 static const struct got_error *
3529 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3530 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3531 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3532 int *line_cur2, FILE *outfile, FILE *rejectfile,
3533 got_worktree_patch_cb patch_cb, void *patch_arg)
3535 const struct got_error *err = NULL;
3536 int start_old = change->cv.a;
3537 int end_old = change->cv.b;
3538 int start_new = change->cv.c;
3539 int end_new = change->cv.d;
3540 long pos1, pos2;
3541 FILE *hunkfile;
3543 *choice = GOT_PATCH_CHOICE_NONE;
3545 hunkfile = got_opentemp();
3546 if (hunkfile == NULL)
3547 return got_error_from_errno("got_opentemp");
3549 pos1 = ftell(f1);
3550 pos2 = ftell(f2);
3552 /* XXX TODO needs error checking */
3553 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3555 if (fseek(f1, pos1, SEEK_SET) == -1) {
3556 err = got_ferror(f1, GOT_ERR_IO);
3557 goto done;
3559 if (fseek(f2, pos2, SEEK_SET) == -1) {
3560 err = got_ferror(f1, GOT_ERR_IO);
3561 goto done;
3563 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3564 err = got_ferror(hunkfile, GOT_ERR_IO);
3565 goto done;
3568 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3569 hunkfile, n, nchanges);
3570 if (err)
3571 goto done;
3573 switch (*choice) {
3574 case GOT_PATCH_CHOICE_YES:
3575 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3576 end_old, start_new, end_new, outfile, rejectfile);
3577 break;
3578 case GOT_PATCH_CHOICE_NO:
3579 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3580 end_old, start_new, end_new, rejectfile, outfile);
3581 break;
3582 case GOT_PATCH_CHOICE_QUIT:
3583 break;
3584 default:
3585 err = got_error(GOT_ERR_PATCH_CHOICE);
3586 break;
3588 done:
3589 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3590 err = got_error_from_errno("fclose");
3591 return err;
3594 struct revert_file_args {
3595 struct got_worktree *worktree;
3596 struct got_fileindex *fileindex;
3597 got_worktree_checkout_cb progress_cb;
3598 void *progress_arg;
3599 got_worktree_patch_cb patch_cb;
3600 void *patch_arg;
3601 struct got_repository *repo;
3604 static const struct got_error *
3605 create_patched_content(char **path_outfile, int reverse_patch,
3606 struct got_object_id *blob_id, const char *path2,
3607 int dirfd2, const char *de_name2,
3608 const char *relpath, struct got_repository *repo,
3609 got_worktree_patch_cb patch_cb, void *patch_arg)
3611 const struct got_error *err;
3612 struct got_blob_object *blob = NULL;
3613 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3614 int fd2 = -1;
3615 char *path1 = NULL, *id_str = NULL;
3616 struct stat sb1, sb2;
3617 struct got_diff_changes *changes = NULL;
3618 struct got_diff_state *ds = NULL;
3619 struct got_diff_args *args = NULL;
3620 struct got_diff_change *change;
3621 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3622 int n = 0;
3624 *path_outfile = NULL;
3626 err = got_object_id_str(&id_str, blob_id);
3627 if (err)
3628 return err;
3630 if (dirfd2 != -1) {
3631 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3632 if (fd2 == -1) {
3633 err = got_error_from_errno2("openat", path2);
3634 goto done;
3636 } else {
3637 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3638 if (fd2 == -1) {
3639 err = got_error_from_errno2("open", path2);
3640 goto done;
3643 if (fstat(fd2, &sb2) == -1) {
3644 err = got_error_from_errno2("fstat", path2);
3645 goto done;
3648 f2 = fdopen(fd2, "r");
3649 if (f2 == NULL) {
3650 err = got_error_from_errno2("fdopen", path2);
3651 goto done;
3653 fd2 = -1;
3655 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3656 if (err)
3657 goto done;
3659 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3660 if (err)
3661 goto done;
3663 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3664 if (err)
3665 goto done;
3667 if (stat(path1, &sb1) == -1) {
3668 err = got_error_from_errno2("stat", path1);
3669 goto done;
3672 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3673 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3674 if (err)
3675 goto done;
3677 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3678 if (err)
3679 goto done;
3681 if (fseek(f1, 0L, SEEK_SET) == -1)
3682 return got_ferror(f1, GOT_ERR_IO);
3683 if (fseek(f2, 0L, SEEK_SET) == -1)
3684 return got_ferror(f2, GOT_ERR_IO);
3685 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3686 int choice;
3687 err = apply_or_reject_change(&choice, change, ++n,
3688 changes->nchanges, ds, args, diff_flags, relpath,
3689 f1, f2, &line_cur1, &line_cur2,
3690 reverse_patch ? NULL : outfile,
3691 reverse_patch ? outfile : NULL,
3692 patch_cb, patch_arg);
3693 if (err)
3694 goto done;
3695 if (choice == GOT_PATCH_CHOICE_YES)
3696 have_content = 1;
3697 else if (choice == GOT_PATCH_CHOICE_QUIT)
3698 break;
3700 if (have_content) {
3701 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3702 reverse_patch ? NULL : outfile,
3703 reverse_patch ? outfile : NULL);
3704 if (err)
3705 goto done;
3707 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3708 err = got_error_from_errno2("chmod", path2);
3709 goto done;
3712 done:
3713 free(id_str);
3714 if (blob)
3715 got_object_blob_close(blob);
3716 if (f1 && fclose(f1) == EOF && err == NULL)
3717 err = got_error_from_errno2("fclose", path1);
3718 if (f2 && fclose(f2) == EOF && err == NULL)
3719 err = got_error_from_errno2("fclose", path2);
3720 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3721 err = got_error_from_errno2("close", path2);
3722 if (outfile && fclose(outfile) == EOF && err == NULL)
3723 err = got_error_from_errno2("fclose", *path_outfile);
3724 if (path1 && unlink(path1) == -1 && err == NULL)
3725 err = got_error_from_errno2("unlink", path1);
3726 if (err || !have_content) {
3727 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3728 err = got_error_from_errno2("unlink", *path_outfile);
3729 free(*path_outfile);
3730 *path_outfile = NULL;
3732 free(args);
3733 if (ds) {
3734 got_diff_state_free(ds);
3735 free(ds);
3737 if (changes)
3738 got_diff_free_changes(changes);
3739 free(path1);
3740 return err;
3743 static const struct got_error *
3744 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3745 const char *relpath, struct got_object_id *blob_id,
3746 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3747 int dirfd, const char *de_name)
3749 struct revert_file_args *a = arg;
3750 const struct got_error *err = NULL;
3751 char *parent_path = NULL;
3752 struct got_fileindex_entry *ie;
3753 struct got_tree_object *tree = NULL;
3754 struct got_object_id *tree_id = NULL;
3755 const struct got_tree_entry *te = NULL;
3756 char *tree_path = NULL, *te_name;
3757 char *ondisk_path = NULL, *path_content = NULL;
3758 struct got_blob_object *blob = NULL;
3760 /* Reverting a staged deletion is a no-op. */
3761 if (status == GOT_STATUS_DELETE &&
3762 staged_status != GOT_STATUS_NO_CHANGE)
3763 return NULL;
3765 if (status == GOT_STATUS_UNVERSIONED)
3766 return (*a->progress_cb)(a->progress_arg,
3767 GOT_STATUS_UNVERSIONED, relpath);
3769 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3770 if (ie == NULL)
3771 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3773 /* Construct in-repository path of tree which contains this blob. */
3774 err = got_path_dirname(&parent_path, ie->path);
3775 if (err) {
3776 if (err->code != GOT_ERR_BAD_PATH)
3777 goto done;
3778 parent_path = strdup("/");
3779 if (parent_path == NULL) {
3780 err = got_error_from_errno("strdup");
3781 goto done;
3784 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3785 tree_path = strdup(parent_path);
3786 if (tree_path == NULL) {
3787 err = got_error_from_errno("strdup");
3788 goto done;
3790 } else {
3791 if (got_path_is_root_dir(parent_path)) {
3792 tree_path = strdup(a->worktree->path_prefix);
3793 if (tree_path == NULL) {
3794 err = got_error_from_errno("strdup");
3795 goto done;
3797 } else {
3798 if (asprintf(&tree_path, "%s/%s",
3799 a->worktree->path_prefix, parent_path) == -1) {
3800 err = got_error_from_errno("asprintf");
3801 goto done;
3806 err = got_object_id_by_path(&tree_id, a->repo,
3807 a->worktree->base_commit_id, tree_path);
3808 if (err) {
3809 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3810 (status == GOT_STATUS_ADD ||
3811 staged_status == GOT_STATUS_ADD)))
3812 goto done;
3813 } else {
3814 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3815 if (err)
3816 goto done;
3818 te_name = basename(ie->path);
3819 if (te_name == NULL) {
3820 err = got_error_from_errno2("basename", ie->path);
3821 goto done;
3824 te = got_object_tree_find_entry(tree, te_name);
3825 if (te == NULL && status != GOT_STATUS_ADD &&
3826 staged_status != GOT_STATUS_ADD) {
3827 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3828 goto done;
3832 switch (status) {
3833 case GOT_STATUS_ADD:
3834 if (a->patch_cb) {
3835 int choice = GOT_PATCH_CHOICE_NONE;
3836 err = (*a->patch_cb)(&choice, a->patch_arg,
3837 status, ie->path, NULL, 1, 1);
3838 if (err)
3839 goto done;
3840 if (choice != GOT_PATCH_CHOICE_YES)
3841 break;
3843 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3844 ie->path);
3845 if (err)
3846 goto done;
3847 got_fileindex_entry_remove(a->fileindex, ie);
3848 break;
3849 case GOT_STATUS_DELETE:
3850 if (a->patch_cb) {
3851 int choice = GOT_PATCH_CHOICE_NONE;
3852 err = (*a->patch_cb)(&choice, a->patch_arg,
3853 status, ie->path, NULL, 1, 1);
3854 if (err)
3855 goto done;
3856 if (choice != GOT_PATCH_CHOICE_YES)
3857 break;
3859 /* fall through */
3860 case GOT_STATUS_MODIFY:
3861 case GOT_STATUS_MODE_CHANGE:
3862 case GOT_STATUS_CONFLICT:
3863 case GOT_STATUS_MISSING: {
3864 struct got_object_id id;
3865 if (staged_status == GOT_STATUS_ADD ||
3866 staged_status == GOT_STATUS_MODIFY) {
3867 memcpy(id.sha1, ie->staged_blob_sha1,
3868 SHA1_DIGEST_LENGTH);
3869 } else
3870 memcpy(id.sha1, ie->blob_sha1,
3871 SHA1_DIGEST_LENGTH);
3872 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3873 if (err)
3874 goto done;
3876 if (asprintf(&ondisk_path, "%s/%s",
3877 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3878 err = got_error_from_errno("asprintf");
3879 goto done;
3882 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3883 status == GOT_STATUS_CONFLICT)) {
3884 err = create_patched_content(&path_content, 1, &id,
3885 ondisk_path, dirfd, de_name, ie->path, a->repo,
3886 a->patch_cb, a->patch_arg);
3887 if (err || path_content == NULL)
3888 break;
3889 if (rename(path_content, ondisk_path) == -1) {
3890 err = got_error_from_errno3("rename",
3891 path_content, ondisk_path);
3892 goto done;
3894 } else {
3895 err = install_blob(a->worktree, ondisk_path, ie->path,
3896 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3897 got_fileindex_perms_to_st(ie), blob, 0, 1,
3898 a->repo, a->progress_cb, a->progress_arg);
3899 if (err)
3900 goto done;
3901 if (status == GOT_STATUS_DELETE ||
3902 status == GOT_STATUS_MODE_CHANGE) {
3903 err = got_fileindex_entry_update(ie,
3904 ondisk_path, blob->id.sha1,
3905 a->worktree->base_commit_id->sha1, 1);
3906 if (err)
3907 goto done;
3910 break;
3912 default:
3913 break;
3915 done:
3916 free(ondisk_path);
3917 free(path_content);
3918 free(parent_path);
3919 free(tree_path);
3920 if (blob)
3921 got_object_blob_close(blob);
3922 if (tree)
3923 got_object_tree_close(tree);
3924 free(tree_id);
3925 return err;
3928 const struct got_error *
3929 got_worktree_revert(struct got_worktree *worktree,
3930 struct got_pathlist_head *paths,
3931 got_worktree_checkout_cb progress_cb, void *progress_arg,
3932 got_worktree_patch_cb patch_cb, void *patch_arg,
3933 struct got_repository *repo)
3935 struct got_fileindex *fileindex = NULL;
3936 char *fileindex_path = NULL;
3937 const struct got_error *err = NULL, *unlockerr = NULL;
3938 const struct got_error *sync_err = NULL;
3939 struct got_pathlist_entry *pe;
3940 struct revert_file_args rfa;
3942 err = lock_worktree(worktree, LOCK_EX);
3943 if (err)
3944 return err;
3946 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3947 if (err)
3948 goto done;
3950 rfa.worktree = worktree;
3951 rfa.fileindex = fileindex;
3952 rfa.progress_cb = progress_cb;
3953 rfa.progress_arg = progress_arg;
3954 rfa.patch_cb = patch_cb;
3955 rfa.patch_arg = patch_arg;
3956 rfa.repo = repo;
3957 TAILQ_FOREACH(pe, paths, entry) {
3958 err = worktree_status(worktree, pe->path, fileindex, repo,
3959 revert_file, &rfa, NULL, NULL, 0, 0);
3960 if (err)
3961 break;
3963 sync_err = sync_fileindex(fileindex, fileindex_path);
3964 if (sync_err && err == NULL)
3965 err = sync_err;
3966 done:
3967 free(fileindex_path);
3968 if (fileindex)
3969 got_fileindex_free(fileindex);
3970 unlockerr = lock_worktree(worktree, LOCK_SH);
3971 if (unlockerr && err == NULL)
3972 err = unlockerr;
3973 return err;
3976 static void
3977 free_commitable(struct got_commitable *ct)
3979 free(ct->path);
3980 free(ct->in_repo_path);
3981 free(ct->ondisk_path);
3982 free(ct->blob_id);
3983 free(ct->base_blob_id);
3984 free(ct->staged_blob_id);
3985 free(ct->base_commit_id);
3986 free(ct);
3989 struct collect_commitables_arg {
3990 struct got_pathlist_head *commitable_paths;
3991 struct got_repository *repo;
3992 struct got_worktree *worktree;
3993 int have_staged_files;
3996 static const struct got_error *
3997 collect_commitables(void *arg, unsigned char status,
3998 unsigned char staged_status, const char *relpath,
3999 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4000 struct got_object_id *commit_id, int dirfd, const char *de_name)
4002 struct collect_commitables_arg *a = arg;
4003 const struct got_error *err = NULL;
4004 struct got_commitable *ct = NULL;
4005 struct got_pathlist_entry *new = NULL;
4006 char *parent_path = NULL, *path = NULL;
4007 struct stat sb;
4009 if (a->have_staged_files) {
4010 if (staged_status != GOT_STATUS_MODIFY &&
4011 staged_status != GOT_STATUS_ADD &&
4012 staged_status != GOT_STATUS_DELETE)
4013 return NULL;
4014 } else {
4015 if (status == GOT_STATUS_CONFLICT)
4016 return got_error(GOT_ERR_COMMIT_CONFLICT);
4018 if (status != GOT_STATUS_MODIFY &&
4019 status != GOT_STATUS_MODE_CHANGE &&
4020 status != GOT_STATUS_ADD &&
4021 status != GOT_STATUS_DELETE)
4022 return NULL;
4025 if (asprintf(&path, "/%s", relpath) == -1) {
4026 err = got_error_from_errno("asprintf");
4027 goto done;
4029 if (strcmp(path, "/") == 0) {
4030 parent_path = strdup("");
4031 if (parent_path == NULL)
4032 return got_error_from_errno("strdup");
4033 } else {
4034 err = got_path_dirname(&parent_path, path);
4035 if (err)
4036 return err;
4039 ct = calloc(1, sizeof(*ct));
4040 if (ct == NULL) {
4041 err = got_error_from_errno("calloc");
4042 goto done;
4045 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4046 relpath) == -1) {
4047 err = got_error_from_errno("asprintf");
4048 goto done;
4050 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4051 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4052 } else {
4053 if (dirfd != -1) {
4054 if (fstatat(dirfd, de_name, &sb,
4055 AT_SYMLINK_NOFOLLOW) == -1) {
4056 err = got_error_from_errno2("fstatat",
4057 ct->ondisk_path);
4058 goto done;
4060 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4061 err = got_error_from_errno2("lstat", ct->ondisk_path);
4062 goto done;
4064 ct->mode = sb.st_mode;
4067 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4068 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4069 relpath) == -1) {
4070 err = got_error_from_errno("asprintf");
4071 goto done;
4074 ct->status = status;
4075 ct->staged_status = staged_status;
4076 ct->blob_id = NULL; /* will be filled in when blob gets created */
4077 if (ct->status != GOT_STATUS_ADD &&
4078 ct->staged_status != GOT_STATUS_ADD) {
4079 ct->base_blob_id = got_object_id_dup(blob_id);
4080 if (ct->base_blob_id == NULL) {
4081 err = got_error_from_errno("got_object_id_dup");
4082 goto done;
4084 ct->base_commit_id = got_object_id_dup(commit_id);
4085 if (ct->base_commit_id == NULL) {
4086 err = got_error_from_errno("got_object_id_dup");
4087 goto done;
4090 if (ct->staged_status == GOT_STATUS_ADD ||
4091 ct->staged_status == GOT_STATUS_MODIFY) {
4092 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4093 if (ct->staged_blob_id == NULL) {
4094 err = got_error_from_errno("got_object_id_dup");
4095 goto done;
4098 ct->path = strdup(path);
4099 if (ct->path == NULL) {
4100 err = got_error_from_errno("strdup");
4101 goto done;
4103 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4104 done:
4105 if (ct && (err || new == NULL))
4106 free_commitable(ct);
4107 free(parent_path);
4108 free(path);
4109 return err;
4112 static const struct got_error *write_tree(struct got_object_id **, int *,
4113 struct got_tree_object *, const char *, struct got_pathlist_head *,
4114 got_worktree_status_cb status_cb, void *status_arg,
4115 struct got_repository *);
4117 static const struct got_error *
4118 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4119 struct got_tree_entry *te, const char *parent_path,
4120 struct got_pathlist_head *commitable_paths,
4121 got_worktree_status_cb status_cb, void *status_arg,
4122 struct got_repository *repo)
4124 const struct got_error *err = NULL;
4125 struct got_tree_object *subtree;
4126 char *subpath;
4128 if (asprintf(&subpath, "%s%s%s", parent_path,
4129 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4130 return got_error_from_errno("asprintf");
4132 err = got_object_open_as_tree(&subtree, repo, &te->id);
4133 if (err)
4134 return err;
4136 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4137 commitable_paths, status_cb, status_arg, repo);
4138 got_object_tree_close(subtree);
4139 free(subpath);
4140 return err;
4143 static const struct got_error *
4144 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4146 const struct got_error *err = NULL;
4147 char *ct_parent_path = NULL;
4149 *match = 0;
4151 if (strchr(ct->in_repo_path, '/') == NULL) {
4152 *match = got_path_is_root_dir(path);
4153 return NULL;
4156 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4157 if (err)
4158 return err;
4159 *match = (strcmp(path, ct_parent_path) == 0);
4160 free(ct_parent_path);
4161 return err;
4164 static mode_t
4165 get_ct_file_mode(struct got_commitable *ct)
4167 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4170 static const struct got_error *
4171 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4172 struct got_tree_entry *te, struct got_commitable *ct)
4174 const struct got_error *err = NULL;
4176 *new_te = NULL;
4178 err = got_object_tree_entry_dup(new_te, te);
4179 if (err)
4180 goto done;
4182 (*new_te)->mode = get_ct_file_mode(ct);
4184 if (ct->staged_status == GOT_STATUS_MODIFY)
4185 memcpy(&(*new_te)->id, ct->staged_blob_id,
4186 sizeof((*new_te)->id));
4187 else
4188 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4189 done:
4190 if (err && *new_te) {
4191 free(*new_te);
4192 *new_te = NULL;
4194 return err;
4197 static const struct got_error *
4198 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4199 struct got_commitable *ct)
4201 const struct got_error *err = NULL;
4202 char *ct_name;
4204 *new_te = NULL;
4206 *new_te = calloc(1, sizeof(**new_te));
4207 if (*new_te == NULL)
4208 return got_error_from_errno("calloc");
4210 ct_name = basename(ct->path);
4211 if (ct_name == NULL) {
4212 err = got_error_from_errno2("basename", ct->path);
4213 goto done;
4215 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4216 sizeof((*new_te)->name)) {
4217 err = got_error(GOT_ERR_NO_SPACE);
4218 goto done;
4221 (*new_te)->mode = get_ct_file_mode(ct);
4223 if (ct->staged_status == GOT_STATUS_ADD)
4224 memcpy(&(*new_te)->id, ct->staged_blob_id,
4225 sizeof((*new_te)->id));
4226 else
4227 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4228 done:
4229 if (err && *new_te) {
4230 free(*new_te);
4231 *new_te = NULL;
4233 return err;
4236 static const struct got_error *
4237 insert_tree_entry(struct got_tree_entry *new_te,
4238 struct got_pathlist_head *paths)
4240 const struct got_error *err = NULL;
4241 struct got_pathlist_entry *new_pe;
4243 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4244 if (err)
4245 return err;
4246 if (new_pe == NULL)
4247 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4248 return NULL;
4251 static const struct got_error *
4252 report_ct_status(struct got_commitable *ct,
4253 got_worktree_status_cb status_cb, void *status_arg)
4255 const char *ct_path = ct->path;
4256 unsigned char status;
4258 while (ct_path[0] == '/')
4259 ct_path++;
4261 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4262 status = ct->staged_status;
4263 else
4264 status = ct->status;
4266 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4267 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4270 static const struct got_error *
4271 match_modified_subtree(int *modified, struct got_tree_entry *te,
4272 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4274 const struct got_error *err = NULL;
4275 struct got_pathlist_entry *pe;
4276 char *te_path;
4278 *modified = 0;
4280 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4281 got_path_is_root_dir(base_tree_path) ? "" : "/",
4282 te->name) == -1)
4283 return got_error_from_errno("asprintf");
4285 TAILQ_FOREACH(pe, commitable_paths, entry) {
4286 struct got_commitable *ct = pe->data;
4287 *modified = got_path_is_child(ct->in_repo_path, te_path,
4288 strlen(te_path));
4289 if (*modified)
4290 break;
4293 free(te_path);
4294 return err;
4297 static const struct got_error *
4298 match_deleted_or_modified_ct(struct got_commitable **ctp,
4299 struct got_tree_entry *te, const char *base_tree_path,
4300 struct got_pathlist_head *commitable_paths)
4302 const struct got_error *err = NULL;
4303 struct got_pathlist_entry *pe;
4305 *ctp = NULL;
4307 TAILQ_FOREACH(pe, commitable_paths, entry) {
4308 struct got_commitable *ct = pe->data;
4309 char *ct_name = NULL;
4310 int path_matches;
4312 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4313 if (ct->status != GOT_STATUS_MODIFY &&
4314 ct->status != GOT_STATUS_MODE_CHANGE &&
4315 ct->status != GOT_STATUS_DELETE)
4316 continue;
4317 } else {
4318 if (ct->staged_status != GOT_STATUS_MODIFY &&
4319 ct->staged_status != GOT_STATUS_DELETE)
4320 continue;
4323 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4324 continue;
4326 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4327 if (err)
4328 return err;
4329 if (!path_matches)
4330 continue;
4332 ct_name = basename(pe->path);
4333 if (ct_name == NULL)
4334 return got_error_from_errno2("basename", pe->path);
4336 if (strcmp(te->name, ct_name) != 0)
4337 continue;
4339 *ctp = ct;
4340 break;
4343 return err;
4346 static const struct got_error *
4347 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4348 const char *child_path, const char *path_base_tree,
4349 struct got_pathlist_head *commitable_paths,
4350 got_worktree_status_cb status_cb, void *status_arg,
4351 struct got_repository *repo)
4353 const struct got_error *err = NULL;
4354 struct got_tree_entry *new_te;
4355 char *subtree_path;
4356 struct got_object_id *id = NULL;
4357 int nentries;
4359 *new_tep = NULL;
4361 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4362 got_path_is_root_dir(path_base_tree) ? "" : "/",
4363 child_path) == -1)
4364 return got_error_from_errno("asprintf");
4366 new_te = calloc(1, sizeof(*new_te));
4367 if (new_te == NULL)
4368 return got_error_from_errno("calloc");
4369 new_te->mode = S_IFDIR;
4371 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4372 sizeof(new_te->name)) {
4373 err = got_error(GOT_ERR_NO_SPACE);
4374 goto done;
4376 err = write_tree(&id, &nentries, NULL, subtree_path,
4377 commitable_paths, status_cb, status_arg, repo);
4378 if (err) {
4379 free(new_te);
4380 goto done;
4382 memcpy(&new_te->id, id, sizeof(new_te->id));
4383 done:
4384 free(id);
4385 free(subtree_path);
4386 if (err == NULL)
4387 *new_tep = new_te;
4388 return err;
4391 static const struct got_error *
4392 write_tree(struct got_object_id **new_tree_id, int *nentries,
4393 struct got_tree_object *base_tree, const char *path_base_tree,
4394 struct got_pathlist_head *commitable_paths,
4395 got_worktree_status_cb status_cb, void *status_arg,
4396 struct got_repository *repo)
4398 const struct got_error *err = NULL;
4399 struct got_pathlist_head paths;
4400 struct got_tree_entry *te, *new_te = NULL;
4401 struct got_pathlist_entry *pe;
4403 TAILQ_INIT(&paths);
4404 *nentries = 0;
4406 /* Insert, and recurse into, newly added entries first. */
4407 TAILQ_FOREACH(pe, commitable_paths, entry) {
4408 struct got_commitable *ct = pe->data;
4409 char *child_path = NULL, *slash;
4411 if ((ct->status != GOT_STATUS_ADD &&
4412 ct->staged_status != GOT_STATUS_ADD) ||
4413 (ct->flags & GOT_COMMITABLE_ADDED))
4414 continue;
4416 if (!got_path_is_child(pe->path, path_base_tree,
4417 strlen(path_base_tree)))
4418 continue;
4420 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4421 pe->path);
4422 if (err)
4423 goto done;
4425 slash = strchr(child_path, '/');
4426 if (slash == NULL) {
4427 err = alloc_added_blob_tree_entry(&new_te, ct);
4428 if (err)
4429 goto done;
4430 err = report_ct_status(ct, status_cb, status_arg);
4431 if (err)
4432 goto done;
4433 ct->flags |= GOT_COMMITABLE_ADDED;
4434 err = insert_tree_entry(new_te, &paths);
4435 if (err)
4436 goto done;
4437 (*nentries)++;
4438 } else {
4439 *slash = '\0'; /* trim trailing path components */
4440 if (base_tree == NULL ||
4441 got_object_tree_find_entry(base_tree, child_path)
4442 == NULL) {
4443 err = make_subtree_for_added_blob(&new_te,
4444 child_path, path_base_tree,
4445 commitable_paths, status_cb, status_arg,
4446 repo);
4447 if (err)
4448 goto done;
4449 err = insert_tree_entry(new_te, &paths);
4450 if (err)
4451 goto done;
4452 (*nentries)++;
4457 if (base_tree) {
4458 int i, nbase_entries;
4459 /* Handle modified and deleted entries. */
4460 nbase_entries = got_object_tree_get_nentries(base_tree);
4461 for (i = 0; i < nbase_entries; i++) {
4462 struct got_commitable *ct = NULL;
4464 te = got_object_tree_get_entry(base_tree, i);
4465 if (got_object_tree_entry_is_submodule(te)) {
4466 /* Entry is a submodule; just copy it. */
4467 err = got_object_tree_entry_dup(&new_te, te);
4468 if (err)
4469 goto done;
4470 err = insert_tree_entry(new_te, &paths);
4471 if (err)
4472 goto done;
4473 (*nentries)++;
4474 continue;
4477 if (S_ISDIR(te->mode)) {
4478 int modified;
4479 err = got_object_tree_entry_dup(&new_te, te);
4480 if (err)
4481 goto done;
4482 err = match_modified_subtree(&modified, te,
4483 path_base_tree, commitable_paths);
4484 if (err)
4485 goto done;
4486 /* Avoid recursion into unmodified subtrees. */
4487 if (modified) {
4488 struct got_object_id *new_id;
4489 int nsubentries;
4490 err = write_subtree(&new_id,
4491 &nsubentries, te,
4492 path_base_tree, commitable_paths,
4493 status_cb, status_arg, repo);
4494 if (err)
4495 goto done;
4496 if (nsubentries == 0) {
4497 /* All entries were deleted. */
4498 free(new_id);
4499 continue;
4501 memcpy(&new_te->id, new_id,
4502 sizeof(new_te->id));
4503 free(new_id);
4505 err = insert_tree_entry(new_te, &paths);
4506 if (err)
4507 goto done;
4508 (*nentries)++;
4509 continue;
4512 err = match_deleted_or_modified_ct(&ct, te,
4513 path_base_tree, commitable_paths);
4514 if (err)
4515 goto done;
4516 if (ct) {
4517 /* NB: Deleted entries get dropped here. */
4518 if (ct->status == GOT_STATUS_MODIFY ||
4519 ct->status == GOT_STATUS_MODE_CHANGE ||
4520 ct->staged_status == GOT_STATUS_MODIFY) {
4521 err = alloc_modified_blob_tree_entry(
4522 &new_te, te, ct);
4523 if (err)
4524 goto done;
4525 err = insert_tree_entry(new_te, &paths);
4526 if (err)
4527 goto done;
4528 (*nentries)++;
4530 err = report_ct_status(ct, status_cb,
4531 status_arg);
4532 if (err)
4533 goto done;
4534 } else {
4535 /* Entry is unchanged; just copy it. */
4536 err = got_object_tree_entry_dup(&new_te, te);
4537 if (err)
4538 goto done;
4539 err = insert_tree_entry(new_te, &paths);
4540 if (err)
4541 goto done;
4542 (*nentries)++;
4547 /* Write new list of entries; deleted entries have been dropped. */
4548 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4549 done:
4550 got_pathlist_free(&paths);
4551 return err;
4554 static const struct got_error *
4555 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4556 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4557 int have_staged_files)
4559 const struct got_error *err = NULL;
4560 struct got_pathlist_entry *pe;
4562 TAILQ_FOREACH(pe, commitable_paths, entry) {
4563 struct got_fileindex_entry *ie;
4564 struct got_commitable *ct = pe->data;
4566 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4567 if (ie) {
4568 if (ct->status == GOT_STATUS_DELETE ||
4569 ct->staged_status == GOT_STATUS_DELETE) {
4570 got_fileindex_entry_remove(fileindex, ie);
4571 } else if (ct->staged_status == GOT_STATUS_ADD ||
4572 ct->staged_status == GOT_STATUS_MODIFY) {
4573 got_fileindex_entry_stage_set(ie,
4574 GOT_FILEIDX_STAGE_NONE);
4575 err = got_fileindex_entry_update(ie,
4576 ct->ondisk_path, ct->staged_blob_id->sha1,
4577 new_base_commit_id->sha1,
4578 !have_staged_files);
4579 } else
4580 err = got_fileindex_entry_update(ie,
4581 ct->ondisk_path, ct->blob_id->sha1,
4582 new_base_commit_id->sha1,
4583 !have_staged_files);
4584 } else {
4585 err = got_fileindex_entry_alloc(&ie, pe->path);
4586 if (err)
4587 break;
4588 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4589 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4590 if (err) {
4591 got_fileindex_entry_free(ie);
4592 break;
4594 err = got_fileindex_entry_add(fileindex, ie);
4595 if (err) {
4596 got_fileindex_entry_free(ie);
4597 break;
4601 return err;
4605 static const struct got_error *
4606 check_out_of_date(const char *in_repo_path, unsigned char status,
4607 unsigned char staged_status, struct got_object_id *base_blob_id,
4608 struct got_object_id *base_commit_id,
4609 struct got_object_id *head_commit_id, struct got_repository *repo,
4610 int ood_errcode)
4612 const struct got_error *err = NULL;
4613 struct got_object_id *id = NULL;
4615 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4616 /* Trivial case: base commit == head commit */
4617 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4618 return NULL;
4620 * Ensure file content which local changes were based
4621 * on matches file content in the branch head.
4623 err = got_object_id_by_path(&id, repo, head_commit_id,
4624 in_repo_path);
4625 if (err) {
4626 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4627 err = got_error(ood_errcode);
4628 goto done;
4629 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4630 err = got_error(ood_errcode);
4631 } else {
4632 /* Require that added files don't exist in the branch head. */
4633 err = got_object_id_by_path(&id, repo, head_commit_id,
4634 in_repo_path);
4635 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4636 goto done;
4637 err = id ? got_error(ood_errcode) : NULL;
4639 done:
4640 free(id);
4641 return err;
4644 const struct got_error *
4645 commit_worktree(struct got_object_id **new_commit_id,
4646 struct got_pathlist_head *commitable_paths,
4647 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4648 const char *author, const char *committer,
4649 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4650 got_worktree_status_cb status_cb, void *status_arg,
4651 struct got_repository *repo)
4653 const struct got_error *err = NULL, *unlockerr = NULL;
4654 struct got_pathlist_entry *pe;
4655 const char *head_ref_name = NULL;
4656 struct got_commit_object *head_commit = NULL;
4657 struct got_reference *head_ref2 = NULL;
4658 struct got_object_id *head_commit_id2 = NULL;
4659 struct got_tree_object *head_tree = NULL;
4660 struct got_object_id *new_tree_id = NULL;
4661 int nentries;
4662 struct got_object_id_queue parent_ids;
4663 struct got_object_qid *pid = NULL;
4664 char *logmsg = NULL;
4666 *new_commit_id = NULL;
4668 SIMPLEQ_INIT(&parent_ids);
4670 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4671 if (err)
4672 goto done;
4674 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4675 if (err)
4676 goto done;
4678 if (commit_msg_cb != NULL) {
4679 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4680 if (err)
4681 goto done;
4684 if (logmsg == NULL || strlen(logmsg) == 0) {
4685 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4686 goto done;
4689 /* Create blobs from added and modified files and record their IDs. */
4690 TAILQ_FOREACH(pe, commitable_paths, entry) {
4691 struct got_commitable *ct = pe->data;
4692 char *ondisk_path;
4694 /* Blobs for staged files already exist. */
4695 if (ct->staged_status == GOT_STATUS_ADD ||
4696 ct->staged_status == GOT_STATUS_MODIFY)
4697 continue;
4699 if (ct->status != GOT_STATUS_ADD &&
4700 ct->status != GOT_STATUS_MODIFY &&
4701 ct->status != GOT_STATUS_MODE_CHANGE)
4702 continue;
4704 if (asprintf(&ondisk_path, "%s/%s",
4705 worktree->root_path, pe->path) == -1) {
4706 err = got_error_from_errno("asprintf");
4707 goto done;
4709 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4710 free(ondisk_path);
4711 if (err)
4712 goto done;
4715 /* Recursively write new tree objects. */
4716 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4717 commitable_paths, status_cb, status_arg, repo);
4718 if (err)
4719 goto done;
4721 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4722 if (err)
4723 goto done;
4724 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4725 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4726 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4727 got_object_qid_free(pid);
4728 if (logmsg != NULL)
4729 free(logmsg);
4730 if (err)
4731 goto done;
4733 /* Check if a concurrent commit to our branch has occurred. */
4734 head_ref_name = got_worktree_get_head_ref_name(worktree);
4735 if (head_ref_name == NULL) {
4736 err = got_error_from_errno("got_worktree_get_head_ref_name");
4737 goto done;
4739 /* Lock the reference here to prevent concurrent modification. */
4740 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4741 if (err)
4742 goto done;
4743 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4744 if (err)
4745 goto done;
4746 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4747 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4748 goto done;
4750 /* Update branch head in repository. */
4751 err = got_ref_change_ref(head_ref2, *new_commit_id);
4752 if (err)
4753 goto done;
4754 err = got_ref_write(head_ref2, repo);
4755 if (err)
4756 goto done;
4758 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4759 if (err)
4760 goto done;
4762 err = ref_base_commit(worktree, repo);
4763 if (err)
4764 goto done;
4765 done:
4766 if (head_tree)
4767 got_object_tree_close(head_tree);
4768 if (head_commit)
4769 got_object_commit_close(head_commit);
4770 free(head_commit_id2);
4771 if (head_ref2) {
4772 unlockerr = got_ref_unlock(head_ref2);
4773 if (unlockerr && err == NULL)
4774 err = unlockerr;
4775 got_ref_close(head_ref2);
4777 return err;
4780 static const struct got_error *
4781 check_path_is_commitable(const char *path,
4782 struct got_pathlist_head *commitable_paths)
4784 struct got_pathlist_entry *cpe = NULL;
4785 size_t path_len = strlen(path);
4787 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4788 struct got_commitable *ct = cpe->data;
4789 const char *ct_path = ct->path;
4791 while (ct_path[0] == '/')
4792 ct_path++;
4794 if (strcmp(path, ct_path) == 0 ||
4795 got_path_is_child(ct_path, path, path_len))
4796 break;
4799 if (cpe == NULL)
4800 return got_error_path(path, GOT_ERR_BAD_PATH);
4802 return NULL;
4805 static const struct got_error *
4806 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4808 int *have_staged_files = arg;
4810 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4811 *have_staged_files = 1;
4812 return got_error(GOT_ERR_CANCELLED);
4815 return NULL;
4818 static const struct got_error *
4819 check_non_staged_files(struct got_fileindex *fileindex,
4820 struct got_pathlist_head *paths)
4822 struct got_pathlist_entry *pe;
4823 struct got_fileindex_entry *ie;
4825 TAILQ_FOREACH(pe, paths, entry) {
4826 if (pe->path[0] == '\0')
4827 continue;
4828 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4829 if (ie == NULL)
4830 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4831 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4832 return got_error_path(pe->path,
4833 GOT_ERR_FILE_NOT_STAGED);
4836 return NULL;
4839 const struct got_error *
4840 got_worktree_commit(struct got_object_id **new_commit_id,
4841 struct got_worktree *worktree, struct got_pathlist_head *paths,
4842 const char *author, const char *committer,
4843 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4844 got_worktree_status_cb status_cb, void *status_arg,
4845 struct got_repository *repo)
4847 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4848 struct got_fileindex *fileindex = NULL;
4849 char *fileindex_path = NULL;
4850 struct got_pathlist_head commitable_paths;
4851 struct collect_commitables_arg cc_arg;
4852 struct got_pathlist_entry *pe;
4853 struct got_reference *head_ref = NULL;
4854 struct got_object_id *head_commit_id = NULL;
4855 int have_staged_files = 0;
4857 *new_commit_id = NULL;
4859 TAILQ_INIT(&commitable_paths);
4861 err = lock_worktree(worktree, LOCK_EX);
4862 if (err)
4863 goto done;
4865 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4866 if (err)
4867 goto done;
4869 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4870 if (err)
4871 goto done;
4873 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4874 if (err)
4875 goto done;
4877 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4878 &have_staged_files);
4879 if (err && err->code != GOT_ERR_CANCELLED)
4880 goto done;
4881 if (have_staged_files) {
4882 err = check_non_staged_files(fileindex, paths);
4883 if (err)
4884 goto done;
4887 cc_arg.commitable_paths = &commitable_paths;
4888 cc_arg.worktree = worktree;
4889 cc_arg.repo = repo;
4890 cc_arg.have_staged_files = have_staged_files;
4891 TAILQ_FOREACH(pe, paths, entry) {
4892 err = worktree_status(worktree, pe->path, fileindex, repo,
4893 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4894 if (err)
4895 goto done;
4898 if (TAILQ_EMPTY(&commitable_paths)) {
4899 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4900 goto done;
4903 TAILQ_FOREACH(pe, paths, entry) {
4904 err = check_path_is_commitable(pe->path, &commitable_paths);
4905 if (err)
4906 goto done;
4909 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4910 struct got_commitable *ct = pe->data;
4911 const char *ct_path = ct->in_repo_path;
4913 while (ct_path[0] == '/')
4914 ct_path++;
4915 err = check_out_of_date(ct_path, ct->status,
4916 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4917 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4918 if (err)
4919 goto done;
4923 err = commit_worktree(new_commit_id, &commitable_paths,
4924 head_commit_id, worktree, author, committer,
4925 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4926 if (err)
4927 goto done;
4929 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4930 fileindex, have_staged_files);
4931 sync_err = sync_fileindex(fileindex, fileindex_path);
4932 if (sync_err && err == NULL)
4933 err = sync_err;
4934 done:
4935 if (fileindex)
4936 got_fileindex_free(fileindex);
4937 free(fileindex_path);
4938 unlockerr = lock_worktree(worktree, LOCK_SH);
4939 if (unlockerr && err == NULL)
4940 err = unlockerr;
4941 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4942 struct got_commitable *ct = pe->data;
4943 free_commitable(ct);
4945 got_pathlist_free(&commitable_paths);
4946 return err;
4949 const char *
4950 got_commitable_get_path(struct got_commitable *ct)
4952 return ct->path;
4955 unsigned int
4956 got_commitable_get_status(struct got_commitable *ct)
4958 return ct->status;
4961 struct check_rebase_ok_arg {
4962 struct got_worktree *worktree;
4963 struct got_repository *repo;
4966 static const struct got_error *
4967 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4969 const struct got_error *err = NULL;
4970 struct check_rebase_ok_arg *a = arg;
4971 unsigned char status;
4972 struct stat sb;
4973 char *ondisk_path;
4975 /* Reject rebase of a work tree with mixed base commits. */
4976 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4977 SHA1_DIGEST_LENGTH))
4978 return got_error(GOT_ERR_MIXED_COMMITS);
4980 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4981 == -1)
4982 return got_error_from_errno("asprintf");
4984 /* Reject rebase of a work tree with modified or staged files. */
4985 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4986 free(ondisk_path);
4987 if (err)
4988 return err;
4990 if (status != GOT_STATUS_NO_CHANGE)
4991 return got_error(GOT_ERR_MODIFIED);
4992 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4993 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4995 return NULL;
4998 const struct got_error *
4999 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5000 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5001 struct got_worktree *worktree, struct got_reference *branch,
5002 struct got_repository *repo)
5004 const struct got_error *err = NULL;
5005 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5006 char *branch_ref_name = NULL;
5007 char *fileindex_path = NULL;
5008 struct check_rebase_ok_arg ok_arg;
5009 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5010 struct got_object_id *wt_branch_tip = NULL;
5012 *new_base_branch_ref = NULL;
5013 *tmp_branch = NULL;
5014 *fileindex = NULL;
5016 err = lock_worktree(worktree, LOCK_EX);
5017 if (err)
5018 return err;
5020 err = open_fileindex(fileindex, &fileindex_path, worktree);
5021 if (err)
5022 goto done;
5024 ok_arg.worktree = worktree;
5025 ok_arg.repo = repo;
5026 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5027 &ok_arg);
5028 if (err)
5029 goto done;
5031 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5032 if (err)
5033 goto done;
5035 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5036 if (err)
5037 goto done;
5039 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5040 if (err)
5041 goto done;
5043 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5044 0);
5045 if (err)
5046 goto done;
5048 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5049 if (err)
5050 goto done;
5051 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5052 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5053 goto done;
5056 err = got_ref_alloc_symref(new_base_branch_ref,
5057 new_base_branch_ref_name, wt_branch);
5058 if (err)
5059 goto done;
5060 err = got_ref_write(*new_base_branch_ref, repo);
5061 if (err)
5062 goto done;
5064 /* TODO Lock original branch's ref while rebasing? */
5066 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5067 if (err)
5068 goto done;
5070 err = got_ref_write(branch_ref, repo);
5071 if (err)
5072 goto done;
5074 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5075 worktree->base_commit_id);
5076 if (err)
5077 goto done;
5078 err = got_ref_write(*tmp_branch, repo);
5079 if (err)
5080 goto done;
5082 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5083 if (err)
5084 goto done;
5085 done:
5086 free(fileindex_path);
5087 free(tmp_branch_name);
5088 free(new_base_branch_ref_name);
5089 free(branch_ref_name);
5090 if (branch_ref)
5091 got_ref_close(branch_ref);
5092 if (wt_branch)
5093 got_ref_close(wt_branch);
5094 free(wt_branch_tip);
5095 if (err) {
5096 if (*new_base_branch_ref) {
5097 got_ref_close(*new_base_branch_ref);
5098 *new_base_branch_ref = NULL;
5100 if (*tmp_branch) {
5101 got_ref_close(*tmp_branch);
5102 *tmp_branch = NULL;
5104 if (*fileindex) {
5105 got_fileindex_free(*fileindex);
5106 *fileindex = NULL;
5108 lock_worktree(worktree, LOCK_SH);
5110 return err;
5113 const struct got_error *
5114 got_worktree_rebase_continue(struct got_object_id **commit_id,
5115 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5116 struct got_reference **branch, struct got_fileindex **fileindex,
5117 struct got_worktree *worktree, struct got_repository *repo)
5119 const struct got_error *err;
5120 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5121 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5122 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5123 char *fileindex_path = NULL;
5124 int have_staged_files = 0;
5126 *commit_id = NULL;
5127 *new_base_branch = NULL;
5128 *tmp_branch = NULL;
5129 *branch = NULL;
5130 *fileindex = NULL;
5132 err = lock_worktree(worktree, LOCK_EX);
5133 if (err)
5134 return err;
5136 err = open_fileindex(fileindex, &fileindex_path, worktree);
5137 if (err)
5138 goto done;
5140 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5141 &have_staged_files);
5142 if (err && err->code != GOT_ERR_CANCELLED)
5143 goto done;
5144 if (have_staged_files) {
5145 err = got_error(GOT_ERR_STAGED_PATHS);
5146 goto done;
5149 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5150 if (err)
5151 goto done;
5153 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5154 if (err)
5155 goto done;
5157 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5158 if (err)
5159 goto done;
5161 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5162 if (err)
5163 goto done;
5165 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5166 if (err)
5167 goto done;
5169 err = got_ref_open(branch, repo,
5170 got_ref_get_symref_target(branch_ref), 0);
5171 if (err)
5172 goto done;
5174 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5175 if (err)
5176 goto done;
5178 err = got_ref_resolve(commit_id, repo, commit_ref);
5179 if (err)
5180 goto done;
5182 err = got_ref_open(new_base_branch, repo,
5183 new_base_branch_ref_name, 0);
5184 if (err)
5185 goto done;
5187 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5188 if (err)
5189 goto done;
5190 done:
5191 free(commit_ref_name);
5192 free(branch_ref_name);
5193 free(fileindex_path);
5194 if (commit_ref)
5195 got_ref_close(commit_ref);
5196 if (branch_ref)
5197 got_ref_close(branch_ref);
5198 if (err) {
5199 free(*commit_id);
5200 *commit_id = NULL;
5201 if (*tmp_branch) {
5202 got_ref_close(*tmp_branch);
5203 *tmp_branch = NULL;
5205 if (*new_base_branch) {
5206 got_ref_close(*new_base_branch);
5207 *new_base_branch = NULL;
5209 if (*branch) {
5210 got_ref_close(*branch);
5211 *branch = NULL;
5213 if (*fileindex) {
5214 got_fileindex_free(*fileindex);
5215 *fileindex = NULL;
5217 lock_worktree(worktree, LOCK_SH);
5219 return err;
5222 const struct got_error *
5223 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5225 const struct got_error *err;
5226 char *tmp_branch_name = NULL;
5228 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5229 if (err)
5230 return err;
5232 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5233 free(tmp_branch_name);
5234 return NULL;
5237 static const struct got_error *
5238 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5239 char **logmsg, void *arg)
5241 *logmsg = arg;
5242 return NULL;
5245 static const struct got_error *
5246 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5247 const char *path, struct got_object_id *blob_id,
5248 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5249 int dirfd, const char *de_name)
5251 return NULL;
5254 struct collect_merged_paths_arg {
5255 got_worktree_checkout_cb progress_cb;
5256 void *progress_arg;
5257 struct got_pathlist_head *merged_paths;
5260 static const struct got_error *
5261 collect_merged_paths(void *arg, unsigned char status, const char *path)
5263 const struct got_error *err;
5264 struct collect_merged_paths_arg *a = arg;
5265 char *p;
5266 struct got_pathlist_entry *new;
5268 err = (*a->progress_cb)(a->progress_arg, status, path);
5269 if (err)
5270 return err;
5272 if (status != GOT_STATUS_MERGE &&
5273 status != GOT_STATUS_ADD &&
5274 status != GOT_STATUS_DELETE &&
5275 status != GOT_STATUS_CONFLICT)
5276 return NULL;
5278 p = strdup(path);
5279 if (p == NULL)
5280 return got_error_from_errno("strdup");
5282 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5283 if (err || new == NULL)
5284 free(p);
5285 return err;
5288 void
5289 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5291 struct got_pathlist_entry *pe;
5293 TAILQ_FOREACH(pe, merged_paths, entry)
5294 free((char *)pe->path);
5296 got_pathlist_free(merged_paths);
5299 static const struct got_error *
5300 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5301 int is_rebase, struct got_repository *repo)
5303 const struct got_error *err;
5304 struct got_reference *commit_ref = NULL;
5306 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5307 if (err) {
5308 if (err->code != GOT_ERR_NOT_REF)
5309 goto done;
5310 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5311 if (err)
5312 goto done;
5313 err = got_ref_write(commit_ref, repo);
5314 if (err)
5315 goto done;
5316 } else if (is_rebase) {
5317 struct got_object_id *stored_id;
5318 int cmp;
5320 err = got_ref_resolve(&stored_id, repo, commit_ref);
5321 if (err)
5322 goto done;
5323 cmp = got_object_id_cmp(commit_id, stored_id);
5324 free(stored_id);
5325 if (cmp != 0) {
5326 err = got_error(GOT_ERR_REBASE_COMMITID);
5327 goto done;
5330 done:
5331 if (commit_ref)
5332 got_ref_close(commit_ref);
5333 return err;
5336 static const struct got_error *
5337 rebase_merge_files(struct got_pathlist_head *merged_paths,
5338 const char *commit_ref_name, struct got_worktree *worktree,
5339 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5340 struct got_object_id *commit_id, struct got_repository *repo,
5341 got_worktree_checkout_cb progress_cb, void *progress_arg,
5342 got_cancel_cb cancel_cb, void *cancel_arg)
5344 const struct got_error *err;
5345 struct got_reference *commit_ref = NULL;
5346 struct collect_merged_paths_arg cmp_arg;
5347 char *fileindex_path;
5349 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5351 err = get_fileindex_path(&fileindex_path, worktree);
5352 if (err)
5353 return err;
5355 cmp_arg.progress_cb = progress_cb;
5356 cmp_arg.progress_arg = progress_arg;
5357 cmp_arg.merged_paths = merged_paths;
5358 err = merge_files(worktree, fileindex, fileindex_path,
5359 parent_commit_id, commit_id, repo, collect_merged_paths,
5360 &cmp_arg, cancel_cb, cancel_arg);
5361 if (commit_ref)
5362 got_ref_close(commit_ref);
5363 return err;
5366 const struct got_error *
5367 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5368 struct got_worktree *worktree, struct got_fileindex *fileindex,
5369 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5370 struct got_repository *repo,
5371 got_worktree_checkout_cb progress_cb, void *progress_arg,
5372 got_cancel_cb cancel_cb, void *cancel_arg)
5374 const struct got_error *err;
5375 char *commit_ref_name;
5377 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5378 if (err)
5379 return err;
5381 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5382 if (err)
5383 goto done;
5385 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5386 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5387 progress_arg, cancel_cb, cancel_arg);
5388 done:
5389 free(commit_ref_name);
5390 return err;
5393 const struct got_error *
5394 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5395 struct got_worktree *worktree, struct got_fileindex *fileindex,
5396 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5397 struct got_repository *repo,
5398 got_worktree_checkout_cb progress_cb, void *progress_arg,
5399 got_cancel_cb cancel_cb, void *cancel_arg)
5401 const struct got_error *err;
5402 char *commit_ref_name;
5404 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5405 if (err)
5406 return err;
5408 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5409 if (err)
5410 goto done;
5412 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5413 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5414 progress_arg, cancel_cb, cancel_arg);
5415 done:
5416 free(commit_ref_name);
5417 return err;
5420 static const struct got_error *
5421 rebase_commit(struct got_object_id **new_commit_id,
5422 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5423 struct got_worktree *worktree, struct got_fileindex *fileindex,
5424 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5425 const char *new_logmsg, struct got_repository *repo)
5427 const struct got_error *err, *sync_err;
5428 struct got_pathlist_head commitable_paths;
5429 struct collect_commitables_arg cc_arg;
5430 char *fileindex_path = NULL;
5431 struct got_reference *head_ref = NULL;
5432 struct got_object_id *head_commit_id = NULL;
5433 char *logmsg = NULL;
5435 TAILQ_INIT(&commitable_paths);
5436 *new_commit_id = NULL;
5438 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5440 err = get_fileindex_path(&fileindex_path, worktree);
5441 if (err)
5442 return err;
5444 cc_arg.commitable_paths = &commitable_paths;
5445 cc_arg.worktree = worktree;
5446 cc_arg.repo = repo;
5447 cc_arg.have_staged_files = 0;
5449 * If possible get the status of individual files directly to
5450 * avoid crawling the entire work tree once per rebased commit.
5451 * TODO: Ideally, merged_paths would contain a list of commitables
5452 * we could use so we could skip worktree_status() entirely.
5454 if (merged_paths) {
5455 struct got_pathlist_entry *pe;
5456 TAILQ_FOREACH(pe, merged_paths, entry) {
5457 err = worktree_status(worktree, pe->path, fileindex,
5458 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5459 0);
5460 if (err)
5461 goto done;
5463 } else {
5464 err = worktree_status(worktree, "", fileindex, repo,
5465 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5466 if (err)
5467 goto done;
5470 if (TAILQ_EMPTY(&commitable_paths)) {
5471 /* No-op change; commit will be elided. */
5472 err = got_ref_delete(commit_ref, repo);
5473 if (err)
5474 goto done;
5475 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5476 goto done;
5479 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5480 if (err)
5481 goto done;
5483 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5484 if (err)
5485 goto done;
5487 if (new_logmsg) {
5488 logmsg = strdup(new_logmsg);
5489 if (logmsg == NULL) {
5490 err = got_error_from_errno("strdup");
5491 goto done;
5493 } else {
5494 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5495 if (err)
5496 goto done;
5499 /* NB: commit_worktree will call free(logmsg) */
5500 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5501 worktree, got_object_commit_get_author(orig_commit),
5502 got_object_commit_get_committer(orig_commit),
5503 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5504 if (err)
5505 goto done;
5507 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5508 if (err)
5509 goto done;
5511 err = got_ref_delete(commit_ref, repo);
5512 if (err)
5513 goto done;
5515 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5516 fileindex, 0);
5517 sync_err = sync_fileindex(fileindex, fileindex_path);
5518 if (sync_err && err == NULL)
5519 err = sync_err;
5520 done:
5521 free(fileindex_path);
5522 free(head_commit_id);
5523 if (head_ref)
5524 got_ref_close(head_ref);
5525 if (err) {
5526 free(*new_commit_id);
5527 *new_commit_id = NULL;
5529 return err;
5532 const struct got_error *
5533 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5534 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5535 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5536 struct got_commit_object *orig_commit,
5537 struct got_object_id *orig_commit_id, struct got_repository *repo)
5539 const struct got_error *err;
5540 char *commit_ref_name;
5541 struct got_reference *commit_ref = NULL;
5542 struct got_object_id *commit_id = NULL;
5544 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5545 if (err)
5546 return err;
5548 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5549 if (err)
5550 goto done;
5551 err = got_ref_resolve(&commit_id, repo, commit_ref);
5552 if (err)
5553 goto done;
5554 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5555 err = got_error(GOT_ERR_REBASE_COMMITID);
5556 goto done;
5559 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5560 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5561 done:
5562 if (commit_ref)
5563 got_ref_close(commit_ref);
5564 free(commit_ref_name);
5565 free(commit_id);
5566 return err;
5569 const struct got_error *
5570 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5571 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5572 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5573 struct got_commit_object *orig_commit,
5574 struct got_object_id *orig_commit_id, const char *new_logmsg,
5575 struct got_repository *repo)
5577 const struct got_error *err;
5578 char *commit_ref_name;
5579 struct got_reference *commit_ref = NULL;
5581 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5582 if (err)
5583 return err;
5585 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5586 if (err)
5587 goto done;
5589 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5590 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5591 done:
5592 if (commit_ref)
5593 got_ref_close(commit_ref);
5594 free(commit_ref_name);
5595 return err;
5598 const struct got_error *
5599 got_worktree_rebase_postpone(struct got_worktree *worktree,
5600 struct got_fileindex *fileindex)
5602 if (fileindex)
5603 got_fileindex_free(fileindex);
5604 return lock_worktree(worktree, LOCK_SH);
5607 static const struct got_error *
5608 delete_ref(const char *name, struct got_repository *repo)
5610 const struct got_error *err;
5611 struct got_reference *ref;
5613 err = got_ref_open(&ref, repo, name, 0);
5614 if (err) {
5615 if (err->code == GOT_ERR_NOT_REF)
5616 return NULL;
5617 return err;
5620 err = got_ref_delete(ref, repo);
5621 got_ref_close(ref);
5622 return err;
5625 static const struct got_error *
5626 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5628 const struct got_error *err;
5629 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5630 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5632 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5633 if (err)
5634 goto done;
5635 err = delete_ref(tmp_branch_name, repo);
5636 if (err)
5637 goto done;
5639 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5640 if (err)
5641 goto done;
5642 err = delete_ref(new_base_branch_ref_name, repo);
5643 if (err)
5644 goto done;
5646 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5647 if (err)
5648 goto done;
5649 err = delete_ref(branch_ref_name, repo);
5650 if (err)
5651 goto done;
5653 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5654 if (err)
5655 goto done;
5656 err = delete_ref(commit_ref_name, repo);
5657 if (err)
5658 goto done;
5660 done:
5661 free(tmp_branch_name);
5662 free(new_base_branch_ref_name);
5663 free(branch_ref_name);
5664 free(commit_ref_name);
5665 return err;
5668 const struct got_error *
5669 got_worktree_rebase_complete(struct got_worktree *worktree,
5670 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5671 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5672 struct got_repository *repo)
5674 const struct got_error *err, *unlockerr;
5675 struct got_object_id *new_head_commit_id = NULL;
5677 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5678 if (err)
5679 return err;
5681 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5682 if (err)
5683 goto done;
5685 err = got_ref_write(rebased_branch, repo);
5686 if (err)
5687 goto done;
5689 err = got_worktree_set_head_ref(worktree, rebased_branch);
5690 if (err)
5691 goto done;
5693 err = delete_rebase_refs(worktree, repo);
5694 done:
5695 if (fileindex)
5696 got_fileindex_free(fileindex);
5697 free(new_head_commit_id);
5698 unlockerr = lock_worktree(worktree, LOCK_SH);
5699 if (unlockerr && err == NULL)
5700 err = unlockerr;
5701 return err;
5704 const struct got_error *
5705 got_worktree_rebase_abort(struct got_worktree *worktree,
5706 struct got_fileindex *fileindex, struct got_repository *repo,
5707 struct got_reference *new_base_branch,
5708 got_worktree_checkout_cb progress_cb, void *progress_arg)
5710 const struct got_error *err, *unlockerr, *sync_err;
5711 struct got_reference *resolved = NULL;
5712 struct got_object_id *commit_id = NULL;
5713 char *fileindex_path = NULL;
5714 struct revert_file_args rfa;
5715 struct got_object_id *tree_id = NULL;
5717 err = lock_worktree(worktree, LOCK_EX);
5718 if (err)
5719 return err;
5721 err = got_ref_open(&resolved, repo,
5722 got_ref_get_symref_target(new_base_branch), 0);
5723 if (err)
5724 goto done;
5726 err = got_worktree_set_head_ref(worktree, resolved);
5727 if (err)
5728 goto done;
5731 * XXX commits to the base branch could have happened while
5732 * we were busy rebasing; should we store the original commit ID
5733 * when rebase begins and read it back here?
5735 err = got_ref_resolve(&commit_id, repo, resolved);
5736 if (err)
5737 goto done;
5739 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5740 if (err)
5741 goto done;
5743 err = got_object_id_by_path(&tree_id, repo,
5744 worktree->base_commit_id, worktree->path_prefix);
5745 if (err)
5746 goto done;
5748 err = delete_rebase_refs(worktree, repo);
5749 if (err)
5750 goto done;
5752 err = get_fileindex_path(&fileindex_path, worktree);
5753 if (err)
5754 goto done;
5756 rfa.worktree = worktree;
5757 rfa.fileindex = fileindex;
5758 rfa.progress_cb = progress_cb;
5759 rfa.progress_arg = progress_arg;
5760 rfa.patch_cb = NULL;
5761 rfa.patch_arg = NULL;
5762 rfa.repo = repo;
5763 err = worktree_status(worktree, "", fileindex, repo,
5764 revert_file, &rfa, NULL, NULL, 0, 0);
5765 if (err)
5766 goto sync;
5768 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5769 repo, progress_cb, progress_arg, NULL, NULL);
5770 sync:
5771 sync_err = sync_fileindex(fileindex, fileindex_path);
5772 if (sync_err && err == NULL)
5773 err = sync_err;
5774 done:
5775 got_ref_close(resolved);
5776 free(tree_id);
5777 free(commit_id);
5778 if (fileindex)
5779 got_fileindex_free(fileindex);
5780 free(fileindex_path);
5782 unlockerr = lock_worktree(worktree, LOCK_SH);
5783 if (unlockerr && err == NULL)
5784 err = unlockerr;
5785 return err;
5788 const struct got_error *
5789 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5790 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5791 struct got_fileindex **fileindex, struct got_worktree *worktree,
5792 struct got_repository *repo)
5794 const struct got_error *err = NULL;
5795 char *tmp_branch_name = NULL;
5796 char *branch_ref_name = NULL;
5797 char *base_commit_ref_name = NULL;
5798 char *fileindex_path = NULL;
5799 struct check_rebase_ok_arg ok_arg;
5800 struct got_reference *wt_branch = NULL;
5801 struct got_reference *base_commit_ref = NULL;
5803 *tmp_branch = NULL;
5804 *branch_ref = NULL;
5805 *base_commit_id = NULL;
5806 *fileindex = NULL;
5808 err = lock_worktree(worktree, LOCK_EX);
5809 if (err)
5810 return err;
5812 err = open_fileindex(fileindex, &fileindex_path, worktree);
5813 if (err)
5814 goto done;
5816 ok_arg.worktree = worktree;
5817 ok_arg.repo = repo;
5818 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5819 &ok_arg);
5820 if (err)
5821 goto done;
5823 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5824 if (err)
5825 goto done;
5827 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5828 if (err)
5829 goto done;
5831 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5832 worktree);
5833 if (err)
5834 goto done;
5836 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5837 0);
5838 if (err)
5839 goto done;
5841 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5842 if (err)
5843 goto done;
5845 err = got_ref_write(*branch_ref, repo);
5846 if (err)
5847 goto done;
5849 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5850 worktree->base_commit_id);
5851 if (err)
5852 goto done;
5853 err = got_ref_write(base_commit_ref, repo);
5854 if (err)
5855 goto done;
5856 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5857 if (*base_commit_id == NULL) {
5858 err = got_error_from_errno("got_object_id_dup");
5859 goto done;
5862 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5863 worktree->base_commit_id);
5864 if (err)
5865 goto done;
5866 err = got_ref_write(*tmp_branch, repo);
5867 if (err)
5868 goto done;
5870 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5871 if (err)
5872 goto done;
5873 done:
5874 free(fileindex_path);
5875 free(tmp_branch_name);
5876 free(branch_ref_name);
5877 free(base_commit_ref_name);
5878 if (wt_branch)
5879 got_ref_close(wt_branch);
5880 if (err) {
5881 if (*branch_ref) {
5882 got_ref_close(*branch_ref);
5883 *branch_ref = NULL;
5885 if (*tmp_branch) {
5886 got_ref_close(*tmp_branch);
5887 *tmp_branch = NULL;
5889 free(*base_commit_id);
5890 if (*fileindex) {
5891 got_fileindex_free(*fileindex);
5892 *fileindex = NULL;
5894 lock_worktree(worktree, LOCK_SH);
5896 return err;
5899 const struct got_error *
5900 got_worktree_histedit_postpone(struct got_worktree *worktree,
5901 struct got_fileindex *fileindex)
5903 if (fileindex)
5904 got_fileindex_free(fileindex);
5905 return lock_worktree(worktree, LOCK_SH);
5908 const struct got_error *
5909 got_worktree_histedit_in_progress(int *in_progress,
5910 struct got_worktree *worktree)
5912 const struct got_error *err;
5913 char *tmp_branch_name = NULL;
5915 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5916 if (err)
5917 return err;
5919 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5920 free(tmp_branch_name);
5921 return NULL;
5924 const struct got_error *
5925 got_worktree_histedit_continue(struct got_object_id **commit_id,
5926 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5927 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5928 struct got_worktree *worktree, struct got_repository *repo)
5930 const struct got_error *err;
5931 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5932 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5933 struct got_reference *commit_ref = NULL;
5934 struct got_reference *base_commit_ref = NULL;
5935 char *fileindex_path = NULL;
5936 int have_staged_files = 0;
5938 *commit_id = NULL;
5939 *tmp_branch = NULL;
5940 *base_commit_id = NULL;
5941 *fileindex = NULL;
5943 err = lock_worktree(worktree, LOCK_EX);
5944 if (err)
5945 return err;
5947 err = open_fileindex(fileindex, &fileindex_path, worktree);
5948 if (err)
5949 goto done;
5951 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5952 &have_staged_files);
5953 if (err && err->code != GOT_ERR_CANCELLED)
5954 goto done;
5955 if (have_staged_files) {
5956 err = got_error(GOT_ERR_STAGED_PATHS);
5957 goto done;
5960 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5961 if (err)
5962 goto done;
5964 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5965 if (err)
5966 goto done;
5968 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5969 if (err)
5970 goto done;
5972 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5973 worktree);
5974 if (err)
5975 goto done;
5977 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5978 if (err)
5979 goto done;
5981 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5982 if (err)
5983 goto done;
5984 err = got_ref_resolve(commit_id, repo, commit_ref);
5985 if (err)
5986 goto done;
5988 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5989 if (err)
5990 goto done;
5991 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5992 if (err)
5993 goto done;
5995 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5996 if (err)
5997 goto done;
5998 done:
5999 free(commit_ref_name);
6000 free(branch_ref_name);
6001 free(fileindex_path);
6002 if (commit_ref)
6003 got_ref_close(commit_ref);
6004 if (base_commit_ref)
6005 got_ref_close(base_commit_ref);
6006 if (err) {
6007 free(*commit_id);
6008 *commit_id = NULL;
6009 free(*base_commit_id);
6010 *base_commit_id = NULL;
6011 if (*tmp_branch) {
6012 got_ref_close(*tmp_branch);
6013 *tmp_branch = NULL;
6015 if (*fileindex) {
6016 got_fileindex_free(*fileindex);
6017 *fileindex = NULL;
6019 lock_worktree(worktree, LOCK_EX);
6021 return err;
6024 static const struct got_error *
6025 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6027 const struct got_error *err;
6028 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6029 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6031 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6032 if (err)
6033 goto done;
6034 err = delete_ref(tmp_branch_name, repo);
6035 if (err)
6036 goto done;
6038 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6039 worktree);
6040 if (err)
6041 goto done;
6042 err = delete_ref(base_commit_ref_name, repo);
6043 if (err)
6044 goto done;
6046 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6047 if (err)
6048 goto done;
6049 err = delete_ref(branch_ref_name, repo);
6050 if (err)
6051 goto done;
6053 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6054 if (err)
6055 goto done;
6056 err = delete_ref(commit_ref_name, repo);
6057 if (err)
6058 goto done;
6059 done:
6060 free(tmp_branch_name);
6061 free(base_commit_ref_name);
6062 free(branch_ref_name);
6063 free(commit_ref_name);
6064 return err;
6067 const struct got_error *
6068 got_worktree_histedit_abort(struct got_worktree *worktree,
6069 struct got_fileindex *fileindex, struct got_repository *repo,
6070 struct got_reference *branch, struct got_object_id *base_commit_id,
6071 got_worktree_checkout_cb progress_cb, void *progress_arg)
6073 const struct got_error *err, *unlockerr, *sync_err;
6074 struct got_reference *resolved = NULL;
6075 char *fileindex_path = NULL;
6076 struct got_object_id *tree_id = NULL;
6077 struct revert_file_args rfa;
6079 err = lock_worktree(worktree, LOCK_EX);
6080 if (err)
6081 return err;
6083 err = got_ref_open(&resolved, repo,
6084 got_ref_get_symref_target(branch), 0);
6085 if (err)
6086 goto done;
6088 err = got_worktree_set_head_ref(worktree, resolved);
6089 if (err)
6090 goto done;
6092 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6093 if (err)
6094 goto done;
6096 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6097 worktree->path_prefix);
6098 if (err)
6099 goto done;
6101 err = delete_histedit_refs(worktree, repo);
6102 if (err)
6103 goto done;
6105 err = get_fileindex_path(&fileindex_path, worktree);
6106 if (err)
6107 goto done;
6109 rfa.worktree = worktree;
6110 rfa.fileindex = fileindex;
6111 rfa.progress_cb = progress_cb;
6112 rfa.progress_arg = progress_arg;
6113 rfa.patch_cb = NULL;
6114 rfa.patch_arg = NULL;
6115 rfa.repo = repo;
6116 err = worktree_status(worktree, "", fileindex, repo,
6117 revert_file, &rfa, NULL, NULL, 0, 0);
6118 if (err)
6119 goto sync;
6121 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6122 repo, progress_cb, progress_arg, NULL, NULL);
6123 sync:
6124 sync_err = sync_fileindex(fileindex, fileindex_path);
6125 if (sync_err && err == NULL)
6126 err = sync_err;
6127 done:
6128 got_ref_close(resolved);
6129 free(tree_id);
6130 free(fileindex_path);
6132 unlockerr = lock_worktree(worktree, LOCK_SH);
6133 if (unlockerr && err == NULL)
6134 err = unlockerr;
6135 return err;
6138 const struct got_error *
6139 got_worktree_histedit_complete(struct got_worktree *worktree,
6140 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6141 struct got_reference *edited_branch, struct got_repository *repo)
6143 const struct got_error *err, *unlockerr;
6144 struct got_object_id *new_head_commit_id = NULL;
6145 struct got_reference *resolved = NULL;
6147 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6148 if (err)
6149 return err;
6151 err = got_ref_open(&resolved, repo,
6152 got_ref_get_symref_target(edited_branch), 0);
6153 if (err)
6154 goto done;
6156 err = got_ref_change_ref(resolved, new_head_commit_id);
6157 if (err)
6158 goto done;
6160 err = got_ref_write(resolved, repo);
6161 if (err)
6162 goto done;
6164 err = got_worktree_set_head_ref(worktree, resolved);
6165 if (err)
6166 goto done;
6168 err = delete_histedit_refs(worktree, repo);
6169 done:
6170 if (fileindex)
6171 got_fileindex_free(fileindex);
6172 free(new_head_commit_id);
6173 unlockerr = lock_worktree(worktree, LOCK_SH);
6174 if (unlockerr && err == NULL)
6175 err = unlockerr;
6176 return err;
6179 const struct got_error *
6180 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6181 struct got_object_id *commit_id, struct got_repository *repo)
6183 const struct got_error *err;
6184 char *commit_ref_name;
6186 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6187 if (err)
6188 return err;
6190 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6191 if (err)
6192 goto done;
6194 err = delete_ref(commit_ref_name, repo);
6195 done:
6196 free(commit_ref_name);
6197 return err;
6200 const struct got_error *
6201 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6202 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6203 struct got_worktree *worktree, const char *refname,
6204 struct got_repository *repo)
6206 const struct got_error *err = NULL;
6207 char *fileindex_path = NULL;
6208 struct check_rebase_ok_arg ok_arg;
6210 *fileindex = NULL;
6211 *branch_ref = NULL;
6212 *base_branch_ref = NULL;
6214 err = lock_worktree(worktree, LOCK_EX);
6215 if (err)
6216 return err;
6218 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6219 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6220 "cannot integrate a branch into itself; "
6221 "update -b or different branch name required");
6222 goto done;
6225 err = open_fileindex(fileindex, &fileindex_path, worktree);
6226 if (err)
6227 goto done;
6229 /* Preconditions are the same as for rebase. */
6230 ok_arg.worktree = worktree;
6231 ok_arg.repo = repo;
6232 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6233 &ok_arg);
6234 if (err)
6235 goto done;
6237 err = got_ref_open(branch_ref, repo, refname, 1);
6238 if (err)
6239 goto done;
6241 err = got_ref_open(base_branch_ref, repo,
6242 got_worktree_get_head_ref_name(worktree), 1);
6243 done:
6244 if (err) {
6245 if (*branch_ref) {
6246 got_ref_close(*branch_ref);
6247 *branch_ref = NULL;
6249 if (*base_branch_ref) {
6250 got_ref_close(*base_branch_ref);
6251 *base_branch_ref = NULL;
6253 if (*fileindex) {
6254 got_fileindex_free(*fileindex);
6255 *fileindex = NULL;
6257 lock_worktree(worktree, LOCK_SH);
6259 return err;
6262 const struct got_error *
6263 got_worktree_integrate_continue(struct got_worktree *worktree,
6264 struct got_fileindex *fileindex, struct got_repository *repo,
6265 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6266 got_worktree_checkout_cb progress_cb, void *progress_arg,
6267 got_cancel_cb cancel_cb, void *cancel_arg)
6269 const struct got_error *err = NULL, *sync_err, *unlockerr;
6270 char *fileindex_path = NULL;
6271 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6273 err = get_fileindex_path(&fileindex_path, worktree);
6274 if (err)
6275 goto done;
6277 err = got_ref_resolve(&commit_id, repo, branch_ref);
6278 if (err)
6279 goto done;
6281 err = got_object_id_by_path(&tree_id, repo, commit_id,
6282 worktree->path_prefix);
6283 if (err)
6284 goto done;
6286 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6287 if (err)
6288 goto done;
6290 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6291 progress_cb, progress_arg, cancel_cb, cancel_arg);
6292 if (err)
6293 goto sync;
6295 err = got_ref_change_ref(base_branch_ref, commit_id);
6296 if (err)
6297 goto sync;
6299 err = got_ref_write(base_branch_ref, repo);
6300 sync:
6301 sync_err = sync_fileindex(fileindex, fileindex_path);
6302 if (sync_err && err == NULL)
6303 err = sync_err;
6305 done:
6306 unlockerr = got_ref_unlock(branch_ref);
6307 if (unlockerr && err == NULL)
6308 err = unlockerr;
6309 got_ref_close(branch_ref);
6311 unlockerr = got_ref_unlock(base_branch_ref);
6312 if (unlockerr && err == NULL)
6313 err = unlockerr;
6314 got_ref_close(base_branch_ref);
6316 got_fileindex_free(fileindex);
6317 free(fileindex_path);
6318 free(tree_id);
6320 unlockerr = lock_worktree(worktree, LOCK_SH);
6321 if (unlockerr && err == NULL)
6322 err = unlockerr;
6323 return err;
6326 const struct got_error *
6327 got_worktree_integrate_abort(struct got_worktree *worktree,
6328 struct got_fileindex *fileindex, struct got_repository *repo,
6329 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6331 const struct got_error *err = NULL, *unlockerr = NULL;
6333 got_fileindex_free(fileindex);
6335 err = lock_worktree(worktree, LOCK_SH);
6337 unlockerr = got_ref_unlock(branch_ref);
6338 if (unlockerr && err == NULL)
6339 err = unlockerr;
6340 got_ref_close(branch_ref);
6342 unlockerr = got_ref_unlock(base_branch_ref);
6343 if (unlockerr && err == NULL)
6344 err = unlockerr;
6345 got_ref_close(base_branch_ref);
6347 return err;
6350 struct check_stage_ok_arg {
6351 struct got_object_id *head_commit_id;
6352 struct got_worktree *worktree;
6353 struct got_fileindex *fileindex;
6354 struct got_repository *repo;
6355 int have_changes;
6358 const struct got_error *
6359 check_stage_ok(void *arg, unsigned char status,
6360 unsigned char staged_status, const char *relpath,
6361 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6362 struct got_object_id *commit_id, int dirfd, const char *de_name)
6364 struct check_stage_ok_arg *a = arg;
6365 const struct got_error *err = NULL;
6366 struct got_fileindex_entry *ie;
6367 struct got_object_id base_commit_id;
6368 struct got_object_id *base_commit_idp = NULL;
6369 char *in_repo_path = NULL, *p;
6371 if (status == GOT_STATUS_UNVERSIONED ||
6372 status == GOT_STATUS_NO_CHANGE)
6373 return NULL;
6374 if (status == GOT_STATUS_NONEXISTENT)
6375 return got_error_set_errno(ENOENT, relpath);
6377 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6378 if (ie == NULL)
6379 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6381 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6382 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6383 relpath) == -1)
6384 return got_error_from_errno("asprintf");
6386 if (got_fileindex_entry_has_commit(ie)) {
6387 memcpy(base_commit_id.sha1, ie->commit_sha1,
6388 SHA1_DIGEST_LENGTH);
6389 base_commit_idp = &base_commit_id;
6392 if (status == GOT_STATUS_CONFLICT) {
6393 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6394 goto done;
6395 } else if (status != GOT_STATUS_ADD &&
6396 status != GOT_STATUS_MODIFY &&
6397 status != GOT_STATUS_DELETE) {
6398 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6399 goto done;
6402 a->have_changes = 1;
6404 p = in_repo_path;
6405 while (p[0] == '/')
6406 p++;
6407 err = check_out_of_date(p, status, staged_status,
6408 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6409 GOT_ERR_STAGE_OUT_OF_DATE);
6410 done:
6411 free(in_repo_path);
6412 return err;
6415 struct stage_path_arg {
6416 struct got_worktree *worktree;
6417 struct got_fileindex *fileindex;
6418 struct got_repository *repo;
6419 got_worktree_status_cb status_cb;
6420 void *status_arg;
6421 got_worktree_patch_cb patch_cb;
6422 void *patch_arg;
6423 int staged_something;
6426 static const struct got_error *
6427 stage_path(void *arg, unsigned char status,
6428 unsigned char staged_status, const char *relpath,
6429 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6430 struct got_object_id *commit_id, int dirfd, const char *de_name)
6432 struct stage_path_arg *a = arg;
6433 const struct got_error *err = NULL;
6434 struct got_fileindex_entry *ie;
6435 char *ondisk_path = NULL, *path_content = NULL;
6436 uint32_t stage;
6437 struct got_object_id *new_staged_blob_id = NULL;
6439 if (status == GOT_STATUS_UNVERSIONED)
6440 return NULL;
6442 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6443 if (ie == NULL)
6444 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6446 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6447 relpath)== -1)
6448 return got_error_from_errno("asprintf");
6450 switch (status) {
6451 case GOT_STATUS_ADD:
6452 case GOT_STATUS_MODIFY:
6453 if (a->patch_cb) {
6454 if (status == GOT_STATUS_ADD) {
6455 int choice = GOT_PATCH_CHOICE_NONE;
6456 err = (*a->patch_cb)(&choice, a->patch_arg,
6457 status, ie->path, NULL, 1, 1);
6458 if (err)
6459 break;
6460 if (choice != GOT_PATCH_CHOICE_YES)
6461 break;
6462 } else {
6463 err = create_patched_content(&path_content, 0,
6464 staged_blob_id ? staged_blob_id : blob_id,
6465 ondisk_path, dirfd, de_name, ie->path,
6466 a->repo, a->patch_cb, a->patch_arg);
6467 if (err || path_content == NULL)
6468 break;
6471 err = got_object_blob_create(&new_staged_blob_id,
6472 path_content ? path_content : ondisk_path, a->repo);
6473 if (err)
6474 break;
6475 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6476 SHA1_DIGEST_LENGTH);
6477 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6478 stage = GOT_FILEIDX_STAGE_ADD;
6479 else
6480 stage = GOT_FILEIDX_STAGE_MODIFY;
6481 got_fileindex_entry_stage_set(ie, stage);
6482 a->staged_something = 1;
6483 if (a->status_cb == NULL)
6484 break;
6485 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6486 get_staged_status(ie), relpath, blob_id,
6487 new_staged_blob_id, NULL, dirfd, de_name);
6488 break;
6489 case GOT_STATUS_DELETE:
6490 if (staged_status == GOT_STATUS_DELETE)
6491 break;
6492 if (a->patch_cb) {
6493 int choice = GOT_PATCH_CHOICE_NONE;
6494 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6495 ie->path, NULL, 1, 1);
6496 if (err)
6497 break;
6498 if (choice == GOT_PATCH_CHOICE_NO)
6499 break;
6500 if (choice != GOT_PATCH_CHOICE_YES) {
6501 err = got_error(GOT_ERR_PATCH_CHOICE);
6502 break;
6505 stage = GOT_FILEIDX_STAGE_DELETE;
6506 got_fileindex_entry_stage_set(ie, stage);
6507 a->staged_something = 1;
6508 if (a->status_cb == NULL)
6509 break;
6510 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6511 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6512 de_name);
6513 break;
6514 case GOT_STATUS_NO_CHANGE:
6515 break;
6516 case GOT_STATUS_CONFLICT:
6517 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6518 break;
6519 case GOT_STATUS_NONEXISTENT:
6520 err = got_error_set_errno(ENOENT, relpath);
6521 break;
6522 default:
6523 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6524 break;
6527 if (path_content && unlink(path_content) == -1 && err == NULL)
6528 err = got_error_from_errno2("unlink", path_content);
6529 free(path_content);
6530 free(ondisk_path);
6531 free(new_staged_blob_id);
6532 return err;
6535 const struct got_error *
6536 got_worktree_stage(struct got_worktree *worktree,
6537 struct got_pathlist_head *paths,
6538 got_worktree_status_cb status_cb, void *status_arg,
6539 got_worktree_patch_cb patch_cb, void *patch_arg,
6540 struct got_repository *repo)
6542 const struct got_error *err = NULL, *sync_err, *unlockerr;
6543 struct got_pathlist_entry *pe;
6544 struct got_fileindex *fileindex = NULL;
6545 char *fileindex_path = NULL;
6546 struct got_reference *head_ref = NULL;
6547 struct got_object_id *head_commit_id = NULL;
6548 struct check_stage_ok_arg oka;
6549 struct stage_path_arg spa;
6551 err = lock_worktree(worktree, LOCK_EX);
6552 if (err)
6553 return err;
6555 err = got_ref_open(&head_ref, repo,
6556 got_worktree_get_head_ref_name(worktree), 0);
6557 if (err)
6558 goto done;
6559 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6560 if (err)
6561 goto done;
6562 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6563 if (err)
6564 goto done;
6566 /* Check pre-conditions before staging anything. */
6567 oka.head_commit_id = head_commit_id;
6568 oka.worktree = worktree;
6569 oka.fileindex = fileindex;
6570 oka.repo = repo;
6571 oka.have_changes = 0;
6572 TAILQ_FOREACH(pe, paths, entry) {
6573 err = worktree_status(worktree, pe->path, fileindex, repo,
6574 check_stage_ok, &oka, NULL, NULL, 0, 0);
6575 if (err)
6576 goto done;
6578 if (!oka.have_changes) {
6579 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6580 goto done;
6583 spa.worktree = worktree;
6584 spa.fileindex = fileindex;
6585 spa.repo = repo;
6586 spa.patch_cb = patch_cb;
6587 spa.patch_arg = patch_arg;
6588 spa.status_cb = status_cb;
6589 spa.status_arg = status_arg;
6590 spa.staged_something = 0;
6591 TAILQ_FOREACH(pe, paths, entry) {
6592 err = worktree_status(worktree, pe->path, fileindex, repo,
6593 stage_path, &spa, NULL, NULL, 0, 0);
6594 if (err)
6595 goto done;
6597 if (!spa.staged_something) {
6598 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6599 goto done;
6602 sync_err = sync_fileindex(fileindex, fileindex_path);
6603 if (sync_err && err == NULL)
6604 err = sync_err;
6605 done:
6606 if (head_ref)
6607 got_ref_close(head_ref);
6608 free(head_commit_id);
6609 free(fileindex_path);
6610 if (fileindex)
6611 got_fileindex_free(fileindex);
6612 unlockerr = lock_worktree(worktree, LOCK_SH);
6613 if (unlockerr && err == NULL)
6614 err = unlockerr;
6615 return err;
6618 struct unstage_path_arg {
6619 struct got_worktree *worktree;
6620 struct got_fileindex *fileindex;
6621 struct got_repository *repo;
6622 got_worktree_checkout_cb progress_cb;
6623 void *progress_arg;
6624 got_worktree_patch_cb patch_cb;
6625 void *patch_arg;
6628 static const struct got_error *
6629 create_unstaged_content(char **path_unstaged_content,
6630 char **path_new_staged_content, struct got_object_id *blob_id,
6631 struct got_object_id *staged_blob_id, const char *relpath,
6632 struct got_repository *repo,
6633 got_worktree_patch_cb patch_cb, void *patch_arg)
6635 const struct got_error *err;
6636 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6637 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6638 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6639 struct stat sb1, sb2;
6640 struct got_diff_changes *changes = NULL;
6641 struct got_diff_state *ds = NULL;
6642 struct got_diff_args *args = NULL;
6643 struct got_diff_change *change;
6644 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6645 int have_content = 0, have_rejected_content = 0;
6647 *path_unstaged_content = NULL;
6648 *path_new_staged_content = NULL;
6650 err = got_object_id_str(&label1, blob_id);
6651 if (err)
6652 return err;
6653 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6654 if (err)
6655 goto done;
6657 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6658 if (err)
6659 goto done;
6661 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6662 if (err)
6663 goto done;
6665 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6666 if (err)
6667 goto done;
6669 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6670 if (err)
6671 goto done;
6673 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6674 if (err)
6675 goto done;
6677 if (stat(path1, &sb1) == -1) {
6678 err = got_error_from_errno2("stat", path1);
6679 goto done;
6682 if (stat(path2, &sb2) == -1) {
6683 err = got_error_from_errno2("stat", path2);
6684 goto done;
6687 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6688 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6689 if (err)
6690 goto done;
6692 err = got_opentemp_named(path_unstaged_content, &outfile,
6693 "got-unstaged-content");
6694 if (err)
6695 goto done;
6696 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6697 "got-new-staged-content");
6698 if (err)
6699 goto done;
6701 if (fseek(f1, 0L, SEEK_SET) == -1) {
6702 err = got_ferror(f1, GOT_ERR_IO);
6703 goto done;
6705 if (fseek(f2, 0L, SEEK_SET) == -1) {
6706 err = got_ferror(f2, GOT_ERR_IO);
6707 goto done;
6709 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6710 int choice;
6711 err = apply_or_reject_change(&choice, change, ++n,
6712 changes->nchanges, ds, args, diff_flags, relpath,
6713 f1, f2, &line_cur1, &line_cur2,
6714 outfile, rejectfile, patch_cb, patch_arg);
6715 if (err)
6716 goto done;
6717 if (choice == GOT_PATCH_CHOICE_YES)
6718 have_content = 1;
6719 else
6720 have_rejected_content = 1;
6721 if (choice == GOT_PATCH_CHOICE_QUIT)
6722 break;
6724 if (have_content || have_rejected_content)
6725 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6726 outfile, rejectfile);
6727 done:
6728 free(label1);
6729 if (blob)
6730 got_object_blob_close(blob);
6731 if (staged_blob)
6732 got_object_blob_close(staged_blob);
6733 if (f1 && fclose(f1) == EOF && err == NULL)
6734 err = got_error_from_errno2("fclose", path1);
6735 if (f2 && fclose(f2) == EOF && err == NULL)
6736 err = got_error_from_errno2("fclose", path2);
6737 if (outfile && fclose(outfile) == EOF && err == NULL)
6738 err = got_error_from_errno2("fclose", *path_unstaged_content);
6739 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6740 err = got_error_from_errno2("fclose", *path_new_staged_content);
6741 if (path1 && unlink(path1) == -1 && err == NULL)
6742 err = got_error_from_errno2("unlink", path1);
6743 if (path2 && unlink(path2) == -1 && err == NULL)
6744 err = got_error_from_errno2("unlink", path2);
6745 if (err || !have_content) {
6746 if (*path_unstaged_content &&
6747 unlink(*path_unstaged_content) == -1 && err == NULL)
6748 err = got_error_from_errno2("unlink",
6749 *path_unstaged_content);
6750 free(*path_unstaged_content);
6751 *path_unstaged_content = NULL;
6753 if (err || !have_rejected_content) {
6754 if (*path_new_staged_content &&
6755 unlink(*path_new_staged_content) == -1 && err == NULL)
6756 err = got_error_from_errno2("unlink",
6757 *path_new_staged_content);
6758 free(*path_new_staged_content);
6759 *path_new_staged_content = NULL;
6761 free(args);
6762 if (ds) {
6763 got_diff_state_free(ds);
6764 free(ds);
6766 if (changes)
6767 got_diff_free_changes(changes);
6768 free(path1);
6769 free(path2);
6770 return err;
6773 static const struct got_error *
6774 unstage_path(void *arg, unsigned char status,
6775 unsigned char staged_status, const char *relpath,
6776 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6777 struct got_object_id *commit_id, int dirfd, const char *de_name)
6779 const struct got_error *err = NULL;
6780 struct unstage_path_arg *a = arg;
6781 struct got_fileindex_entry *ie;
6782 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6783 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6784 char *path_new_staged_content = NULL;
6785 char *id_str = NULL, *label_orig = NULL;
6786 int local_changes_subsumed;
6787 struct stat sb;
6789 if (staged_status != GOT_STATUS_ADD &&
6790 staged_status != GOT_STATUS_MODIFY &&
6791 staged_status != GOT_STATUS_DELETE)
6792 return NULL;
6794 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6795 if (ie == NULL)
6796 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6798 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6799 == -1)
6800 return got_error_from_errno("asprintf");
6802 err = got_object_id_str(&id_str,
6803 commit_id ? commit_id : a->worktree->base_commit_id);
6804 if (err)
6805 goto done;
6806 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6807 id_str) == -1) {
6808 err = got_error_from_errno("asprintf");
6809 goto done;
6812 switch (staged_status) {
6813 case GOT_STATUS_MODIFY:
6814 err = got_object_open_as_blob(&blob_base, a->repo,
6815 blob_id, 8192);
6816 if (err)
6817 break;
6818 /* fall through */
6819 case GOT_STATUS_ADD:
6820 if (a->patch_cb) {
6821 if (staged_status == GOT_STATUS_ADD) {
6822 int choice = GOT_PATCH_CHOICE_NONE;
6823 err = (*a->patch_cb)(&choice, a->patch_arg,
6824 staged_status, ie->path, NULL, 1, 1);
6825 if (err)
6826 break;
6827 if (choice != GOT_PATCH_CHOICE_YES)
6828 break;
6829 } else {
6830 err = create_unstaged_content(
6831 &path_unstaged_content,
6832 &path_new_staged_content, blob_id,
6833 staged_blob_id, ie->path, a->repo,
6834 a->patch_cb, a->patch_arg);
6835 if (err || path_unstaged_content == NULL)
6836 break;
6837 if (path_new_staged_content) {
6838 err = got_object_blob_create(
6839 &staged_blob_id,
6840 path_new_staged_content,
6841 a->repo);
6842 if (err)
6843 break;
6844 memcpy(ie->staged_blob_sha1,
6845 staged_blob_id->sha1,
6846 SHA1_DIGEST_LENGTH);
6848 err = merge_file(&local_changes_subsumed,
6849 a->worktree, blob_base, ondisk_path,
6850 relpath, got_fileindex_perms_to_st(ie),
6851 path_unstaged_content, label_orig,
6852 "unstaged", a->repo, a->progress_cb,
6853 a->progress_arg);
6854 if (err == NULL &&
6855 path_new_staged_content == NULL)
6856 got_fileindex_entry_stage_set(ie,
6857 GOT_FILEIDX_STAGE_NONE);
6858 break; /* Done with this file. */
6861 err = got_object_open_as_blob(&blob_staged, a->repo,
6862 staged_blob_id, 8192);
6863 if (err)
6864 break;
6865 err = merge_blob(&local_changes_subsumed, a->worktree,
6866 blob_base, ondisk_path, relpath,
6867 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6868 commit_id ? commit_id : a->worktree->base_commit_id,
6869 a->repo, a->progress_cb, a->progress_arg);
6870 if (err == NULL)
6871 got_fileindex_entry_stage_set(ie,
6872 GOT_FILEIDX_STAGE_NONE);
6873 break;
6874 case GOT_STATUS_DELETE:
6875 if (a->patch_cb) {
6876 int choice = GOT_PATCH_CHOICE_NONE;
6877 err = (*a->patch_cb)(&choice, a->patch_arg,
6878 staged_status, ie->path, NULL, 1, 1);
6879 if (err)
6880 break;
6881 if (choice == GOT_PATCH_CHOICE_NO)
6882 break;
6883 if (choice != GOT_PATCH_CHOICE_YES) {
6884 err = got_error(GOT_ERR_PATCH_CHOICE);
6885 break;
6888 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6889 err = get_file_status(&status, &sb, ie, ondisk_path,
6890 dirfd, de_name, a->repo);
6891 if (err)
6892 break;
6893 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6894 break;
6896 done:
6897 free(ondisk_path);
6898 if (path_unstaged_content &&
6899 unlink(path_unstaged_content) == -1 && err == NULL)
6900 err = got_error_from_errno2("unlink", path_unstaged_content);
6901 if (path_new_staged_content &&
6902 unlink(path_new_staged_content) == -1 && err == NULL)
6903 err = got_error_from_errno2("unlink", path_new_staged_content);
6904 free(path_unstaged_content);
6905 free(path_new_staged_content);
6906 if (blob_base)
6907 got_object_blob_close(blob_base);
6908 if (blob_staged)
6909 got_object_blob_close(blob_staged);
6910 free(id_str);
6911 free(label_orig);
6912 return err;
6915 const struct got_error *
6916 got_worktree_unstage(struct got_worktree *worktree,
6917 struct got_pathlist_head *paths,
6918 got_worktree_checkout_cb progress_cb, void *progress_arg,
6919 got_worktree_patch_cb patch_cb, void *patch_arg,
6920 struct got_repository *repo)
6922 const struct got_error *err = NULL, *sync_err, *unlockerr;
6923 struct got_pathlist_entry *pe;
6924 struct got_fileindex *fileindex = NULL;
6925 char *fileindex_path = NULL;
6926 struct unstage_path_arg upa;
6928 err = lock_worktree(worktree, LOCK_EX);
6929 if (err)
6930 return err;
6932 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6933 if (err)
6934 goto done;
6936 upa.worktree = worktree;
6937 upa.fileindex = fileindex;
6938 upa.repo = repo;
6939 upa.progress_cb = progress_cb;
6940 upa.progress_arg = progress_arg;
6941 upa.patch_cb = patch_cb;
6942 upa.patch_arg = patch_arg;
6943 TAILQ_FOREACH(pe, paths, entry) {
6944 err = worktree_status(worktree, pe->path, fileindex, repo,
6945 unstage_path, &upa, NULL, NULL, 0, 0);
6946 if (err)
6947 goto done;
6950 sync_err = sync_fileindex(fileindex, fileindex_path);
6951 if (sync_err && err == NULL)
6952 err = sync_err;
6953 done:
6954 free(fileindex_path);
6955 if (fileindex)
6956 got_fileindex_free(fileindex);
6957 unlockerr = lock_worktree(worktree, LOCK_SH);
6958 if (unlockerr && err == NULL)
6959 err = unlockerr;
6960 return err;