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)) {
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 /* XXX ignore symlinks for now */
2854 if (de->d_type == DT_LNK)
2855 return NULL;
2857 if (parent_path[0]) {
2858 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2859 return got_error_from_errno("asprintf");
2860 } else {
2861 path = de->d_name;
2864 if (de->d_type != DT_DIR &&
2865 got_path_is_child(path, a->status_path, a->status_path_len)
2866 && !match_ignores(&a->ignores, path))
2867 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2868 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2869 if (parent_path[0])
2870 free(path);
2871 return err;
2874 static const struct got_error *
2875 status_traverse(void *arg, const char *path, int dirfd)
2877 const struct got_error *err = NULL;
2878 struct diff_dir_cb_arg *a = arg;
2880 if (a->no_ignores)
2881 return NULL;
2883 err = add_ignores(&a->ignores, a->worktree->root_path,
2884 path, dirfd, ".cvsignore");
2885 if (err)
2886 return err;
2888 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2889 dirfd, ".gitignore");
2891 return err;
2894 static const struct got_error *
2895 report_single_file_status(const char *path, const char *ondisk_path,
2896 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2897 void *status_arg, struct got_repository *repo, int report_unchanged)
2899 struct got_fileindex_entry *ie;
2900 struct stat sb;
2902 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2903 if (ie)
2904 return report_file_status(ie, ondisk_path, -1, NULL,
2905 status_cb, status_arg, repo, report_unchanged);
2907 if (lstat(ondisk_path, &sb) == -1) {
2908 if (errno != ENOENT)
2909 return got_error_from_errno2("lstat", ondisk_path);
2910 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2911 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2912 return NULL;
2915 if (S_ISREG(sb.st_mode))
2916 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2917 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2919 return NULL;
2922 static const struct got_error *
2923 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2924 const char *root_path, const char *path)
2926 const struct got_error *err;
2927 char *parent_path, *next_parent_path = NULL;
2929 err = add_ignores(ignores, root_path, "", -1,
2930 ".cvsignore");
2931 if (err)
2932 return err;
2934 err = add_ignores(ignores, root_path, "", -1,
2935 ".gitignore");
2936 if (err)
2937 return err;
2939 err = got_path_dirname(&parent_path, path);
2940 if (err) {
2941 if (err->code == GOT_ERR_BAD_PATH)
2942 return NULL; /* cannot traverse parent */
2943 return err;
2945 for (;;) {
2946 err = add_ignores(ignores, root_path, parent_path, -1,
2947 ".cvsignore");
2948 if (err)
2949 break;
2950 err = add_ignores(ignores, root_path, parent_path, -1,
2951 ".gitignore");
2952 if (err)
2953 break;
2954 err = got_path_dirname(&next_parent_path, parent_path);
2955 if (err) {
2956 if (err->code == GOT_ERR_BAD_PATH)
2957 err = NULL; /* traversed everything */
2958 break;
2960 free(parent_path);
2961 parent_path = next_parent_path;
2962 next_parent_path = NULL;
2965 free(parent_path);
2966 free(next_parent_path);
2967 return err;
2970 static const struct got_error *
2971 worktree_status(struct got_worktree *worktree, const char *path,
2972 struct got_fileindex *fileindex, struct got_repository *repo,
2973 got_worktree_status_cb status_cb, void *status_arg,
2974 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2975 int report_unchanged)
2977 const struct got_error *err = NULL;
2978 int fd = -1;
2979 struct got_fileindex_diff_dir_cb fdiff_cb;
2980 struct diff_dir_cb_arg arg;
2981 char *ondisk_path = NULL;
2983 TAILQ_INIT(&arg.ignores);
2985 if (asprintf(&ondisk_path, "%s%s%s",
2986 worktree->root_path, path[0] ? "/" : "", path) == -1)
2987 return got_error_from_errno("asprintf");
2989 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2990 if (fd == -1) {
2991 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2992 err = got_error_from_errno2("open", ondisk_path);
2993 else
2994 err = report_single_file_status(path, ondisk_path,
2995 fileindex, status_cb, status_arg, repo,
2996 report_unchanged);
2997 } else {
2998 fdiff_cb.diff_old_new = status_old_new;
2999 fdiff_cb.diff_old = status_old;
3000 fdiff_cb.diff_new = status_new;
3001 fdiff_cb.diff_traverse = status_traverse;
3002 arg.fileindex = fileindex;
3003 arg.worktree = worktree;
3004 arg.status_path = path;
3005 arg.status_path_len = strlen(path);
3006 arg.repo = repo;
3007 arg.status_cb = status_cb;
3008 arg.status_arg = status_arg;
3009 arg.cancel_cb = cancel_cb;
3010 arg.cancel_arg = cancel_arg;
3011 arg.report_unchanged = report_unchanged;
3012 arg.no_ignores = no_ignores;
3013 if (!no_ignores) {
3014 err = add_ignores_from_parent_paths(&arg.ignores,
3015 worktree->root_path, path);
3016 if (err)
3017 goto done;
3019 err = got_fileindex_diff_dir(fileindex, fd,
3020 worktree->root_path, path, repo, &fdiff_cb, &arg);
3022 done:
3023 free_ignores(&arg.ignores);
3024 if (fd != -1 && close(fd) != 0 && err == NULL)
3025 err = got_error_from_errno("close");
3026 free(ondisk_path);
3027 return err;
3030 const struct got_error *
3031 got_worktree_status(struct got_worktree *worktree,
3032 struct got_pathlist_head *paths, struct got_repository *repo,
3033 got_worktree_status_cb status_cb, void *status_arg,
3034 got_cancel_cb cancel_cb, void *cancel_arg)
3036 const struct got_error *err = NULL;
3037 char *fileindex_path = NULL;
3038 struct got_fileindex *fileindex = NULL;
3039 struct got_pathlist_entry *pe;
3041 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3042 if (err)
3043 return err;
3045 TAILQ_FOREACH(pe, paths, entry) {
3046 err = worktree_status(worktree, pe->path, fileindex, repo,
3047 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3048 if (err)
3049 break;
3051 free(fileindex_path);
3052 got_fileindex_free(fileindex);
3053 return err;
3056 const struct got_error *
3057 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3058 const char *arg)
3060 const struct got_error *err = NULL;
3061 char *resolved, *cwd = NULL, *path = NULL;
3062 size_t len;
3064 *wt_path = NULL;
3066 resolved = realpath(arg, NULL);
3067 if (resolved == NULL) {
3068 if (errno != ENOENT)
3069 return got_error_from_errno2("realpath", arg);
3070 cwd = getcwd(NULL, 0);
3071 if (cwd == NULL)
3072 return got_error_from_errno("getcwd");
3073 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3074 err = got_error_from_errno("asprintf");
3075 goto done;
3079 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3080 strlen(got_worktree_get_root_path(worktree)))) {
3081 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3082 goto done;
3085 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3086 err = got_path_skip_common_ancestor(&path,
3087 got_worktree_get_root_path(worktree), resolved);
3088 if (err)
3089 goto done;
3090 } else {
3091 path = strdup("");
3092 if (path == NULL) {
3093 err = got_error_from_errno("strdup");
3094 goto done;
3098 /* XXX status walk can't deal with trailing slash! */
3099 len = strlen(path);
3100 while (len > 0 && path[len - 1] == '/') {
3101 path[len - 1] = '\0';
3102 len--;
3104 done:
3105 free(resolved);
3106 free(cwd);
3107 if (err == NULL)
3108 *wt_path = path;
3109 else
3110 free(path);
3111 return err;
3114 struct schedule_addition_args {
3115 struct got_worktree *worktree;
3116 struct got_fileindex *fileindex;
3117 got_worktree_checkout_cb progress_cb;
3118 void *progress_arg;
3119 struct got_repository *repo;
3122 static const struct got_error *
3123 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3124 const char *relpath, struct got_object_id *blob_id,
3125 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3126 int dirfd, const char *de_name)
3128 struct schedule_addition_args *a = arg;
3129 const struct got_error *err = NULL;
3130 struct got_fileindex_entry *ie;
3131 struct stat sb;
3132 char *ondisk_path;
3134 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3135 relpath) == -1)
3136 return got_error_from_errno("asprintf");
3138 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3139 if (ie) {
3140 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3141 de_name, a->repo);
3142 if (err)
3143 goto done;
3144 /* Re-adding an existing entry is a no-op. */
3145 if (status == GOT_STATUS_ADD)
3146 goto done;
3147 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3148 if (err)
3149 goto done;
3152 if (status != GOT_STATUS_UNVERSIONED) {
3153 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3154 goto done;
3157 err = got_fileindex_entry_alloc(&ie, relpath);
3158 if (err)
3159 goto done;
3160 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3161 if (err) {
3162 got_fileindex_entry_free(ie);
3163 goto done;
3165 err = got_fileindex_entry_add(a->fileindex, ie);
3166 if (err) {
3167 got_fileindex_entry_free(ie);
3168 goto done;
3170 done:
3171 free(ondisk_path);
3172 if (err)
3173 return err;
3174 if (status == GOT_STATUS_ADD)
3175 return NULL;
3176 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3179 const struct got_error *
3180 got_worktree_schedule_add(struct got_worktree *worktree,
3181 struct got_pathlist_head *paths,
3182 got_worktree_checkout_cb progress_cb, void *progress_arg,
3183 struct got_repository *repo, int no_ignores)
3185 struct got_fileindex *fileindex = NULL;
3186 char *fileindex_path = NULL;
3187 const struct got_error *err = NULL, *sync_err, *unlockerr;
3188 struct got_pathlist_entry *pe;
3189 struct schedule_addition_args saa;
3191 err = lock_worktree(worktree, LOCK_EX);
3192 if (err)
3193 return err;
3195 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3196 if (err)
3197 goto done;
3199 saa.worktree = worktree;
3200 saa.fileindex = fileindex;
3201 saa.progress_cb = progress_cb;
3202 saa.progress_arg = progress_arg;
3203 saa.repo = repo;
3205 TAILQ_FOREACH(pe, paths, entry) {
3206 err = worktree_status(worktree, pe->path, fileindex, repo,
3207 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3208 if (err)
3209 break;
3211 sync_err = sync_fileindex(fileindex, fileindex_path);
3212 if (sync_err && err == NULL)
3213 err = sync_err;
3214 done:
3215 free(fileindex_path);
3216 if (fileindex)
3217 got_fileindex_free(fileindex);
3218 unlockerr = lock_worktree(worktree, LOCK_SH);
3219 if (unlockerr && err == NULL)
3220 err = unlockerr;
3221 return err;
3224 struct schedule_deletion_args {
3225 struct got_worktree *worktree;
3226 struct got_fileindex *fileindex;
3227 got_worktree_delete_cb progress_cb;
3228 void *progress_arg;
3229 struct got_repository *repo;
3230 int delete_local_mods;
3231 int keep_on_disk;
3234 static const struct got_error *
3235 schedule_for_deletion(void *arg, unsigned char status,
3236 unsigned char staged_status, const char *relpath,
3237 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3238 struct got_object_id *commit_id, int dirfd, const char *de_name)
3240 struct schedule_deletion_args *a = arg;
3241 const struct got_error *err = NULL;
3242 struct got_fileindex_entry *ie = NULL;
3243 struct stat sb;
3244 char *ondisk_path, *parent = NULL;
3246 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3247 if (ie == NULL)
3248 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3250 staged_status = get_staged_status(ie);
3251 if (staged_status != GOT_STATUS_NO_CHANGE) {
3252 if (staged_status == GOT_STATUS_DELETE)
3253 return NULL;
3254 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3257 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3258 relpath) == -1)
3259 return got_error_from_errno("asprintf");
3261 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3262 a->repo);
3263 if (err)
3264 goto done;
3266 if (status != GOT_STATUS_NO_CHANGE) {
3267 if (status == GOT_STATUS_DELETE)
3268 goto done;
3269 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3270 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3271 goto done;
3273 if (status != GOT_STATUS_MODIFY &&
3274 status != GOT_STATUS_MISSING) {
3275 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3276 goto done;
3280 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3281 if (dirfd != -1) {
3282 if (unlinkat(dirfd, de_name, 0) != 0) {
3283 err = got_error_from_errno2("unlinkat",
3284 ondisk_path);
3285 goto done;
3287 } else if (unlink(ondisk_path) != 0) {
3288 err = got_error_from_errno2("unlink", ondisk_path);
3289 goto done;
3292 parent = dirname(ondisk_path);
3294 if (parent == NULL) {
3295 err = got_error_from_errno2("dirname", ondisk_path);
3296 goto done;
3298 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3299 if (rmdir(parent) == -1) {
3300 if (errno != ENOTEMPTY)
3301 err = got_error_from_errno2("rmdir",
3302 parent);
3303 break;
3305 parent = dirname(parent);
3306 if (parent == NULL) {
3307 err = got_error_from_errno2("dirname", parent);
3308 goto done;
3313 got_fileindex_entry_mark_deleted_from_disk(ie);
3314 done:
3315 free(ondisk_path);
3316 if (err)
3317 return err;
3318 if (status == GOT_STATUS_DELETE)
3319 return NULL;
3320 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3321 staged_status, relpath);
3324 const struct got_error *
3325 got_worktree_schedule_delete(struct got_worktree *worktree,
3326 struct got_pathlist_head *paths, int delete_local_mods,
3327 got_worktree_delete_cb progress_cb, void *progress_arg,
3328 struct got_repository *repo, int keep_on_disk)
3330 struct got_fileindex *fileindex = NULL;
3331 char *fileindex_path = NULL;
3332 const struct got_error *err = NULL, *sync_err, *unlockerr;
3333 struct got_pathlist_entry *pe;
3334 struct schedule_deletion_args sda;
3336 err = lock_worktree(worktree, LOCK_EX);
3337 if (err)
3338 return err;
3340 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3341 if (err)
3342 goto done;
3344 sda.worktree = worktree;
3345 sda.fileindex = fileindex;
3346 sda.progress_cb = progress_cb;
3347 sda.progress_arg = progress_arg;
3348 sda.repo = repo;
3349 sda.delete_local_mods = delete_local_mods;
3350 sda.keep_on_disk = keep_on_disk;
3352 TAILQ_FOREACH(pe, paths, entry) {
3353 err = worktree_status(worktree, pe->path, fileindex, repo,
3354 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3355 if (err)
3356 break;
3358 sync_err = sync_fileindex(fileindex, fileindex_path);
3359 if (sync_err && err == NULL)
3360 err = sync_err;
3361 done:
3362 free(fileindex_path);
3363 if (fileindex)
3364 got_fileindex_free(fileindex);
3365 unlockerr = lock_worktree(worktree, LOCK_SH);
3366 if (unlockerr && err == NULL)
3367 err = unlockerr;
3368 return err;
3371 static const struct got_error *
3372 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3374 const struct got_error *err = NULL;
3375 char *line = NULL;
3376 size_t linesize = 0, n;
3377 ssize_t linelen;
3379 linelen = getline(&line, &linesize, infile);
3380 if (linelen == -1) {
3381 if (ferror(infile)) {
3382 err = got_error_from_errno("getline");
3383 goto done;
3385 return NULL;
3387 if (outfile) {
3388 n = fwrite(line, 1, linelen, outfile);
3389 if (n != linelen) {
3390 err = got_ferror(outfile, GOT_ERR_IO);
3391 goto done;
3394 if (rejectfile) {
3395 n = fwrite(line, 1, linelen, rejectfile);
3396 if (n != linelen)
3397 err = got_ferror(outfile, GOT_ERR_IO);
3399 done:
3400 free(line);
3401 return err;
3404 static const struct got_error *
3405 skip_one_line(FILE *f)
3407 char *line = NULL;
3408 size_t linesize = 0;
3409 ssize_t linelen;
3411 linelen = getline(&line, &linesize, f);
3412 if (linelen == -1) {
3413 if (ferror(f))
3414 return got_error_from_errno("getline");
3415 return NULL;
3417 free(line);
3418 return NULL;
3421 static const struct got_error *
3422 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3423 int start_old, int end_old, int start_new, int end_new,
3424 FILE *outfile, FILE *rejectfile)
3426 const struct got_error *err;
3428 /* Copy old file's lines leading up to patch. */
3429 while (!feof(f1) && *line_cur1 < start_old) {
3430 err = copy_one_line(f1, outfile, NULL);
3431 if (err)
3432 return err;
3433 (*line_cur1)++;
3435 /* Skip new file's lines leading up to patch. */
3436 while (!feof(f2) && *line_cur2 < start_new) {
3437 if (rejectfile)
3438 err = copy_one_line(f2, NULL, rejectfile);
3439 else
3440 err = skip_one_line(f2);
3441 if (err)
3442 return err;
3443 (*line_cur2)++;
3445 /* Copy patched lines. */
3446 while (!feof(f2) && *line_cur2 <= end_new) {
3447 err = copy_one_line(f2, outfile, NULL);
3448 if (err)
3449 return err;
3450 (*line_cur2)++;
3452 /* Skip over old file's replaced lines. */
3453 while (!feof(f1) && *line_cur1 <= end_old) {
3454 if (rejectfile)
3455 err = copy_one_line(f1, NULL, rejectfile);
3456 else
3457 err = skip_one_line(f1);
3458 if (err)
3459 return err;
3460 (*line_cur1)++;
3463 return NULL;
3466 static const struct got_error *
3467 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3468 FILE *outfile, FILE *rejectfile)
3470 const struct got_error *err;
3472 if (outfile) {
3473 /* Copy old file's lines until EOF. */
3474 while (!feof(f1)) {
3475 err = copy_one_line(f1, outfile, NULL);
3476 if (err)
3477 return err;
3478 (*line_cur1)++;
3481 if (rejectfile) {
3482 /* Copy new file's lines until EOF. */
3483 while (!feof(f2)) {
3484 err = copy_one_line(f2, NULL, rejectfile);
3485 if (err)
3486 return err;
3487 (*line_cur2)++;
3491 return NULL;
3494 static const struct got_error *
3495 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3496 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3497 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3498 int *line_cur2, FILE *outfile, FILE *rejectfile,
3499 got_worktree_patch_cb patch_cb, void *patch_arg)
3501 const struct got_error *err = NULL;
3502 int start_old = change->cv.a;
3503 int end_old = change->cv.b;
3504 int start_new = change->cv.c;
3505 int end_new = change->cv.d;
3506 long pos1, pos2;
3507 FILE *hunkfile;
3509 *choice = GOT_PATCH_CHOICE_NONE;
3511 hunkfile = got_opentemp();
3512 if (hunkfile == NULL)
3513 return got_error_from_errno("got_opentemp");
3515 pos1 = ftell(f1);
3516 pos2 = ftell(f2);
3518 /* XXX TODO needs error checking */
3519 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3521 if (fseek(f1, pos1, SEEK_SET) == -1) {
3522 err = got_ferror(f1, GOT_ERR_IO);
3523 goto done;
3525 if (fseek(f2, pos2, SEEK_SET) == -1) {
3526 err = got_ferror(f1, GOT_ERR_IO);
3527 goto done;
3529 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3530 err = got_ferror(hunkfile, GOT_ERR_IO);
3531 goto done;
3534 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3535 hunkfile, n, nchanges);
3536 if (err)
3537 goto done;
3539 switch (*choice) {
3540 case GOT_PATCH_CHOICE_YES:
3541 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3542 end_old, start_new, end_new, outfile, rejectfile);
3543 break;
3544 case GOT_PATCH_CHOICE_NO:
3545 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3546 end_old, start_new, end_new, rejectfile, outfile);
3547 break;
3548 case GOT_PATCH_CHOICE_QUIT:
3549 break;
3550 default:
3551 err = got_error(GOT_ERR_PATCH_CHOICE);
3552 break;
3554 done:
3555 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3556 err = got_error_from_errno("fclose");
3557 return err;
3560 struct revert_file_args {
3561 struct got_worktree *worktree;
3562 struct got_fileindex *fileindex;
3563 got_worktree_checkout_cb progress_cb;
3564 void *progress_arg;
3565 got_worktree_patch_cb patch_cb;
3566 void *patch_arg;
3567 struct got_repository *repo;
3570 static const struct got_error *
3571 create_patched_content(char **path_outfile, int reverse_patch,
3572 struct got_object_id *blob_id, const char *path2,
3573 int dirfd2, const char *de_name2,
3574 const char *relpath, struct got_repository *repo,
3575 got_worktree_patch_cb patch_cb, void *patch_arg)
3577 const struct got_error *err;
3578 struct got_blob_object *blob = NULL;
3579 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3580 int fd2 = -1;
3581 char *path1 = NULL, *id_str = NULL;
3582 struct stat sb1, sb2;
3583 struct got_diff_changes *changes = NULL;
3584 struct got_diff_state *ds = NULL;
3585 struct got_diff_args *args = NULL;
3586 struct got_diff_change *change;
3587 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3588 int n = 0;
3590 *path_outfile = NULL;
3592 err = got_object_id_str(&id_str, blob_id);
3593 if (err)
3594 return err;
3596 if (dirfd2 != -1) {
3597 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3598 if (fd2 == -1) {
3599 err = got_error_from_errno2("openat", path2);
3600 goto done;
3602 } else {
3603 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3604 if (fd2 == -1) {
3605 err = got_error_from_errno2("open", path2);
3606 goto done;
3609 if (fstat(fd2, &sb2) == -1) {
3610 err = got_error_from_errno2("fstat", path2);
3611 goto done;
3614 f2 = fdopen(fd2, "r");
3615 if (f2 == NULL) {
3616 err = got_error_from_errno2("fdopen", path2);
3617 goto done;
3619 fd2 = -1;
3621 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3622 if (err)
3623 goto done;
3625 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3626 if (err)
3627 goto done;
3629 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3630 if (err)
3631 goto done;
3633 if (stat(path1, &sb1) == -1) {
3634 err = got_error_from_errno2("stat", path1);
3635 goto done;
3638 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3639 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3640 if (err)
3641 goto done;
3643 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3644 if (err)
3645 goto done;
3647 if (fseek(f1, 0L, SEEK_SET) == -1)
3648 return got_ferror(f1, GOT_ERR_IO);
3649 if (fseek(f2, 0L, SEEK_SET) == -1)
3650 return got_ferror(f2, GOT_ERR_IO);
3651 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3652 int choice;
3653 err = apply_or_reject_change(&choice, change, ++n,
3654 changes->nchanges, ds, args, diff_flags, relpath,
3655 f1, f2, &line_cur1, &line_cur2,
3656 reverse_patch ? NULL : outfile,
3657 reverse_patch ? outfile : NULL,
3658 patch_cb, patch_arg);
3659 if (err)
3660 goto done;
3661 if (choice == GOT_PATCH_CHOICE_YES)
3662 have_content = 1;
3663 else if (choice == GOT_PATCH_CHOICE_QUIT)
3664 break;
3666 if (have_content) {
3667 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3668 reverse_patch ? NULL : outfile,
3669 reverse_patch ? outfile : NULL);
3670 if (err)
3671 goto done;
3673 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3674 err = got_error_from_errno2("chmod", path2);
3675 goto done;
3678 done:
3679 free(id_str);
3680 if (blob)
3681 got_object_blob_close(blob);
3682 if (f1 && fclose(f1) == EOF && err == NULL)
3683 err = got_error_from_errno2("fclose", path1);
3684 if (f2 && fclose(f2) == EOF && err == NULL)
3685 err = got_error_from_errno2("fclose", path2);
3686 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3687 err = got_error_from_errno2("close", path2);
3688 if (outfile && fclose(outfile) == EOF && err == NULL)
3689 err = got_error_from_errno2("fclose", *path_outfile);
3690 if (path1 && unlink(path1) == -1 && err == NULL)
3691 err = got_error_from_errno2("unlink", path1);
3692 if (err || !have_content) {
3693 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3694 err = got_error_from_errno2("unlink", *path_outfile);
3695 free(*path_outfile);
3696 *path_outfile = NULL;
3698 free(args);
3699 if (ds) {
3700 got_diff_state_free(ds);
3701 free(ds);
3703 if (changes)
3704 got_diff_free_changes(changes);
3705 free(path1);
3706 return err;
3709 static const struct got_error *
3710 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3711 const char *relpath, struct got_object_id *blob_id,
3712 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3713 int dirfd, const char *de_name)
3715 struct revert_file_args *a = arg;
3716 const struct got_error *err = NULL;
3717 char *parent_path = NULL;
3718 struct got_fileindex_entry *ie;
3719 struct got_tree_object *tree = NULL;
3720 struct got_object_id *tree_id = NULL;
3721 const struct got_tree_entry *te = NULL;
3722 char *tree_path = NULL, *te_name;
3723 char *ondisk_path = NULL, *path_content = NULL;
3724 struct got_blob_object *blob = NULL;
3726 /* Reverting a staged deletion is a no-op. */
3727 if (status == GOT_STATUS_DELETE &&
3728 staged_status != GOT_STATUS_NO_CHANGE)
3729 return NULL;
3731 if (status == GOT_STATUS_UNVERSIONED)
3732 return (*a->progress_cb)(a->progress_arg,
3733 GOT_STATUS_UNVERSIONED, relpath);
3735 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3736 if (ie == NULL)
3737 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3739 /* Construct in-repository path of tree which contains this blob. */
3740 err = got_path_dirname(&parent_path, ie->path);
3741 if (err) {
3742 if (err->code != GOT_ERR_BAD_PATH)
3743 goto done;
3744 parent_path = strdup("/");
3745 if (parent_path == NULL) {
3746 err = got_error_from_errno("strdup");
3747 goto done;
3750 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3751 tree_path = strdup(parent_path);
3752 if (tree_path == NULL) {
3753 err = got_error_from_errno("strdup");
3754 goto done;
3756 } else {
3757 if (got_path_is_root_dir(parent_path)) {
3758 tree_path = strdup(a->worktree->path_prefix);
3759 if (tree_path == NULL) {
3760 err = got_error_from_errno("strdup");
3761 goto done;
3763 } else {
3764 if (asprintf(&tree_path, "%s/%s",
3765 a->worktree->path_prefix, parent_path) == -1) {
3766 err = got_error_from_errno("asprintf");
3767 goto done;
3772 err = got_object_id_by_path(&tree_id, a->repo,
3773 a->worktree->base_commit_id, tree_path);
3774 if (err) {
3775 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3776 (status == GOT_STATUS_ADD ||
3777 staged_status == GOT_STATUS_ADD)))
3778 goto done;
3779 } else {
3780 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3781 if (err)
3782 goto done;
3784 te_name = basename(ie->path);
3785 if (te_name == NULL) {
3786 err = got_error_from_errno2("basename", ie->path);
3787 goto done;
3790 te = got_object_tree_find_entry(tree, te_name);
3791 if (te == NULL && status != GOT_STATUS_ADD &&
3792 staged_status != GOT_STATUS_ADD) {
3793 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3794 goto done;
3798 switch (status) {
3799 case GOT_STATUS_ADD:
3800 if (a->patch_cb) {
3801 int choice = GOT_PATCH_CHOICE_NONE;
3802 err = (*a->patch_cb)(&choice, a->patch_arg,
3803 status, ie->path, NULL, 1, 1);
3804 if (err)
3805 goto done;
3806 if (choice != GOT_PATCH_CHOICE_YES)
3807 break;
3809 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3810 ie->path);
3811 if (err)
3812 goto done;
3813 got_fileindex_entry_remove(a->fileindex, ie);
3814 break;
3815 case GOT_STATUS_DELETE:
3816 if (a->patch_cb) {
3817 int choice = GOT_PATCH_CHOICE_NONE;
3818 err = (*a->patch_cb)(&choice, a->patch_arg,
3819 status, ie->path, NULL, 1, 1);
3820 if (err)
3821 goto done;
3822 if (choice != GOT_PATCH_CHOICE_YES)
3823 break;
3825 /* fall through */
3826 case GOT_STATUS_MODIFY:
3827 case GOT_STATUS_MODE_CHANGE:
3828 case GOT_STATUS_CONFLICT:
3829 case GOT_STATUS_MISSING: {
3830 struct got_object_id id;
3831 if (staged_status == GOT_STATUS_ADD ||
3832 staged_status == GOT_STATUS_MODIFY) {
3833 memcpy(id.sha1, ie->staged_blob_sha1,
3834 SHA1_DIGEST_LENGTH);
3835 } else
3836 memcpy(id.sha1, ie->blob_sha1,
3837 SHA1_DIGEST_LENGTH);
3838 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3839 if (err)
3840 goto done;
3842 if (asprintf(&ondisk_path, "%s/%s",
3843 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3844 err = got_error_from_errno("asprintf");
3845 goto done;
3848 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3849 status == GOT_STATUS_CONFLICT)) {
3850 err = create_patched_content(&path_content, 1, &id,
3851 ondisk_path, dirfd, de_name, ie->path, a->repo,
3852 a->patch_cb, a->patch_arg);
3853 if (err || path_content == NULL)
3854 break;
3855 if (rename(path_content, ondisk_path) == -1) {
3856 err = got_error_from_errno3("rename",
3857 path_content, ondisk_path);
3858 goto done;
3860 } else {
3861 err = install_blob(a->worktree, ondisk_path, ie->path,
3862 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3863 got_fileindex_perms_to_st(ie), blob, 0, 1,
3864 a->repo, a->progress_cb, a->progress_arg);
3865 if (err)
3866 goto done;
3867 if (status == GOT_STATUS_DELETE ||
3868 status == GOT_STATUS_MODE_CHANGE) {
3869 err = got_fileindex_entry_update(ie,
3870 ondisk_path, blob->id.sha1,
3871 a->worktree->base_commit_id->sha1, 1);
3872 if (err)
3873 goto done;
3876 break;
3878 default:
3879 break;
3881 done:
3882 free(ondisk_path);
3883 free(path_content);
3884 free(parent_path);
3885 free(tree_path);
3886 if (blob)
3887 got_object_blob_close(blob);
3888 if (tree)
3889 got_object_tree_close(tree);
3890 free(tree_id);
3891 return err;
3894 const struct got_error *
3895 got_worktree_revert(struct got_worktree *worktree,
3896 struct got_pathlist_head *paths,
3897 got_worktree_checkout_cb progress_cb, void *progress_arg,
3898 got_worktree_patch_cb patch_cb, void *patch_arg,
3899 struct got_repository *repo)
3901 struct got_fileindex *fileindex = NULL;
3902 char *fileindex_path = NULL;
3903 const struct got_error *err = NULL, *unlockerr = NULL;
3904 const struct got_error *sync_err = NULL;
3905 struct got_pathlist_entry *pe;
3906 struct revert_file_args rfa;
3908 err = lock_worktree(worktree, LOCK_EX);
3909 if (err)
3910 return err;
3912 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3913 if (err)
3914 goto done;
3916 rfa.worktree = worktree;
3917 rfa.fileindex = fileindex;
3918 rfa.progress_cb = progress_cb;
3919 rfa.progress_arg = progress_arg;
3920 rfa.patch_cb = patch_cb;
3921 rfa.patch_arg = patch_arg;
3922 rfa.repo = repo;
3923 TAILQ_FOREACH(pe, paths, entry) {
3924 err = worktree_status(worktree, pe->path, fileindex, repo,
3925 revert_file, &rfa, NULL, NULL, 0, 0);
3926 if (err)
3927 break;
3929 sync_err = sync_fileindex(fileindex, fileindex_path);
3930 if (sync_err && err == NULL)
3931 err = sync_err;
3932 done:
3933 free(fileindex_path);
3934 if (fileindex)
3935 got_fileindex_free(fileindex);
3936 unlockerr = lock_worktree(worktree, LOCK_SH);
3937 if (unlockerr && err == NULL)
3938 err = unlockerr;
3939 return err;
3942 static void
3943 free_commitable(struct got_commitable *ct)
3945 free(ct->path);
3946 free(ct->in_repo_path);
3947 free(ct->ondisk_path);
3948 free(ct->blob_id);
3949 free(ct->base_blob_id);
3950 free(ct->staged_blob_id);
3951 free(ct->base_commit_id);
3952 free(ct);
3955 struct collect_commitables_arg {
3956 struct got_pathlist_head *commitable_paths;
3957 struct got_repository *repo;
3958 struct got_worktree *worktree;
3959 int have_staged_files;
3962 static const struct got_error *
3963 collect_commitables(void *arg, unsigned char status,
3964 unsigned char staged_status, const char *relpath,
3965 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3966 struct got_object_id *commit_id, int dirfd, const char *de_name)
3968 struct collect_commitables_arg *a = arg;
3969 const struct got_error *err = NULL;
3970 struct got_commitable *ct = NULL;
3971 struct got_pathlist_entry *new = NULL;
3972 char *parent_path = NULL, *path = NULL;
3973 struct stat sb;
3975 if (a->have_staged_files) {
3976 if (staged_status != GOT_STATUS_MODIFY &&
3977 staged_status != GOT_STATUS_ADD &&
3978 staged_status != GOT_STATUS_DELETE)
3979 return NULL;
3980 } else {
3981 if (status == GOT_STATUS_CONFLICT)
3982 return got_error(GOT_ERR_COMMIT_CONFLICT);
3984 if (status != GOT_STATUS_MODIFY &&
3985 status != GOT_STATUS_MODE_CHANGE &&
3986 status != GOT_STATUS_ADD &&
3987 status != GOT_STATUS_DELETE)
3988 return NULL;
3991 if (asprintf(&path, "/%s", relpath) == -1) {
3992 err = got_error_from_errno("asprintf");
3993 goto done;
3995 if (strcmp(path, "/") == 0) {
3996 parent_path = strdup("");
3997 if (parent_path == NULL)
3998 return got_error_from_errno("strdup");
3999 } else {
4000 err = got_path_dirname(&parent_path, path);
4001 if (err)
4002 return err;
4005 ct = calloc(1, sizeof(*ct));
4006 if (ct == NULL) {
4007 err = got_error_from_errno("calloc");
4008 goto done;
4011 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4012 relpath) == -1) {
4013 err = got_error_from_errno("asprintf");
4014 goto done;
4016 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4017 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4018 } else {
4019 if (dirfd != -1) {
4020 if (fstatat(dirfd, de_name, &sb,
4021 AT_SYMLINK_NOFOLLOW) == -1) {
4022 err = got_error_from_errno2("fstatat",
4023 ct->ondisk_path);
4024 goto done;
4026 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4027 err = got_error_from_errno2("lstat", ct->ondisk_path);
4028 goto done;
4030 ct->mode = sb.st_mode;
4033 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4034 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4035 relpath) == -1) {
4036 err = got_error_from_errno("asprintf");
4037 goto done;
4040 ct->status = status;
4041 ct->staged_status = staged_status;
4042 ct->blob_id = NULL; /* will be filled in when blob gets created */
4043 if (ct->status != GOT_STATUS_ADD &&
4044 ct->staged_status != GOT_STATUS_ADD) {
4045 ct->base_blob_id = got_object_id_dup(blob_id);
4046 if (ct->base_blob_id == NULL) {
4047 err = got_error_from_errno("got_object_id_dup");
4048 goto done;
4050 ct->base_commit_id = got_object_id_dup(commit_id);
4051 if (ct->base_commit_id == NULL) {
4052 err = got_error_from_errno("got_object_id_dup");
4053 goto done;
4056 if (ct->staged_status == GOT_STATUS_ADD ||
4057 ct->staged_status == GOT_STATUS_MODIFY) {
4058 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4059 if (ct->staged_blob_id == NULL) {
4060 err = got_error_from_errno("got_object_id_dup");
4061 goto done;
4064 ct->path = strdup(path);
4065 if (ct->path == NULL) {
4066 err = got_error_from_errno("strdup");
4067 goto done;
4069 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4070 done:
4071 if (ct && (err || new == NULL))
4072 free_commitable(ct);
4073 free(parent_path);
4074 free(path);
4075 return err;
4078 static const struct got_error *write_tree(struct got_object_id **, int *,
4079 struct got_tree_object *, const char *, struct got_pathlist_head *,
4080 got_worktree_status_cb status_cb, void *status_arg,
4081 struct got_repository *);
4083 static const struct got_error *
4084 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4085 struct got_tree_entry *te, const char *parent_path,
4086 struct got_pathlist_head *commitable_paths,
4087 got_worktree_status_cb status_cb, void *status_arg,
4088 struct got_repository *repo)
4090 const struct got_error *err = NULL;
4091 struct got_tree_object *subtree;
4092 char *subpath;
4094 if (asprintf(&subpath, "%s%s%s", parent_path,
4095 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4096 return got_error_from_errno("asprintf");
4098 err = got_object_open_as_tree(&subtree, repo, &te->id);
4099 if (err)
4100 return err;
4102 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4103 commitable_paths, status_cb, status_arg, repo);
4104 got_object_tree_close(subtree);
4105 free(subpath);
4106 return err;
4109 static const struct got_error *
4110 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4112 const struct got_error *err = NULL;
4113 char *ct_parent_path = NULL;
4115 *match = 0;
4117 if (strchr(ct->in_repo_path, '/') == NULL) {
4118 *match = got_path_is_root_dir(path);
4119 return NULL;
4122 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4123 if (err)
4124 return err;
4125 *match = (strcmp(path, ct_parent_path) == 0);
4126 free(ct_parent_path);
4127 return err;
4130 static mode_t
4131 get_ct_file_mode(struct got_commitable *ct)
4133 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4136 static const struct got_error *
4137 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4138 struct got_tree_entry *te, struct got_commitable *ct)
4140 const struct got_error *err = NULL;
4142 *new_te = NULL;
4144 err = got_object_tree_entry_dup(new_te, te);
4145 if (err)
4146 goto done;
4148 (*new_te)->mode = get_ct_file_mode(ct);
4150 if (ct->staged_status == GOT_STATUS_MODIFY)
4151 memcpy(&(*new_te)->id, ct->staged_blob_id,
4152 sizeof((*new_te)->id));
4153 else
4154 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4155 done:
4156 if (err && *new_te) {
4157 free(*new_te);
4158 *new_te = NULL;
4160 return err;
4163 static const struct got_error *
4164 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4165 struct got_commitable *ct)
4167 const struct got_error *err = NULL;
4168 char *ct_name;
4170 *new_te = NULL;
4172 *new_te = calloc(1, sizeof(**new_te));
4173 if (*new_te == NULL)
4174 return got_error_from_errno("calloc");
4176 ct_name = basename(ct->path);
4177 if (ct_name == NULL) {
4178 err = got_error_from_errno2("basename", ct->path);
4179 goto done;
4181 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4182 sizeof((*new_te)->name)) {
4183 err = got_error(GOT_ERR_NO_SPACE);
4184 goto done;
4187 (*new_te)->mode = get_ct_file_mode(ct);
4189 if (ct->staged_status == GOT_STATUS_ADD)
4190 memcpy(&(*new_te)->id, ct->staged_blob_id,
4191 sizeof((*new_te)->id));
4192 else
4193 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4194 done:
4195 if (err && *new_te) {
4196 free(*new_te);
4197 *new_te = NULL;
4199 return err;
4202 static const struct got_error *
4203 insert_tree_entry(struct got_tree_entry *new_te,
4204 struct got_pathlist_head *paths)
4206 const struct got_error *err = NULL;
4207 struct got_pathlist_entry *new_pe;
4209 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4210 if (err)
4211 return err;
4212 if (new_pe == NULL)
4213 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4214 return NULL;
4217 static const struct got_error *
4218 report_ct_status(struct got_commitable *ct,
4219 got_worktree_status_cb status_cb, void *status_arg)
4221 const char *ct_path = ct->path;
4222 unsigned char status;
4224 while (ct_path[0] == '/')
4225 ct_path++;
4227 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4228 status = ct->staged_status;
4229 else
4230 status = ct->status;
4232 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4233 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4236 static const struct got_error *
4237 match_modified_subtree(int *modified, struct got_tree_entry *te,
4238 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4240 const struct got_error *err = NULL;
4241 struct got_pathlist_entry *pe;
4242 char *te_path;
4244 *modified = 0;
4246 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4247 got_path_is_root_dir(base_tree_path) ? "" : "/",
4248 te->name) == -1)
4249 return got_error_from_errno("asprintf");
4251 TAILQ_FOREACH(pe, commitable_paths, entry) {
4252 struct got_commitable *ct = pe->data;
4253 *modified = got_path_is_child(ct->in_repo_path, te_path,
4254 strlen(te_path));
4255 if (*modified)
4256 break;
4259 free(te_path);
4260 return err;
4263 static const struct got_error *
4264 match_deleted_or_modified_ct(struct got_commitable **ctp,
4265 struct got_tree_entry *te, const char *base_tree_path,
4266 struct got_pathlist_head *commitable_paths)
4268 const struct got_error *err = NULL;
4269 struct got_pathlist_entry *pe;
4271 *ctp = NULL;
4273 TAILQ_FOREACH(pe, commitable_paths, entry) {
4274 struct got_commitable *ct = pe->data;
4275 char *ct_name = NULL;
4276 int path_matches;
4278 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4279 if (ct->status != GOT_STATUS_MODIFY &&
4280 ct->status != GOT_STATUS_MODE_CHANGE &&
4281 ct->status != GOT_STATUS_DELETE)
4282 continue;
4283 } else {
4284 if (ct->staged_status != GOT_STATUS_MODIFY &&
4285 ct->staged_status != GOT_STATUS_DELETE)
4286 continue;
4289 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4290 continue;
4292 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4293 if (err)
4294 return err;
4295 if (!path_matches)
4296 continue;
4298 ct_name = basename(pe->path);
4299 if (ct_name == NULL)
4300 return got_error_from_errno2("basename", pe->path);
4302 if (strcmp(te->name, ct_name) != 0)
4303 continue;
4305 *ctp = ct;
4306 break;
4309 return err;
4312 static const struct got_error *
4313 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4314 const char *child_path, const char *path_base_tree,
4315 struct got_pathlist_head *commitable_paths,
4316 got_worktree_status_cb status_cb, void *status_arg,
4317 struct got_repository *repo)
4319 const struct got_error *err = NULL;
4320 struct got_tree_entry *new_te;
4321 char *subtree_path;
4322 struct got_object_id *id = NULL;
4323 int nentries;
4325 *new_tep = NULL;
4327 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4328 got_path_is_root_dir(path_base_tree) ? "" : "/",
4329 child_path) == -1)
4330 return got_error_from_errno("asprintf");
4332 new_te = calloc(1, sizeof(*new_te));
4333 if (new_te == NULL)
4334 return got_error_from_errno("calloc");
4335 new_te->mode = S_IFDIR;
4337 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4338 sizeof(new_te->name)) {
4339 err = got_error(GOT_ERR_NO_SPACE);
4340 goto done;
4342 err = write_tree(&id, &nentries, NULL, subtree_path,
4343 commitable_paths, status_cb, status_arg, repo);
4344 if (err) {
4345 free(new_te);
4346 goto done;
4348 memcpy(&new_te->id, id, sizeof(new_te->id));
4349 done:
4350 free(id);
4351 free(subtree_path);
4352 if (err == NULL)
4353 *new_tep = new_te;
4354 return err;
4357 static const struct got_error *
4358 write_tree(struct got_object_id **new_tree_id, int *nentries,
4359 struct got_tree_object *base_tree, const char *path_base_tree,
4360 struct got_pathlist_head *commitable_paths,
4361 got_worktree_status_cb status_cb, void *status_arg,
4362 struct got_repository *repo)
4364 const struct got_error *err = NULL;
4365 struct got_pathlist_head paths;
4366 struct got_tree_entry *te, *new_te = NULL;
4367 struct got_pathlist_entry *pe;
4369 TAILQ_INIT(&paths);
4370 *nentries = 0;
4372 /* Insert, and recurse into, newly added entries first. */
4373 TAILQ_FOREACH(pe, commitable_paths, entry) {
4374 struct got_commitable *ct = pe->data;
4375 char *child_path = NULL, *slash;
4377 if ((ct->status != GOT_STATUS_ADD &&
4378 ct->staged_status != GOT_STATUS_ADD) ||
4379 (ct->flags & GOT_COMMITABLE_ADDED))
4380 continue;
4382 if (!got_path_is_child(pe->path, path_base_tree,
4383 strlen(path_base_tree)))
4384 continue;
4386 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4387 pe->path);
4388 if (err)
4389 goto done;
4391 slash = strchr(child_path, '/');
4392 if (slash == NULL) {
4393 err = alloc_added_blob_tree_entry(&new_te, ct);
4394 if (err)
4395 goto done;
4396 err = report_ct_status(ct, status_cb, status_arg);
4397 if (err)
4398 goto done;
4399 ct->flags |= GOT_COMMITABLE_ADDED;
4400 err = insert_tree_entry(new_te, &paths);
4401 if (err)
4402 goto done;
4403 (*nentries)++;
4404 } else {
4405 *slash = '\0'; /* trim trailing path components */
4406 if (base_tree == NULL ||
4407 got_object_tree_find_entry(base_tree, child_path)
4408 == NULL) {
4409 err = make_subtree_for_added_blob(&new_te,
4410 child_path, path_base_tree,
4411 commitable_paths, status_cb, status_arg,
4412 repo);
4413 if (err)
4414 goto done;
4415 err = insert_tree_entry(new_te, &paths);
4416 if (err)
4417 goto done;
4418 (*nentries)++;
4423 if (base_tree) {
4424 int i, nbase_entries;
4425 /* Handle modified and deleted entries. */
4426 nbase_entries = got_object_tree_get_nentries(base_tree);
4427 for (i = 0; i < nbase_entries; i++) {
4428 struct got_commitable *ct = NULL;
4430 te = got_object_tree_get_entry(base_tree, i);
4431 if (got_object_tree_entry_is_submodule(te)) {
4432 /* Entry is a submodule; just copy it. */
4433 err = got_object_tree_entry_dup(&new_te, te);
4434 if (err)
4435 goto done;
4436 err = insert_tree_entry(new_te, &paths);
4437 if (err)
4438 goto done;
4439 (*nentries)++;
4440 continue;
4443 if (S_ISDIR(te->mode)) {
4444 int modified;
4445 err = got_object_tree_entry_dup(&new_te, te);
4446 if (err)
4447 goto done;
4448 err = match_modified_subtree(&modified, te,
4449 path_base_tree, commitable_paths);
4450 if (err)
4451 goto done;
4452 /* Avoid recursion into unmodified subtrees. */
4453 if (modified) {
4454 struct got_object_id *new_id;
4455 int nsubentries;
4456 err = write_subtree(&new_id,
4457 &nsubentries, te,
4458 path_base_tree, commitable_paths,
4459 status_cb, status_arg, repo);
4460 if (err)
4461 goto done;
4462 if (nsubentries == 0) {
4463 /* All entries were deleted. */
4464 free(new_id);
4465 continue;
4467 memcpy(&new_te->id, new_id,
4468 sizeof(new_te->id));
4469 free(new_id);
4471 err = insert_tree_entry(new_te, &paths);
4472 if (err)
4473 goto done;
4474 (*nentries)++;
4475 continue;
4478 err = match_deleted_or_modified_ct(&ct, te,
4479 path_base_tree, commitable_paths);
4480 if (err)
4481 goto done;
4482 if (ct) {
4483 /* NB: Deleted entries get dropped here. */
4484 if (ct->status == GOT_STATUS_MODIFY ||
4485 ct->status == GOT_STATUS_MODE_CHANGE ||
4486 ct->staged_status == GOT_STATUS_MODIFY) {
4487 err = alloc_modified_blob_tree_entry(
4488 &new_te, te, ct);
4489 if (err)
4490 goto done;
4491 err = insert_tree_entry(new_te, &paths);
4492 if (err)
4493 goto done;
4494 (*nentries)++;
4496 err = report_ct_status(ct, status_cb,
4497 status_arg);
4498 if (err)
4499 goto done;
4500 } else {
4501 /* Entry is unchanged; just copy it. */
4502 err = got_object_tree_entry_dup(&new_te, te);
4503 if (err)
4504 goto done;
4505 err = insert_tree_entry(new_te, &paths);
4506 if (err)
4507 goto done;
4508 (*nentries)++;
4513 /* Write new list of entries; deleted entries have been dropped. */
4514 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4515 done:
4516 got_pathlist_free(&paths);
4517 return err;
4520 static const struct got_error *
4521 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4522 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4523 int have_staged_files)
4525 const struct got_error *err = NULL;
4526 struct got_pathlist_entry *pe;
4528 TAILQ_FOREACH(pe, commitable_paths, entry) {
4529 struct got_fileindex_entry *ie;
4530 struct got_commitable *ct = pe->data;
4532 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4533 if (ie) {
4534 if (ct->status == GOT_STATUS_DELETE ||
4535 ct->staged_status == GOT_STATUS_DELETE) {
4536 got_fileindex_entry_remove(fileindex, ie);
4537 } else if (ct->staged_status == GOT_STATUS_ADD ||
4538 ct->staged_status == GOT_STATUS_MODIFY) {
4539 got_fileindex_entry_stage_set(ie,
4540 GOT_FILEIDX_STAGE_NONE);
4541 err = got_fileindex_entry_update(ie,
4542 ct->ondisk_path, ct->staged_blob_id->sha1,
4543 new_base_commit_id->sha1,
4544 !have_staged_files);
4545 } else
4546 err = got_fileindex_entry_update(ie,
4547 ct->ondisk_path, ct->blob_id->sha1,
4548 new_base_commit_id->sha1,
4549 !have_staged_files);
4550 } else {
4551 err = got_fileindex_entry_alloc(&ie, pe->path);
4552 if (err)
4553 break;
4554 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4555 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4556 if (err) {
4557 got_fileindex_entry_free(ie);
4558 break;
4560 err = got_fileindex_entry_add(fileindex, ie);
4561 if (err) {
4562 got_fileindex_entry_free(ie);
4563 break;
4567 return err;
4571 static const struct got_error *
4572 check_out_of_date(const char *in_repo_path, unsigned char status,
4573 unsigned char staged_status, struct got_object_id *base_blob_id,
4574 struct got_object_id *base_commit_id,
4575 struct got_object_id *head_commit_id, struct got_repository *repo,
4576 int ood_errcode)
4578 const struct got_error *err = NULL;
4579 struct got_object_id *id = NULL;
4581 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4582 /* Trivial case: base commit == head commit */
4583 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4584 return NULL;
4586 * Ensure file content which local changes were based
4587 * on matches file content in the branch head.
4589 err = got_object_id_by_path(&id, repo, head_commit_id,
4590 in_repo_path);
4591 if (err) {
4592 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4593 err = got_error(ood_errcode);
4594 goto done;
4595 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4596 err = got_error(ood_errcode);
4597 } else {
4598 /* Require that added files don't exist in the branch head. */
4599 err = got_object_id_by_path(&id, repo, head_commit_id,
4600 in_repo_path);
4601 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4602 goto done;
4603 err = id ? got_error(ood_errcode) : NULL;
4605 done:
4606 free(id);
4607 return err;
4610 const struct got_error *
4611 commit_worktree(struct got_object_id **new_commit_id,
4612 struct got_pathlist_head *commitable_paths,
4613 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4614 const char *author, const char *committer,
4615 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4616 got_worktree_status_cb status_cb, void *status_arg,
4617 struct got_repository *repo)
4619 const struct got_error *err = NULL, *unlockerr = NULL;
4620 struct got_pathlist_entry *pe;
4621 const char *head_ref_name = NULL;
4622 struct got_commit_object *head_commit = NULL;
4623 struct got_reference *head_ref2 = NULL;
4624 struct got_object_id *head_commit_id2 = NULL;
4625 struct got_tree_object *head_tree = NULL;
4626 struct got_object_id *new_tree_id = NULL;
4627 int nentries;
4628 struct got_object_id_queue parent_ids;
4629 struct got_object_qid *pid = NULL;
4630 char *logmsg = NULL;
4632 *new_commit_id = NULL;
4634 SIMPLEQ_INIT(&parent_ids);
4636 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4637 if (err)
4638 goto done;
4640 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4641 if (err)
4642 goto done;
4644 if (commit_msg_cb != NULL) {
4645 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4646 if (err)
4647 goto done;
4650 if (logmsg == NULL || strlen(logmsg) == 0) {
4651 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4652 goto done;
4655 /* Create blobs from added and modified files and record their IDs. */
4656 TAILQ_FOREACH(pe, commitable_paths, entry) {
4657 struct got_commitable *ct = pe->data;
4658 char *ondisk_path;
4660 /* Blobs for staged files already exist. */
4661 if (ct->staged_status == GOT_STATUS_ADD ||
4662 ct->staged_status == GOT_STATUS_MODIFY)
4663 continue;
4665 if (ct->status != GOT_STATUS_ADD &&
4666 ct->status != GOT_STATUS_MODIFY &&
4667 ct->status != GOT_STATUS_MODE_CHANGE)
4668 continue;
4670 if (asprintf(&ondisk_path, "%s/%s",
4671 worktree->root_path, pe->path) == -1) {
4672 err = got_error_from_errno("asprintf");
4673 goto done;
4675 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4676 free(ondisk_path);
4677 if (err)
4678 goto done;
4681 /* Recursively write new tree objects. */
4682 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4683 commitable_paths, status_cb, status_arg, repo);
4684 if (err)
4685 goto done;
4687 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4688 if (err)
4689 goto done;
4690 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4691 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4692 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4693 got_object_qid_free(pid);
4694 if (logmsg != NULL)
4695 free(logmsg);
4696 if (err)
4697 goto done;
4699 /* Check if a concurrent commit to our branch has occurred. */
4700 head_ref_name = got_worktree_get_head_ref_name(worktree);
4701 if (head_ref_name == NULL) {
4702 err = got_error_from_errno("got_worktree_get_head_ref_name");
4703 goto done;
4705 /* Lock the reference here to prevent concurrent modification. */
4706 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4707 if (err)
4708 goto done;
4709 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4710 if (err)
4711 goto done;
4712 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4713 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4714 goto done;
4716 /* Update branch head in repository. */
4717 err = got_ref_change_ref(head_ref2, *new_commit_id);
4718 if (err)
4719 goto done;
4720 err = got_ref_write(head_ref2, repo);
4721 if (err)
4722 goto done;
4724 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4725 if (err)
4726 goto done;
4728 err = ref_base_commit(worktree, repo);
4729 if (err)
4730 goto done;
4731 done:
4732 if (head_tree)
4733 got_object_tree_close(head_tree);
4734 if (head_commit)
4735 got_object_commit_close(head_commit);
4736 free(head_commit_id2);
4737 if (head_ref2) {
4738 unlockerr = got_ref_unlock(head_ref2);
4739 if (unlockerr && err == NULL)
4740 err = unlockerr;
4741 got_ref_close(head_ref2);
4743 return err;
4746 static const struct got_error *
4747 check_path_is_commitable(const char *path,
4748 struct got_pathlist_head *commitable_paths)
4750 struct got_pathlist_entry *cpe = NULL;
4751 size_t path_len = strlen(path);
4753 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4754 struct got_commitable *ct = cpe->data;
4755 const char *ct_path = ct->path;
4757 while (ct_path[0] == '/')
4758 ct_path++;
4760 if (strcmp(path, ct_path) == 0 ||
4761 got_path_is_child(ct_path, path, path_len))
4762 break;
4765 if (cpe == NULL)
4766 return got_error_path(path, GOT_ERR_BAD_PATH);
4768 return NULL;
4771 static const struct got_error *
4772 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4774 int *have_staged_files = arg;
4776 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4777 *have_staged_files = 1;
4778 return got_error(GOT_ERR_CANCELLED);
4781 return NULL;
4784 static const struct got_error *
4785 check_non_staged_files(struct got_fileindex *fileindex,
4786 struct got_pathlist_head *paths)
4788 struct got_pathlist_entry *pe;
4789 struct got_fileindex_entry *ie;
4791 TAILQ_FOREACH(pe, paths, entry) {
4792 if (pe->path[0] == '\0')
4793 continue;
4794 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4795 if (ie == NULL)
4796 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4797 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4798 return got_error_path(pe->path,
4799 GOT_ERR_FILE_NOT_STAGED);
4802 return NULL;
4805 const struct got_error *
4806 got_worktree_commit(struct got_object_id **new_commit_id,
4807 struct got_worktree *worktree, struct got_pathlist_head *paths,
4808 const char *author, const char *committer,
4809 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4810 got_worktree_status_cb status_cb, void *status_arg,
4811 struct got_repository *repo)
4813 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4814 struct got_fileindex *fileindex = NULL;
4815 char *fileindex_path = NULL;
4816 struct got_pathlist_head commitable_paths;
4817 struct collect_commitables_arg cc_arg;
4818 struct got_pathlist_entry *pe;
4819 struct got_reference *head_ref = NULL;
4820 struct got_object_id *head_commit_id = NULL;
4821 int have_staged_files = 0;
4823 *new_commit_id = NULL;
4825 TAILQ_INIT(&commitable_paths);
4827 err = lock_worktree(worktree, LOCK_EX);
4828 if (err)
4829 goto done;
4831 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4832 if (err)
4833 goto done;
4835 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4836 if (err)
4837 goto done;
4839 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4840 if (err)
4841 goto done;
4843 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4844 &have_staged_files);
4845 if (err && err->code != GOT_ERR_CANCELLED)
4846 goto done;
4847 if (have_staged_files) {
4848 err = check_non_staged_files(fileindex, paths);
4849 if (err)
4850 goto done;
4853 cc_arg.commitable_paths = &commitable_paths;
4854 cc_arg.worktree = worktree;
4855 cc_arg.repo = repo;
4856 cc_arg.have_staged_files = have_staged_files;
4857 TAILQ_FOREACH(pe, paths, entry) {
4858 err = worktree_status(worktree, pe->path, fileindex, repo,
4859 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4860 if (err)
4861 goto done;
4864 if (TAILQ_EMPTY(&commitable_paths)) {
4865 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4866 goto done;
4869 TAILQ_FOREACH(pe, paths, entry) {
4870 err = check_path_is_commitable(pe->path, &commitable_paths);
4871 if (err)
4872 goto done;
4875 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4876 struct got_commitable *ct = pe->data;
4877 const char *ct_path = ct->in_repo_path;
4879 while (ct_path[0] == '/')
4880 ct_path++;
4881 err = check_out_of_date(ct_path, ct->status,
4882 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4883 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4884 if (err)
4885 goto done;
4889 err = commit_worktree(new_commit_id, &commitable_paths,
4890 head_commit_id, worktree, author, committer,
4891 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4892 if (err)
4893 goto done;
4895 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4896 fileindex, have_staged_files);
4897 sync_err = sync_fileindex(fileindex, fileindex_path);
4898 if (sync_err && err == NULL)
4899 err = sync_err;
4900 done:
4901 if (fileindex)
4902 got_fileindex_free(fileindex);
4903 free(fileindex_path);
4904 unlockerr = lock_worktree(worktree, LOCK_SH);
4905 if (unlockerr && err == NULL)
4906 err = unlockerr;
4907 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4908 struct got_commitable *ct = pe->data;
4909 free_commitable(ct);
4911 got_pathlist_free(&commitable_paths);
4912 return err;
4915 const char *
4916 got_commitable_get_path(struct got_commitable *ct)
4918 return ct->path;
4921 unsigned int
4922 got_commitable_get_status(struct got_commitable *ct)
4924 return ct->status;
4927 struct check_rebase_ok_arg {
4928 struct got_worktree *worktree;
4929 struct got_repository *repo;
4932 static const struct got_error *
4933 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4935 const struct got_error *err = NULL;
4936 struct check_rebase_ok_arg *a = arg;
4937 unsigned char status;
4938 struct stat sb;
4939 char *ondisk_path;
4941 /* Reject rebase of a work tree with mixed base commits. */
4942 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4943 SHA1_DIGEST_LENGTH))
4944 return got_error(GOT_ERR_MIXED_COMMITS);
4946 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4947 == -1)
4948 return got_error_from_errno("asprintf");
4950 /* Reject rebase of a work tree with modified or staged files. */
4951 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4952 free(ondisk_path);
4953 if (err)
4954 return err;
4956 if (status != GOT_STATUS_NO_CHANGE)
4957 return got_error(GOT_ERR_MODIFIED);
4958 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4959 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4961 return NULL;
4964 const struct got_error *
4965 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4966 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4967 struct got_worktree *worktree, struct got_reference *branch,
4968 struct got_repository *repo)
4970 const struct got_error *err = NULL;
4971 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4972 char *branch_ref_name = NULL;
4973 char *fileindex_path = NULL;
4974 struct check_rebase_ok_arg ok_arg;
4975 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4976 struct got_object_id *wt_branch_tip = NULL;
4978 *new_base_branch_ref = NULL;
4979 *tmp_branch = NULL;
4980 *fileindex = NULL;
4982 err = lock_worktree(worktree, LOCK_EX);
4983 if (err)
4984 return err;
4986 err = open_fileindex(fileindex, &fileindex_path, worktree);
4987 if (err)
4988 goto done;
4990 ok_arg.worktree = worktree;
4991 ok_arg.repo = repo;
4992 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4993 &ok_arg);
4994 if (err)
4995 goto done;
4997 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4998 if (err)
4999 goto done;
5001 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5002 if (err)
5003 goto done;
5005 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5006 if (err)
5007 goto done;
5009 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5010 0);
5011 if (err)
5012 goto done;
5014 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5015 if (err)
5016 goto done;
5017 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5018 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5019 goto done;
5022 err = got_ref_alloc_symref(new_base_branch_ref,
5023 new_base_branch_ref_name, wt_branch);
5024 if (err)
5025 goto done;
5026 err = got_ref_write(*new_base_branch_ref, repo);
5027 if (err)
5028 goto done;
5030 /* TODO Lock original branch's ref while rebasing? */
5032 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5033 if (err)
5034 goto done;
5036 err = got_ref_write(branch_ref, repo);
5037 if (err)
5038 goto done;
5040 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5041 worktree->base_commit_id);
5042 if (err)
5043 goto done;
5044 err = got_ref_write(*tmp_branch, repo);
5045 if (err)
5046 goto done;
5048 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5049 if (err)
5050 goto done;
5051 done:
5052 free(fileindex_path);
5053 free(tmp_branch_name);
5054 free(new_base_branch_ref_name);
5055 free(branch_ref_name);
5056 if (branch_ref)
5057 got_ref_close(branch_ref);
5058 if (wt_branch)
5059 got_ref_close(wt_branch);
5060 free(wt_branch_tip);
5061 if (err) {
5062 if (*new_base_branch_ref) {
5063 got_ref_close(*new_base_branch_ref);
5064 *new_base_branch_ref = NULL;
5066 if (*tmp_branch) {
5067 got_ref_close(*tmp_branch);
5068 *tmp_branch = NULL;
5070 if (*fileindex) {
5071 got_fileindex_free(*fileindex);
5072 *fileindex = NULL;
5074 lock_worktree(worktree, LOCK_SH);
5076 return err;
5079 const struct got_error *
5080 got_worktree_rebase_continue(struct got_object_id **commit_id,
5081 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5082 struct got_reference **branch, struct got_fileindex **fileindex,
5083 struct got_worktree *worktree, struct got_repository *repo)
5085 const struct got_error *err;
5086 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5087 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5088 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5089 char *fileindex_path = NULL;
5090 int have_staged_files = 0;
5092 *commit_id = NULL;
5093 *new_base_branch = NULL;
5094 *tmp_branch = NULL;
5095 *branch = NULL;
5096 *fileindex = NULL;
5098 err = lock_worktree(worktree, LOCK_EX);
5099 if (err)
5100 return err;
5102 err = open_fileindex(fileindex, &fileindex_path, worktree);
5103 if (err)
5104 goto done;
5106 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5107 &have_staged_files);
5108 if (err && err->code != GOT_ERR_CANCELLED)
5109 goto done;
5110 if (have_staged_files) {
5111 err = got_error(GOT_ERR_STAGED_PATHS);
5112 goto done;
5115 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5116 if (err)
5117 goto done;
5119 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5120 if (err)
5121 goto done;
5123 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5124 if (err)
5125 goto done;
5127 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5128 if (err)
5129 goto done;
5131 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5132 if (err)
5133 goto done;
5135 err = got_ref_open(branch, repo,
5136 got_ref_get_symref_target(branch_ref), 0);
5137 if (err)
5138 goto done;
5140 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5141 if (err)
5142 goto done;
5144 err = got_ref_resolve(commit_id, repo, commit_ref);
5145 if (err)
5146 goto done;
5148 err = got_ref_open(new_base_branch, repo,
5149 new_base_branch_ref_name, 0);
5150 if (err)
5151 goto done;
5153 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5154 if (err)
5155 goto done;
5156 done:
5157 free(commit_ref_name);
5158 free(branch_ref_name);
5159 free(fileindex_path);
5160 if (commit_ref)
5161 got_ref_close(commit_ref);
5162 if (branch_ref)
5163 got_ref_close(branch_ref);
5164 if (err) {
5165 free(*commit_id);
5166 *commit_id = NULL;
5167 if (*tmp_branch) {
5168 got_ref_close(*tmp_branch);
5169 *tmp_branch = NULL;
5171 if (*new_base_branch) {
5172 got_ref_close(*new_base_branch);
5173 *new_base_branch = NULL;
5175 if (*branch) {
5176 got_ref_close(*branch);
5177 *branch = NULL;
5179 if (*fileindex) {
5180 got_fileindex_free(*fileindex);
5181 *fileindex = NULL;
5183 lock_worktree(worktree, LOCK_SH);
5185 return err;
5188 const struct got_error *
5189 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5191 const struct got_error *err;
5192 char *tmp_branch_name = NULL;
5194 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5195 if (err)
5196 return err;
5198 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5199 free(tmp_branch_name);
5200 return NULL;
5203 static const struct got_error *
5204 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5205 char **logmsg, void *arg)
5207 *logmsg = arg;
5208 return NULL;
5211 static const struct got_error *
5212 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5213 const char *path, struct got_object_id *blob_id,
5214 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5215 int dirfd, const char *de_name)
5217 return NULL;
5220 struct collect_merged_paths_arg {
5221 got_worktree_checkout_cb progress_cb;
5222 void *progress_arg;
5223 struct got_pathlist_head *merged_paths;
5226 static const struct got_error *
5227 collect_merged_paths(void *arg, unsigned char status, const char *path)
5229 const struct got_error *err;
5230 struct collect_merged_paths_arg *a = arg;
5231 char *p;
5232 struct got_pathlist_entry *new;
5234 err = (*a->progress_cb)(a->progress_arg, status, path);
5235 if (err)
5236 return err;
5238 if (status != GOT_STATUS_MERGE &&
5239 status != GOT_STATUS_ADD &&
5240 status != GOT_STATUS_DELETE &&
5241 status != GOT_STATUS_CONFLICT)
5242 return NULL;
5244 p = strdup(path);
5245 if (p == NULL)
5246 return got_error_from_errno("strdup");
5248 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5249 if (err || new == NULL)
5250 free(p);
5251 return err;
5254 void
5255 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5257 struct got_pathlist_entry *pe;
5259 TAILQ_FOREACH(pe, merged_paths, entry)
5260 free((char *)pe->path);
5262 got_pathlist_free(merged_paths);
5265 static const struct got_error *
5266 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5267 int is_rebase, struct got_repository *repo)
5269 const struct got_error *err;
5270 struct got_reference *commit_ref = NULL;
5272 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5273 if (err) {
5274 if (err->code != GOT_ERR_NOT_REF)
5275 goto done;
5276 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5277 if (err)
5278 goto done;
5279 err = got_ref_write(commit_ref, repo);
5280 if (err)
5281 goto done;
5282 } else if (is_rebase) {
5283 struct got_object_id *stored_id;
5284 int cmp;
5286 err = got_ref_resolve(&stored_id, repo, commit_ref);
5287 if (err)
5288 goto done;
5289 cmp = got_object_id_cmp(commit_id, stored_id);
5290 free(stored_id);
5291 if (cmp != 0) {
5292 err = got_error(GOT_ERR_REBASE_COMMITID);
5293 goto done;
5296 done:
5297 if (commit_ref)
5298 got_ref_close(commit_ref);
5299 return err;
5302 static const struct got_error *
5303 rebase_merge_files(struct got_pathlist_head *merged_paths,
5304 const char *commit_ref_name, struct got_worktree *worktree,
5305 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5306 struct got_object_id *commit_id, struct got_repository *repo,
5307 got_worktree_checkout_cb progress_cb, void *progress_arg,
5308 got_cancel_cb cancel_cb, void *cancel_arg)
5310 const struct got_error *err;
5311 struct got_reference *commit_ref = NULL;
5312 struct collect_merged_paths_arg cmp_arg;
5313 char *fileindex_path;
5315 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5317 err = get_fileindex_path(&fileindex_path, worktree);
5318 if (err)
5319 return err;
5321 cmp_arg.progress_cb = progress_cb;
5322 cmp_arg.progress_arg = progress_arg;
5323 cmp_arg.merged_paths = merged_paths;
5324 err = merge_files(worktree, fileindex, fileindex_path,
5325 parent_commit_id, commit_id, repo, collect_merged_paths,
5326 &cmp_arg, cancel_cb, cancel_arg);
5327 if (commit_ref)
5328 got_ref_close(commit_ref);
5329 return err;
5332 const struct got_error *
5333 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5334 struct got_worktree *worktree, struct got_fileindex *fileindex,
5335 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5336 struct got_repository *repo,
5337 got_worktree_checkout_cb progress_cb, void *progress_arg,
5338 got_cancel_cb cancel_cb, void *cancel_arg)
5340 const struct got_error *err;
5341 char *commit_ref_name;
5343 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5344 if (err)
5345 return err;
5347 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5348 if (err)
5349 goto done;
5351 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5352 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5353 progress_arg, cancel_cb, cancel_arg);
5354 done:
5355 free(commit_ref_name);
5356 return err;
5359 const struct got_error *
5360 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5361 struct got_worktree *worktree, struct got_fileindex *fileindex,
5362 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5363 struct got_repository *repo,
5364 got_worktree_checkout_cb progress_cb, void *progress_arg,
5365 got_cancel_cb cancel_cb, void *cancel_arg)
5367 const struct got_error *err;
5368 char *commit_ref_name;
5370 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5371 if (err)
5372 return err;
5374 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5375 if (err)
5376 goto done;
5378 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5379 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5380 progress_arg, cancel_cb, cancel_arg);
5381 done:
5382 free(commit_ref_name);
5383 return err;
5386 static const struct got_error *
5387 rebase_commit(struct got_object_id **new_commit_id,
5388 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5389 struct got_worktree *worktree, struct got_fileindex *fileindex,
5390 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5391 const char *new_logmsg, struct got_repository *repo)
5393 const struct got_error *err, *sync_err;
5394 struct got_pathlist_head commitable_paths;
5395 struct collect_commitables_arg cc_arg;
5396 char *fileindex_path = NULL;
5397 struct got_reference *head_ref = NULL;
5398 struct got_object_id *head_commit_id = NULL;
5399 char *logmsg = NULL;
5401 TAILQ_INIT(&commitable_paths);
5402 *new_commit_id = NULL;
5404 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5406 err = get_fileindex_path(&fileindex_path, worktree);
5407 if (err)
5408 return err;
5410 cc_arg.commitable_paths = &commitable_paths;
5411 cc_arg.worktree = worktree;
5412 cc_arg.repo = repo;
5413 cc_arg.have_staged_files = 0;
5415 * If possible get the status of individual files directly to
5416 * avoid crawling the entire work tree once per rebased commit.
5417 * TODO: Ideally, merged_paths would contain a list of commitables
5418 * we could use so we could skip worktree_status() entirely.
5420 if (merged_paths) {
5421 struct got_pathlist_entry *pe;
5422 TAILQ_FOREACH(pe, merged_paths, entry) {
5423 err = worktree_status(worktree, pe->path, fileindex,
5424 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5425 0);
5426 if (err)
5427 goto done;
5429 } else {
5430 err = worktree_status(worktree, "", fileindex, repo,
5431 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5432 if (err)
5433 goto done;
5436 if (TAILQ_EMPTY(&commitable_paths)) {
5437 /* No-op change; commit will be elided. */
5438 err = got_ref_delete(commit_ref, repo);
5439 if (err)
5440 goto done;
5441 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5442 goto done;
5445 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5446 if (err)
5447 goto done;
5449 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5450 if (err)
5451 goto done;
5453 if (new_logmsg) {
5454 logmsg = strdup(new_logmsg);
5455 if (logmsg == NULL) {
5456 err = got_error_from_errno("strdup");
5457 goto done;
5459 } else {
5460 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5461 if (err)
5462 goto done;
5465 /* NB: commit_worktree will call free(logmsg) */
5466 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5467 worktree, got_object_commit_get_author(orig_commit),
5468 got_object_commit_get_committer(orig_commit),
5469 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5470 if (err)
5471 goto done;
5473 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5474 if (err)
5475 goto done;
5477 err = got_ref_delete(commit_ref, repo);
5478 if (err)
5479 goto done;
5481 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5482 fileindex, 0);
5483 sync_err = sync_fileindex(fileindex, fileindex_path);
5484 if (sync_err && err == NULL)
5485 err = sync_err;
5486 done:
5487 free(fileindex_path);
5488 free(head_commit_id);
5489 if (head_ref)
5490 got_ref_close(head_ref);
5491 if (err) {
5492 free(*new_commit_id);
5493 *new_commit_id = NULL;
5495 return err;
5498 const struct got_error *
5499 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5500 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5501 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5502 struct got_commit_object *orig_commit,
5503 struct got_object_id *orig_commit_id, struct got_repository *repo)
5505 const struct got_error *err;
5506 char *commit_ref_name;
5507 struct got_reference *commit_ref = NULL;
5508 struct got_object_id *commit_id = NULL;
5510 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5511 if (err)
5512 return err;
5514 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5515 if (err)
5516 goto done;
5517 err = got_ref_resolve(&commit_id, repo, commit_ref);
5518 if (err)
5519 goto done;
5520 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5521 err = got_error(GOT_ERR_REBASE_COMMITID);
5522 goto done;
5525 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5526 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5527 done:
5528 if (commit_ref)
5529 got_ref_close(commit_ref);
5530 free(commit_ref_name);
5531 free(commit_id);
5532 return err;
5535 const struct got_error *
5536 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5537 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5538 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5539 struct got_commit_object *orig_commit,
5540 struct got_object_id *orig_commit_id, const char *new_logmsg,
5541 struct got_repository *repo)
5543 const struct got_error *err;
5544 char *commit_ref_name;
5545 struct got_reference *commit_ref = NULL;
5547 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5548 if (err)
5549 return err;
5551 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5552 if (err)
5553 goto done;
5555 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5556 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5557 done:
5558 if (commit_ref)
5559 got_ref_close(commit_ref);
5560 free(commit_ref_name);
5561 return err;
5564 const struct got_error *
5565 got_worktree_rebase_postpone(struct got_worktree *worktree,
5566 struct got_fileindex *fileindex)
5568 if (fileindex)
5569 got_fileindex_free(fileindex);
5570 return lock_worktree(worktree, LOCK_SH);
5573 static const struct got_error *
5574 delete_ref(const char *name, struct got_repository *repo)
5576 const struct got_error *err;
5577 struct got_reference *ref;
5579 err = got_ref_open(&ref, repo, name, 0);
5580 if (err) {
5581 if (err->code == GOT_ERR_NOT_REF)
5582 return NULL;
5583 return err;
5586 err = got_ref_delete(ref, repo);
5587 got_ref_close(ref);
5588 return err;
5591 static const struct got_error *
5592 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5594 const struct got_error *err;
5595 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5596 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5598 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5599 if (err)
5600 goto done;
5601 err = delete_ref(tmp_branch_name, repo);
5602 if (err)
5603 goto done;
5605 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5606 if (err)
5607 goto done;
5608 err = delete_ref(new_base_branch_ref_name, repo);
5609 if (err)
5610 goto done;
5612 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5613 if (err)
5614 goto done;
5615 err = delete_ref(branch_ref_name, repo);
5616 if (err)
5617 goto done;
5619 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5620 if (err)
5621 goto done;
5622 err = delete_ref(commit_ref_name, repo);
5623 if (err)
5624 goto done;
5626 done:
5627 free(tmp_branch_name);
5628 free(new_base_branch_ref_name);
5629 free(branch_ref_name);
5630 free(commit_ref_name);
5631 return err;
5634 const struct got_error *
5635 got_worktree_rebase_complete(struct got_worktree *worktree,
5636 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5637 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5638 struct got_repository *repo)
5640 const struct got_error *err, *unlockerr;
5641 struct got_object_id *new_head_commit_id = NULL;
5643 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5644 if (err)
5645 return err;
5647 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5648 if (err)
5649 goto done;
5651 err = got_ref_write(rebased_branch, repo);
5652 if (err)
5653 goto done;
5655 err = got_worktree_set_head_ref(worktree, rebased_branch);
5656 if (err)
5657 goto done;
5659 err = delete_rebase_refs(worktree, repo);
5660 done:
5661 if (fileindex)
5662 got_fileindex_free(fileindex);
5663 free(new_head_commit_id);
5664 unlockerr = lock_worktree(worktree, LOCK_SH);
5665 if (unlockerr && err == NULL)
5666 err = unlockerr;
5667 return err;
5670 const struct got_error *
5671 got_worktree_rebase_abort(struct got_worktree *worktree,
5672 struct got_fileindex *fileindex, struct got_repository *repo,
5673 struct got_reference *new_base_branch,
5674 got_worktree_checkout_cb progress_cb, void *progress_arg)
5676 const struct got_error *err, *unlockerr, *sync_err;
5677 struct got_reference *resolved = NULL;
5678 struct got_object_id *commit_id = NULL;
5679 char *fileindex_path = NULL;
5680 struct revert_file_args rfa;
5681 struct got_object_id *tree_id = NULL;
5683 err = lock_worktree(worktree, LOCK_EX);
5684 if (err)
5685 return err;
5687 err = got_ref_open(&resolved, repo,
5688 got_ref_get_symref_target(new_base_branch), 0);
5689 if (err)
5690 goto done;
5692 err = got_worktree_set_head_ref(worktree, resolved);
5693 if (err)
5694 goto done;
5697 * XXX commits to the base branch could have happened while
5698 * we were busy rebasing; should we store the original commit ID
5699 * when rebase begins and read it back here?
5701 err = got_ref_resolve(&commit_id, repo, resolved);
5702 if (err)
5703 goto done;
5705 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5706 if (err)
5707 goto done;
5709 err = got_object_id_by_path(&tree_id, repo,
5710 worktree->base_commit_id, worktree->path_prefix);
5711 if (err)
5712 goto done;
5714 err = delete_rebase_refs(worktree, repo);
5715 if (err)
5716 goto done;
5718 err = get_fileindex_path(&fileindex_path, worktree);
5719 if (err)
5720 goto done;
5722 rfa.worktree = worktree;
5723 rfa.fileindex = fileindex;
5724 rfa.progress_cb = progress_cb;
5725 rfa.progress_arg = progress_arg;
5726 rfa.patch_cb = NULL;
5727 rfa.patch_arg = NULL;
5728 rfa.repo = repo;
5729 err = worktree_status(worktree, "", fileindex, repo,
5730 revert_file, &rfa, NULL, NULL, 0, 0);
5731 if (err)
5732 goto sync;
5734 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5735 repo, progress_cb, progress_arg, NULL, NULL);
5736 sync:
5737 sync_err = sync_fileindex(fileindex, fileindex_path);
5738 if (sync_err && err == NULL)
5739 err = sync_err;
5740 done:
5741 got_ref_close(resolved);
5742 free(tree_id);
5743 free(commit_id);
5744 if (fileindex)
5745 got_fileindex_free(fileindex);
5746 free(fileindex_path);
5748 unlockerr = lock_worktree(worktree, LOCK_SH);
5749 if (unlockerr && err == NULL)
5750 err = unlockerr;
5751 return err;
5754 const struct got_error *
5755 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5756 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5757 struct got_fileindex **fileindex, struct got_worktree *worktree,
5758 struct got_repository *repo)
5760 const struct got_error *err = NULL;
5761 char *tmp_branch_name = NULL;
5762 char *branch_ref_name = NULL;
5763 char *base_commit_ref_name = NULL;
5764 char *fileindex_path = NULL;
5765 struct check_rebase_ok_arg ok_arg;
5766 struct got_reference *wt_branch = NULL;
5767 struct got_reference *base_commit_ref = NULL;
5769 *tmp_branch = NULL;
5770 *branch_ref = NULL;
5771 *base_commit_id = NULL;
5772 *fileindex = NULL;
5774 err = lock_worktree(worktree, LOCK_EX);
5775 if (err)
5776 return err;
5778 err = open_fileindex(fileindex, &fileindex_path, worktree);
5779 if (err)
5780 goto done;
5782 ok_arg.worktree = worktree;
5783 ok_arg.repo = repo;
5784 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5785 &ok_arg);
5786 if (err)
5787 goto done;
5789 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5790 if (err)
5791 goto done;
5793 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5794 if (err)
5795 goto done;
5797 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5798 worktree);
5799 if (err)
5800 goto done;
5802 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5803 0);
5804 if (err)
5805 goto done;
5807 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5808 if (err)
5809 goto done;
5811 err = got_ref_write(*branch_ref, repo);
5812 if (err)
5813 goto done;
5815 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5816 worktree->base_commit_id);
5817 if (err)
5818 goto done;
5819 err = got_ref_write(base_commit_ref, repo);
5820 if (err)
5821 goto done;
5822 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5823 if (*base_commit_id == NULL) {
5824 err = got_error_from_errno("got_object_id_dup");
5825 goto done;
5828 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5829 worktree->base_commit_id);
5830 if (err)
5831 goto done;
5832 err = got_ref_write(*tmp_branch, repo);
5833 if (err)
5834 goto done;
5836 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5837 if (err)
5838 goto done;
5839 done:
5840 free(fileindex_path);
5841 free(tmp_branch_name);
5842 free(branch_ref_name);
5843 free(base_commit_ref_name);
5844 if (wt_branch)
5845 got_ref_close(wt_branch);
5846 if (err) {
5847 if (*branch_ref) {
5848 got_ref_close(*branch_ref);
5849 *branch_ref = NULL;
5851 if (*tmp_branch) {
5852 got_ref_close(*tmp_branch);
5853 *tmp_branch = NULL;
5855 free(*base_commit_id);
5856 if (*fileindex) {
5857 got_fileindex_free(*fileindex);
5858 *fileindex = NULL;
5860 lock_worktree(worktree, LOCK_SH);
5862 return err;
5865 const struct got_error *
5866 got_worktree_histedit_postpone(struct got_worktree *worktree,
5867 struct got_fileindex *fileindex)
5869 if (fileindex)
5870 got_fileindex_free(fileindex);
5871 return lock_worktree(worktree, LOCK_SH);
5874 const struct got_error *
5875 got_worktree_histedit_in_progress(int *in_progress,
5876 struct got_worktree *worktree)
5878 const struct got_error *err;
5879 char *tmp_branch_name = NULL;
5881 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5882 if (err)
5883 return err;
5885 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5886 free(tmp_branch_name);
5887 return NULL;
5890 const struct got_error *
5891 got_worktree_histedit_continue(struct got_object_id **commit_id,
5892 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5893 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5894 struct got_worktree *worktree, struct got_repository *repo)
5896 const struct got_error *err;
5897 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5898 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5899 struct got_reference *commit_ref = NULL;
5900 struct got_reference *base_commit_ref = NULL;
5901 char *fileindex_path = NULL;
5902 int have_staged_files = 0;
5904 *commit_id = NULL;
5905 *tmp_branch = NULL;
5906 *base_commit_id = NULL;
5907 *fileindex = NULL;
5909 err = lock_worktree(worktree, LOCK_EX);
5910 if (err)
5911 return err;
5913 err = open_fileindex(fileindex, &fileindex_path, worktree);
5914 if (err)
5915 goto done;
5917 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5918 &have_staged_files);
5919 if (err && err->code != GOT_ERR_CANCELLED)
5920 goto done;
5921 if (have_staged_files) {
5922 err = got_error(GOT_ERR_STAGED_PATHS);
5923 goto done;
5926 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5927 if (err)
5928 goto done;
5930 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5931 if (err)
5932 goto done;
5934 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5935 if (err)
5936 goto done;
5938 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5939 worktree);
5940 if (err)
5941 goto done;
5943 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5944 if (err)
5945 goto done;
5947 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5948 if (err)
5949 goto done;
5950 err = got_ref_resolve(commit_id, repo, commit_ref);
5951 if (err)
5952 goto done;
5954 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5955 if (err)
5956 goto done;
5957 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5958 if (err)
5959 goto done;
5961 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5962 if (err)
5963 goto done;
5964 done:
5965 free(commit_ref_name);
5966 free(branch_ref_name);
5967 free(fileindex_path);
5968 if (commit_ref)
5969 got_ref_close(commit_ref);
5970 if (base_commit_ref)
5971 got_ref_close(base_commit_ref);
5972 if (err) {
5973 free(*commit_id);
5974 *commit_id = NULL;
5975 free(*base_commit_id);
5976 *base_commit_id = NULL;
5977 if (*tmp_branch) {
5978 got_ref_close(*tmp_branch);
5979 *tmp_branch = NULL;
5981 if (*fileindex) {
5982 got_fileindex_free(*fileindex);
5983 *fileindex = NULL;
5985 lock_worktree(worktree, LOCK_EX);
5987 return err;
5990 static const struct got_error *
5991 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5993 const struct got_error *err;
5994 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5995 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5997 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5998 if (err)
5999 goto done;
6000 err = delete_ref(tmp_branch_name, repo);
6001 if (err)
6002 goto done;
6004 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6005 worktree);
6006 if (err)
6007 goto done;
6008 err = delete_ref(base_commit_ref_name, repo);
6009 if (err)
6010 goto done;
6012 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6013 if (err)
6014 goto done;
6015 err = delete_ref(branch_ref_name, repo);
6016 if (err)
6017 goto done;
6019 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6020 if (err)
6021 goto done;
6022 err = delete_ref(commit_ref_name, repo);
6023 if (err)
6024 goto done;
6025 done:
6026 free(tmp_branch_name);
6027 free(base_commit_ref_name);
6028 free(branch_ref_name);
6029 free(commit_ref_name);
6030 return err;
6033 const struct got_error *
6034 got_worktree_histedit_abort(struct got_worktree *worktree,
6035 struct got_fileindex *fileindex, struct got_repository *repo,
6036 struct got_reference *branch, struct got_object_id *base_commit_id,
6037 got_worktree_checkout_cb progress_cb, void *progress_arg)
6039 const struct got_error *err, *unlockerr, *sync_err;
6040 struct got_reference *resolved = NULL;
6041 char *fileindex_path = NULL;
6042 struct got_object_id *tree_id = NULL;
6043 struct revert_file_args rfa;
6045 err = lock_worktree(worktree, LOCK_EX);
6046 if (err)
6047 return err;
6049 err = got_ref_open(&resolved, repo,
6050 got_ref_get_symref_target(branch), 0);
6051 if (err)
6052 goto done;
6054 err = got_worktree_set_head_ref(worktree, resolved);
6055 if (err)
6056 goto done;
6058 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6059 if (err)
6060 goto done;
6062 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6063 worktree->path_prefix);
6064 if (err)
6065 goto done;
6067 err = delete_histedit_refs(worktree, repo);
6068 if (err)
6069 goto done;
6071 err = get_fileindex_path(&fileindex_path, worktree);
6072 if (err)
6073 goto done;
6075 rfa.worktree = worktree;
6076 rfa.fileindex = fileindex;
6077 rfa.progress_cb = progress_cb;
6078 rfa.progress_arg = progress_arg;
6079 rfa.patch_cb = NULL;
6080 rfa.patch_arg = NULL;
6081 rfa.repo = repo;
6082 err = worktree_status(worktree, "", fileindex, repo,
6083 revert_file, &rfa, NULL, NULL, 0, 0);
6084 if (err)
6085 goto sync;
6087 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6088 repo, progress_cb, progress_arg, NULL, NULL);
6089 sync:
6090 sync_err = sync_fileindex(fileindex, fileindex_path);
6091 if (sync_err && err == NULL)
6092 err = sync_err;
6093 done:
6094 got_ref_close(resolved);
6095 free(tree_id);
6096 free(fileindex_path);
6098 unlockerr = lock_worktree(worktree, LOCK_SH);
6099 if (unlockerr && err == NULL)
6100 err = unlockerr;
6101 return err;
6104 const struct got_error *
6105 got_worktree_histedit_complete(struct got_worktree *worktree,
6106 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6107 struct got_reference *edited_branch, struct got_repository *repo)
6109 const struct got_error *err, *unlockerr;
6110 struct got_object_id *new_head_commit_id = NULL;
6111 struct got_reference *resolved = NULL;
6113 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6114 if (err)
6115 return err;
6117 err = got_ref_open(&resolved, repo,
6118 got_ref_get_symref_target(edited_branch), 0);
6119 if (err)
6120 goto done;
6122 err = got_ref_change_ref(resolved, new_head_commit_id);
6123 if (err)
6124 goto done;
6126 err = got_ref_write(resolved, repo);
6127 if (err)
6128 goto done;
6130 err = got_worktree_set_head_ref(worktree, resolved);
6131 if (err)
6132 goto done;
6134 err = delete_histedit_refs(worktree, repo);
6135 done:
6136 if (fileindex)
6137 got_fileindex_free(fileindex);
6138 free(new_head_commit_id);
6139 unlockerr = lock_worktree(worktree, LOCK_SH);
6140 if (unlockerr && err == NULL)
6141 err = unlockerr;
6142 return err;
6145 const struct got_error *
6146 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6147 struct got_object_id *commit_id, struct got_repository *repo)
6149 const struct got_error *err;
6150 char *commit_ref_name;
6152 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6153 if (err)
6154 return err;
6156 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6157 if (err)
6158 goto done;
6160 err = delete_ref(commit_ref_name, repo);
6161 done:
6162 free(commit_ref_name);
6163 return err;
6166 const struct got_error *
6167 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6168 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6169 struct got_worktree *worktree, const char *refname,
6170 struct got_repository *repo)
6172 const struct got_error *err = NULL;
6173 char *fileindex_path = NULL;
6174 struct check_rebase_ok_arg ok_arg;
6176 *fileindex = NULL;
6177 *branch_ref = NULL;
6178 *base_branch_ref = NULL;
6180 err = lock_worktree(worktree, LOCK_EX);
6181 if (err)
6182 return err;
6184 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6185 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6186 "cannot integrate a branch into itself; "
6187 "update -b or different branch name required");
6188 goto done;
6191 err = open_fileindex(fileindex, &fileindex_path, worktree);
6192 if (err)
6193 goto done;
6195 /* Preconditions are the same as for rebase. */
6196 ok_arg.worktree = worktree;
6197 ok_arg.repo = repo;
6198 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6199 &ok_arg);
6200 if (err)
6201 goto done;
6203 err = got_ref_open(branch_ref, repo, refname, 1);
6204 if (err)
6205 goto done;
6207 err = got_ref_open(base_branch_ref, repo,
6208 got_worktree_get_head_ref_name(worktree), 1);
6209 done:
6210 if (err) {
6211 if (*branch_ref) {
6212 got_ref_close(*branch_ref);
6213 *branch_ref = NULL;
6215 if (*base_branch_ref) {
6216 got_ref_close(*base_branch_ref);
6217 *base_branch_ref = NULL;
6219 if (*fileindex) {
6220 got_fileindex_free(*fileindex);
6221 *fileindex = NULL;
6223 lock_worktree(worktree, LOCK_SH);
6225 return err;
6228 const struct got_error *
6229 got_worktree_integrate_continue(struct got_worktree *worktree,
6230 struct got_fileindex *fileindex, struct got_repository *repo,
6231 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6232 got_worktree_checkout_cb progress_cb, void *progress_arg,
6233 got_cancel_cb cancel_cb, void *cancel_arg)
6235 const struct got_error *err = NULL, *sync_err, *unlockerr;
6236 char *fileindex_path = NULL;
6237 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6239 err = get_fileindex_path(&fileindex_path, worktree);
6240 if (err)
6241 goto done;
6243 err = got_ref_resolve(&commit_id, repo, branch_ref);
6244 if (err)
6245 goto done;
6247 err = got_object_id_by_path(&tree_id, repo, commit_id,
6248 worktree->path_prefix);
6249 if (err)
6250 goto done;
6252 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6253 if (err)
6254 goto done;
6256 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6257 progress_cb, progress_arg, cancel_cb, cancel_arg);
6258 if (err)
6259 goto sync;
6261 err = got_ref_change_ref(base_branch_ref, commit_id);
6262 if (err)
6263 goto sync;
6265 err = got_ref_write(base_branch_ref, repo);
6266 sync:
6267 sync_err = sync_fileindex(fileindex, fileindex_path);
6268 if (sync_err && err == NULL)
6269 err = sync_err;
6271 done:
6272 unlockerr = got_ref_unlock(branch_ref);
6273 if (unlockerr && err == NULL)
6274 err = unlockerr;
6275 got_ref_close(branch_ref);
6277 unlockerr = got_ref_unlock(base_branch_ref);
6278 if (unlockerr && err == NULL)
6279 err = unlockerr;
6280 got_ref_close(base_branch_ref);
6282 got_fileindex_free(fileindex);
6283 free(fileindex_path);
6284 free(tree_id);
6286 unlockerr = lock_worktree(worktree, LOCK_SH);
6287 if (unlockerr && err == NULL)
6288 err = unlockerr;
6289 return err;
6292 const struct got_error *
6293 got_worktree_integrate_abort(struct got_worktree *worktree,
6294 struct got_fileindex *fileindex, struct got_repository *repo,
6295 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6297 const struct got_error *err = NULL, *unlockerr = NULL;
6299 got_fileindex_free(fileindex);
6301 err = lock_worktree(worktree, LOCK_SH);
6303 unlockerr = got_ref_unlock(branch_ref);
6304 if (unlockerr && err == NULL)
6305 err = unlockerr;
6306 got_ref_close(branch_ref);
6308 unlockerr = got_ref_unlock(base_branch_ref);
6309 if (unlockerr && err == NULL)
6310 err = unlockerr;
6311 got_ref_close(base_branch_ref);
6313 return err;
6316 struct check_stage_ok_arg {
6317 struct got_object_id *head_commit_id;
6318 struct got_worktree *worktree;
6319 struct got_fileindex *fileindex;
6320 struct got_repository *repo;
6321 int have_changes;
6324 const struct got_error *
6325 check_stage_ok(void *arg, unsigned char status,
6326 unsigned char staged_status, const char *relpath,
6327 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6328 struct got_object_id *commit_id, int dirfd, const char *de_name)
6330 struct check_stage_ok_arg *a = arg;
6331 const struct got_error *err = NULL;
6332 struct got_fileindex_entry *ie;
6333 struct got_object_id base_commit_id;
6334 struct got_object_id *base_commit_idp = NULL;
6335 char *in_repo_path = NULL, *p;
6337 if (status == GOT_STATUS_UNVERSIONED ||
6338 status == GOT_STATUS_NO_CHANGE)
6339 return NULL;
6340 if (status == GOT_STATUS_NONEXISTENT)
6341 return got_error_set_errno(ENOENT, relpath);
6343 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6344 if (ie == NULL)
6345 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6347 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6348 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6349 relpath) == -1)
6350 return got_error_from_errno("asprintf");
6352 if (got_fileindex_entry_has_commit(ie)) {
6353 memcpy(base_commit_id.sha1, ie->commit_sha1,
6354 SHA1_DIGEST_LENGTH);
6355 base_commit_idp = &base_commit_id;
6358 if (status == GOT_STATUS_CONFLICT) {
6359 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6360 goto done;
6361 } else if (status != GOT_STATUS_ADD &&
6362 status != GOT_STATUS_MODIFY &&
6363 status != GOT_STATUS_DELETE) {
6364 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6365 goto done;
6368 a->have_changes = 1;
6370 p = in_repo_path;
6371 while (p[0] == '/')
6372 p++;
6373 err = check_out_of_date(p, status, staged_status,
6374 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6375 GOT_ERR_STAGE_OUT_OF_DATE);
6376 done:
6377 free(in_repo_path);
6378 return err;
6381 struct stage_path_arg {
6382 struct got_worktree *worktree;
6383 struct got_fileindex *fileindex;
6384 struct got_repository *repo;
6385 got_worktree_status_cb status_cb;
6386 void *status_arg;
6387 got_worktree_patch_cb patch_cb;
6388 void *patch_arg;
6389 int staged_something;
6392 static const struct got_error *
6393 stage_path(void *arg, unsigned char status,
6394 unsigned char staged_status, const char *relpath,
6395 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6396 struct got_object_id *commit_id, int dirfd, const char *de_name)
6398 struct stage_path_arg *a = arg;
6399 const struct got_error *err = NULL;
6400 struct got_fileindex_entry *ie;
6401 char *ondisk_path = NULL, *path_content = NULL;
6402 uint32_t stage;
6403 struct got_object_id *new_staged_blob_id = NULL;
6405 if (status == GOT_STATUS_UNVERSIONED)
6406 return NULL;
6408 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6409 if (ie == NULL)
6410 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6412 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6413 relpath)== -1)
6414 return got_error_from_errno("asprintf");
6416 switch (status) {
6417 case GOT_STATUS_ADD:
6418 case GOT_STATUS_MODIFY:
6419 if (a->patch_cb) {
6420 if (status == GOT_STATUS_ADD) {
6421 int choice = GOT_PATCH_CHOICE_NONE;
6422 err = (*a->patch_cb)(&choice, a->patch_arg,
6423 status, ie->path, NULL, 1, 1);
6424 if (err)
6425 break;
6426 if (choice != GOT_PATCH_CHOICE_YES)
6427 break;
6428 } else {
6429 err = create_patched_content(&path_content, 0,
6430 staged_blob_id ? staged_blob_id : blob_id,
6431 ondisk_path, dirfd, de_name, ie->path,
6432 a->repo, a->patch_cb, a->patch_arg);
6433 if (err || path_content == NULL)
6434 break;
6437 err = got_object_blob_create(&new_staged_blob_id,
6438 path_content ? path_content : ondisk_path, a->repo);
6439 if (err)
6440 break;
6441 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6442 SHA1_DIGEST_LENGTH);
6443 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6444 stage = GOT_FILEIDX_STAGE_ADD;
6445 else
6446 stage = GOT_FILEIDX_STAGE_MODIFY;
6447 got_fileindex_entry_stage_set(ie, stage);
6448 a->staged_something = 1;
6449 if (a->status_cb == NULL)
6450 break;
6451 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6452 get_staged_status(ie), relpath, blob_id,
6453 new_staged_blob_id, NULL, dirfd, de_name);
6454 break;
6455 case GOT_STATUS_DELETE:
6456 if (staged_status == GOT_STATUS_DELETE)
6457 break;
6458 if (a->patch_cb) {
6459 int choice = GOT_PATCH_CHOICE_NONE;
6460 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6461 ie->path, NULL, 1, 1);
6462 if (err)
6463 break;
6464 if (choice == GOT_PATCH_CHOICE_NO)
6465 break;
6466 if (choice != GOT_PATCH_CHOICE_YES) {
6467 err = got_error(GOT_ERR_PATCH_CHOICE);
6468 break;
6471 stage = GOT_FILEIDX_STAGE_DELETE;
6472 got_fileindex_entry_stage_set(ie, stage);
6473 a->staged_something = 1;
6474 if (a->status_cb == NULL)
6475 break;
6476 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6477 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6478 de_name);
6479 break;
6480 case GOT_STATUS_NO_CHANGE:
6481 break;
6482 case GOT_STATUS_CONFLICT:
6483 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6484 break;
6485 case GOT_STATUS_NONEXISTENT:
6486 err = got_error_set_errno(ENOENT, relpath);
6487 break;
6488 default:
6489 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6490 break;
6493 if (path_content && unlink(path_content) == -1 && err == NULL)
6494 err = got_error_from_errno2("unlink", path_content);
6495 free(path_content);
6496 free(ondisk_path);
6497 free(new_staged_blob_id);
6498 return err;
6501 const struct got_error *
6502 got_worktree_stage(struct got_worktree *worktree,
6503 struct got_pathlist_head *paths,
6504 got_worktree_status_cb status_cb, void *status_arg,
6505 got_worktree_patch_cb patch_cb, void *patch_arg,
6506 struct got_repository *repo)
6508 const struct got_error *err = NULL, *sync_err, *unlockerr;
6509 struct got_pathlist_entry *pe;
6510 struct got_fileindex *fileindex = NULL;
6511 char *fileindex_path = NULL;
6512 struct got_reference *head_ref = NULL;
6513 struct got_object_id *head_commit_id = NULL;
6514 struct check_stage_ok_arg oka;
6515 struct stage_path_arg spa;
6517 err = lock_worktree(worktree, LOCK_EX);
6518 if (err)
6519 return err;
6521 err = got_ref_open(&head_ref, repo,
6522 got_worktree_get_head_ref_name(worktree), 0);
6523 if (err)
6524 goto done;
6525 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6526 if (err)
6527 goto done;
6528 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6529 if (err)
6530 goto done;
6532 /* Check pre-conditions before staging anything. */
6533 oka.head_commit_id = head_commit_id;
6534 oka.worktree = worktree;
6535 oka.fileindex = fileindex;
6536 oka.repo = repo;
6537 oka.have_changes = 0;
6538 TAILQ_FOREACH(pe, paths, entry) {
6539 err = worktree_status(worktree, pe->path, fileindex, repo,
6540 check_stage_ok, &oka, NULL, NULL, 0, 0);
6541 if (err)
6542 goto done;
6544 if (!oka.have_changes) {
6545 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6546 goto done;
6549 spa.worktree = worktree;
6550 spa.fileindex = fileindex;
6551 spa.repo = repo;
6552 spa.patch_cb = patch_cb;
6553 spa.patch_arg = patch_arg;
6554 spa.status_cb = status_cb;
6555 spa.status_arg = status_arg;
6556 spa.staged_something = 0;
6557 TAILQ_FOREACH(pe, paths, entry) {
6558 err = worktree_status(worktree, pe->path, fileindex, repo,
6559 stage_path, &spa, NULL, NULL, 0, 0);
6560 if (err)
6561 goto done;
6563 if (!spa.staged_something) {
6564 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6565 goto done;
6568 sync_err = sync_fileindex(fileindex, fileindex_path);
6569 if (sync_err && err == NULL)
6570 err = sync_err;
6571 done:
6572 if (head_ref)
6573 got_ref_close(head_ref);
6574 free(head_commit_id);
6575 free(fileindex_path);
6576 if (fileindex)
6577 got_fileindex_free(fileindex);
6578 unlockerr = lock_worktree(worktree, LOCK_SH);
6579 if (unlockerr && err == NULL)
6580 err = unlockerr;
6581 return err;
6584 struct unstage_path_arg {
6585 struct got_worktree *worktree;
6586 struct got_fileindex *fileindex;
6587 struct got_repository *repo;
6588 got_worktree_checkout_cb progress_cb;
6589 void *progress_arg;
6590 got_worktree_patch_cb patch_cb;
6591 void *patch_arg;
6594 static const struct got_error *
6595 create_unstaged_content(char **path_unstaged_content,
6596 char **path_new_staged_content, struct got_object_id *blob_id,
6597 struct got_object_id *staged_blob_id, const char *relpath,
6598 struct got_repository *repo,
6599 got_worktree_patch_cb patch_cb, void *patch_arg)
6601 const struct got_error *err;
6602 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6603 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6604 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6605 struct stat sb1, sb2;
6606 struct got_diff_changes *changes = NULL;
6607 struct got_diff_state *ds = NULL;
6608 struct got_diff_args *args = NULL;
6609 struct got_diff_change *change;
6610 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6611 int have_content = 0, have_rejected_content = 0;
6613 *path_unstaged_content = NULL;
6614 *path_new_staged_content = NULL;
6616 err = got_object_id_str(&label1, blob_id);
6617 if (err)
6618 return err;
6619 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6620 if (err)
6621 goto done;
6623 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6624 if (err)
6625 goto done;
6627 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6628 if (err)
6629 goto done;
6631 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6632 if (err)
6633 goto done;
6635 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6636 if (err)
6637 goto done;
6639 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6640 if (err)
6641 goto done;
6643 if (stat(path1, &sb1) == -1) {
6644 err = got_error_from_errno2("stat", path1);
6645 goto done;
6648 if (stat(path2, &sb2) == -1) {
6649 err = got_error_from_errno2("stat", path2);
6650 goto done;
6653 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6654 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6655 if (err)
6656 goto done;
6658 err = got_opentemp_named(path_unstaged_content, &outfile,
6659 "got-unstaged-content");
6660 if (err)
6661 goto done;
6662 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6663 "got-new-staged-content");
6664 if (err)
6665 goto done;
6667 if (fseek(f1, 0L, SEEK_SET) == -1) {
6668 err = got_ferror(f1, GOT_ERR_IO);
6669 goto done;
6671 if (fseek(f2, 0L, SEEK_SET) == -1) {
6672 err = got_ferror(f2, GOT_ERR_IO);
6673 goto done;
6675 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6676 int choice;
6677 err = apply_or_reject_change(&choice, change, ++n,
6678 changes->nchanges, ds, args, diff_flags, relpath,
6679 f1, f2, &line_cur1, &line_cur2,
6680 outfile, rejectfile, patch_cb, patch_arg);
6681 if (err)
6682 goto done;
6683 if (choice == GOT_PATCH_CHOICE_YES)
6684 have_content = 1;
6685 else
6686 have_rejected_content = 1;
6687 if (choice == GOT_PATCH_CHOICE_QUIT)
6688 break;
6690 if (have_content || have_rejected_content)
6691 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6692 outfile, rejectfile);
6693 done:
6694 free(label1);
6695 if (blob)
6696 got_object_blob_close(blob);
6697 if (staged_blob)
6698 got_object_blob_close(staged_blob);
6699 if (f1 && fclose(f1) == EOF && err == NULL)
6700 err = got_error_from_errno2("fclose", path1);
6701 if (f2 && fclose(f2) == EOF && err == NULL)
6702 err = got_error_from_errno2("fclose", path2);
6703 if (outfile && fclose(outfile) == EOF && err == NULL)
6704 err = got_error_from_errno2("fclose", *path_unstaged_content);
6705 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6706 err = got_error_from_errno2("fclose", *path_new_staged_content);
6707 if (path1 && unlink(path1) == -1 && err == NULL)
6708 err = got_error_from_errno2("unlink", path1);
6709 if (path2 && unlink(path2) == -1 && err == NULL)
6710 err = got_error_from_errno2("unlink", path2);
6711 if (err || !have_content) {
6712 if (*path_unstaged_content &&
6713 unlink(*path_unstaged_content) == -1 && err == NULL)
6714 err = got_error_from_errno2("unlink",
6715 *path_unstaged_content);
6716 free(*path_unstaged_content);
6717 *path_unstaged_content = NULL;
6719 if (err || !have_rejected_content) {
6720 if (*path_new_staged_content &&
6721 unlink(*path_new_staged_content) == -1 && err == NULL)
6722 err = got_error_from_errno2("unlink",
6723 *path_new_staged_content);
6724 free(*path_new_staged_content);
6725 *path_new_staged_content = NULL;
6727 free(args);
6728 if (ds) {
6729 got_diff_state_free(ds);
6730 free(ds);
6732 if (changes)
6733 got_diff_free_changes(changes);
6734 free(path1);
6735 free(path2);
6736 return err;
6739 static const struct got_error *
6740 unstage_path(void *arg, unsigned char status,
6741 unsigned char staged_status, const char *relpath,
6742 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6743 struct got_object_id *commit_id, int dirfd, const char *de_name)
6745 const struct got_error *err = NULL;
6746 struct unstage_path_arg *a = arg;
6747 struct got_fileindex_entry *ie;
6748 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6749 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6750 char *path_new_staged_content = NULL;
6751 char *id_str = NULL, *label_orig = NULL;
6752 int local_changes_subsumed;
6753 struct stat sb;
6755 if (staged_status != GOT_STATUS_ADD &&
6756 staged_status != GOT_STATUS_MODIFY &&
6757 staged_status != GOT_STATUS_DELETE)
6758 return NULL;
6760 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6761 if (ie == NULL)
6762 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6764 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6765 == -1)
6766 return got_error_from_errno("asprintf");
6768 err = got_object_id_str(&id_str,
6769 commit_id ? commit_id : a->worktree->base_commit_id);
6770 if (err)
6771 goto done;
6772 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6773 id_str) == -1) {
6774 err = got_error_from_errno("asprintf");
6775 goto done;
6778 switch (staged_status) {
6779 case GOT_STATUS_MODIFY:
6780 err = got_object_open_as_blob(&blob_base, a->repo,
6781 blob_id, 8192);
6782 if (err)
6783 break;
6784 /* fall through */
6785 case GOT_STATUS_ADD:
6786 if (a->patch_cb) {
6787 if (staged_status == GOT_STATUS_ADD) {
6788 int choice = GOT_PATCH_CHOICE_NONE;
6789 err = (*a->patch_cb)(&choice, a->patch_arg,
6790 staged_status, ie->path, NULL, 1, 1);
6791 if (err)
6792 break;
6793 if (choice != GOT_PATCH_CHOICE_YES)
6794 break;
6795 } else {
6796 err = create_unstaged_content(
6797 &path_unstaged_content,
6798 &path_new_staged_content, blob_id,
6799 staged_blob_id, ie->path, a->repo,
6800 a->patch_cb, a->patch_arg);
6801 if (err || path_unstaged_content == NULL)
6802 break;
6803 if (path_new_staged_content) {
6804 err = got_object_blob_create(
6805 &staged_blob_id,
6806 path_new_staged_content,
6807 a->repo);
6808 if (err)
6809 break;
6810 memcpy(ie->staged_blob_sha1,
6811 staged_blob_id->sha1,
6812 SHA1_DIGEST_LENGTH);
6814 err = merge_file(&local_changes_subsumed,
6815 a->worktree, blob_base, ondisk_path,
6816 relpath, got_fileindex_perms_to_st(ie),
6817 path_unstaged_content, label_orig,
6818 "unstaged", a->repo, a->progress_cb,
6819 a->progress_arg);
6820 if (err == NULL &&
6821 path_new_staged_content == NULL)
6822 got_fileindex_entry_stage_set(ie,
6823 GOT_FILEIDX_STAGE_NONE);
6824 break; /* Done with this file. */
6827 err = got_object_open_as_blob(&blob_staged, a->repo,
6828 staged_blob_id, 8192);
6829 if (err)
6830 break;
6831 err = merge_blob(&local_changes_subsumed, a->worktree,
6832 blob_base, ondisk_path, relpath,
6833 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6834 commit_id ? commit_id : a->worktree->base_commit_id,
6835 a->repo, a->progress_cb, a->progress_arg);
6836 if (err == NULL)
6837 got_fileindex_entry_stage_set(ie,
6838 GOT_FILEIDX_STAGE_NONE);
6839 break;
6840 case GOT_STATUS_DELETE:
6841 if (a->patch_cb) {
6842 int choice = GOT_PATCH_CHOICE_NONE;
6843 err = (*a->patch_cb)(&choice, a->patch_arg,
6844 staged_status, ie->path, NULL, 1, 1);
6845 if (err)
6846 break;
6847 if (choice == GOT_PATCH_CHOICE_NO)
6848 break;
6849 if (choice != GOT_PATCH_CHOICE_YES) {
6850 err = got_error(GOT_ERR_PATCH_CHOICE);
6851 break;
6854 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6855 err = get_file_status(&status, &sb, ie, ondisk_path,
6856 dirfd, de_name, a->repo);
6857 if (err)
6858 break;
6859 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6860 break;
6862 done:
6863 free(ondisk_path);
6864 if (path_unstaged_content &&
6865 unlink(path_unstaged_content) == -1 && err == NULL)
6866 err = got_error_from_errno2("unlink", path_unstaged_content);
6867 if (path_new_staged_content &&
6868 unlink(path_new_staged_content) == -1 && err == NULL)
6869 err = got_error_from_errno2("unlink", path_new_staged_content);
6870 free(path_unstaged_content);
6871 free(path_new_staged_content);
6872 if (blob_base)
6873 got_object_blob_close(blob_base);
6874 if (blob_staged)
6875 got_object_blob_close(blob_staged);
6876 free(id_str);
6877 free(label_orig);
6878 return err;
6881 const struct got_error *
6882 got_worktree_unstage(struct got_worktree *worktree,
6883 struct got_pathlist_head *paths,
6884 got_worktree_checkout_cb progress_cb, void *progress_arg,
6885 got_worktree_patch_cb patch_cb, void *patch_arg,
6886 struct got_repository *repo)
6888 const struct got_error *err = NULL, *sync_err, *unlockerr;
6889 struct got_pathlist_entry *pe;
6890 struct got_fileindex *fileindex = NULL;
6891 char *fileindex_path = NULL;
6892 struct unstage_path_arg upa;
6894 err = lock_worktree(worktree, LOCK_EX);
6895 if (err)
6896 return err;
6898 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6899 if (err)
6900 goto done;
6902 upa.worktree = worktree;
6903 upa.fileindex = fileindex;
6904 upa.repo = repo;
6905 upa.progress_cb = progress_cb;
6906 upa.progress_arg = progress_arg;
6907 upa.patch_cb = patch_cb;
6908 upa.patch_arg = patch_arg;
6909 TAILQ_FOREACH(pe, paths, entry) {
6910 err = worktree_status(worktree, pe->path, fileindex, repo,
6911 unstage_path, &upa, NULL, NULL, 0, 0);
6912 if (err)
6913 goto done;
6916 sync_err = sync_fileindex(fileindex, fileindex_path);
6917 if (sync_err && err == NULL)
6918 err = sync_err;
6919 done:
6920 free(fileindex_path);
6921 if (fileindex)
6922 got_fileindex_free(fileindex);
6923 unlockerr = lock_worktree(worktree, LOCK_SH);
6924 if (unlockerr && err == NULL)
6925 err = unlockerr;
6926 return err;