Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
745 char *symlink_path = NULL;
746 FILE *symlinkf = NULL;
748 *local_changes_subsumed = 0;
750 parent = dirname(ondisk_path);
751 if (parent == NULL)
752 return got_error_from_errno2("dirname", ondisk_path);
754 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 return got_error_from_errno("asprintf");
757 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
758 if (err)
759 goto done;
761 free(base_path);
762 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 err = got_error_from_errno("asprintf");
764 base_path = NULL;
765 goto done;
768 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
769 if (err)
770 goto done;
771 if (blob_orig) {
772 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
773 blob_orig);
774 if (err)
775 goto done;
776 } else {
777 /*
778 * If the file has no blob, this is an "add vs add" conflict,
779 * and we simply use an empty ancestor file to make both files
780 * appear in the merged result in their entirety.
781 */
784 /*
785 * In order the run a 3-way merge with a symlink we copy the symlink's
786 * target path into a temporary file and use that file with diff3.
787 */
788 if (S_ISLNK(st_mode)) {
789 char target_path[PATH_MAX];
790 ssize_t target_len;
791 size_t n;
793 free(base_path);
794 if (asprintf(&base_path, "%s/got-symlink-merge",
795 parent) == -1) {
796 err = got_error_from_errno("asprintf");
797 base_path = NULL;
798 goto done;
800 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
801 if (err)
802 goto done;
803 target_len = readlink(ondisk_path, target_path,
804 sizeof(target_path));
805 if (target_len == -1) {
806 err = got_error_from_errno2("readlink", ondisk_path);
807 goto done;
809 n = fwrite(target_path, 1, target_len, symlinkf);
810 if (n != target_len) {
811 err = got_ferror(symlinkf, GOT_ERR_IO);
812 goto done;
814 if (fflush(symlinkf) == EOF) {
815 err = got_error_from_errno2("fflush", symlink_path);
816 goto done;
820 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 label_deriv, label_orig, NULL);
823 if (err)
824 goto done;
826 err = (*progress_cb)(progress_arg,
827 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
828 if (err)
829 goto done;
831 if (fsync(merged_fd) != 0) {
832 err = got_error_from_errno("fsync");
833 goto done;
836 /* Check if a clean merge has subsumed all local changes. */
837 if (overlapcnt == 0) {
838 err = check_files_equal(local_changes_subsumed, deriv_path,
839 merged_path);
840 if (err)
841 goto done;
844 if (fchmod(merged_fd, st_mode) != 0) {
845 err = got_error_from_errno2("fchmod", merged_path);
846 goto done;
849 if (rename(merged_path, ondisk_path) != 0) {
850 err = got_error_from_errno3("rename", merged_path,
851 ondisk_path);
852 goto done;
854 done:
855 if (err) {
856 if (merged_path)
857 unlink(merged_path);
859 if (symlink_path) {
860 if (unlink(symlink_path) == -1 && err == NULL)
861 err = got_error_from_errno2("unlink", symlink_path);
863 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 err = got_error_from_errno2("fclose", symlink_path);
865 free(symlink_path);
866 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 err = got_error_from_errno("close");
868 if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 err = got_error_from_errno("fclose");
870 free(merged_path);
871 free(base_path);
872 if (blob_orig_path) {
873 unlink(blob_orig_path);
874 free(blob_orig_path);
876 return err;
879 static const struct got_error *
880 update_symlink(const char *ondisk_path, const char *target_path,
881 size_t target_len)
883 /* This is not atomic but matches what 'ln -sf' does. */
884 if (unlink(ondisk_path) == -1)
885 return got_error_from_errno2("unlink", ondisk_path);
886 if (symlink(target_path, ondisk_path) == -1)
887 return got_error_from_errno3("symlink", target_path,
888 ondisk_path);
889 return NULL;
892 /*
893 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 * in the work tree with a file that contains conflict markers and the
895 * conflicting target paths of the original version, a "derived version"
896 * of a symlink from an incoming change, and a local version of the symlink.
898 * The original versions's target path can be NULL if it is not available,
899 * such as if both derived versions added a new symlink at the same path.
901 * The incoming derived symlink target is NULL in case the incoming change
902 * has deleted this symlink.
903 */
904 static const struct got_error *
905 install_symlink_conflict(const char *deriv_target,
906 struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 const char *label_orig, const char *local_target, const char *ondisk_path)
909 const struct got_error *err;
910 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
911 FILE *f = NULL;
913 err = got_object_id_str(&id_str, deriv_base_commit_id);
914 if (err)
915 return got_error_from_errno("asprintf");
917 if (asprintf(&label_deriv, "%s: commit %s",
918 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 err = got_error_from_errno("asprintf");
920 goto done;
923 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
924 if (err)
925 goto done;
927 if (fprintf(f, "%s: Could not install symbolic link because of merge "
928 "conflict.\nln(1) may be used to fix the situation. If this is "
929 "intended to be a\nregular file instead then its expected "
930 "contents may be filled in.\nThe following conflicting symlink "
931 "target paths were found:\n"
932 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
933 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
934 deriv_target ? deriv_target : "(symlink was deleted)",
935 orig_target ? label_orig : "",
936 orig_target ? "\n" : "",
937 orig_target ? orig_target : "",
938 orig_target ? "\n" : "",
939 GOT_DIFF_CONFLICT_MARKER_SEP,
940 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
941 err = got_error_from_errno2("fprintf", path);
942 goto done;
945 if (unlink(ondisk_path) == -1) {
946 err = got_error_from_errno2("unlink", ondisk_path);
947 goto done;
949 if (rename(path, ondisk_path) == -1) {
950 err = got_error_from_errno3("rename", path, ondisk_path);
951 goto done;
953 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
954 err = got_error_from_errno2("chmod", ondisk_path);
955 goto done;
957 done:
958 if (f != NULL && fclose(f) == EOF && err == NULL)
959 err = got_error_from_errno2("fclose", path);
960 free(path);
961 free(id_str);
962 free(label_deriv);
963 return err;
966 /* forward declaration */
967 static const struct got_error *
968 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
969 const char *, const char *, uint16_t, const char *,
970 struct got_blob_object *, struct got_object_id *,
971 struct got_repository *, got_worktree_checkout_cb, void *);
973 /*
974 * Merge a symlink into the work tree, where blob_orig acts as the common
975 * ancestor, blob_deriv acts as the first derived version, and the symlink
976 * on disk acts as the second derived version.
977 * Assume that contents of both blobs represent symlinks.
978 */
979 static const struct got_error *
980 merge_symlink(struct got_worktree *worktree,
981 struct got_blob_object *blob_orig, const char *ondisk_path,
982 const char *path, uint16_t st_mode, const char *label_orig,
983 struct got_blob_object *blob_deriv,
984 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
985 got_worktree_checkout_cb progress_cb, void *progress_arg)
987 const struct got_error *err = NULL;
988 char *ancestor_target = NULL, *deriv_target = NULL;
989 struct stat sb;
990 ssize_t ondisk_len, deriv_len;
991 char ondisk_target[PATH_MAX];
992 int have_local_change = 0;
993 int have_incoming_change = 0;
995 if (lstat(ondisk_path, &sb) == -1)
996 return got_error_from_errno2("lstat", ondisk_path);
998 if (!S_ISLNK(sb.st_mode)) {
999 /*
1000 * If there is a regular file on disk, merge the symlink
1001 * target path into this file, which will usually cause
1002 * a merge conflict.
1004 if (S_ISREG(sb.st_mode)) {
1005 int local_changes_subsumed;
1006 return merge_blob(&local_changes_subsumed, worktree,
1007 NULL, ondisk_path, path, sb.st_mode, label_orig,
1008 blob_deriv, deriv_base_commit_id,
1009 repo, progress_cb, progress_arg);
1012 /* TODO symlink is obstructed; do something */
1013 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
1016 ondisk_len = readlink(ondisk_path, ondisk_target,
1017 sizeof(ondisk_target));
1018 if (ondisk_len == -1) {
1019 err = got_error_from_errno2("readlink",
1020 ondisk_path);
1021 goto done;
1023 ondisk_target[ondisk_len] = '\0';
1025 if (blob_orig) {
1026 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1027 if (err)
1028 goto done;
1031 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
1032 if (err)
1033 goto done;
1035 if (ancestor_target == NULL ||
1036 (ondisk_len != strlen(ancestor_target) ||
1037 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1038 have_local_change = 1;
1040 deriv_len = strlen(deriv_target);
1041 if (ancestor_target == NULL ||
1042 (deriv_len != strlen(ancestor_target) ||
1043 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1044 have_incoming_change = 1;
1046 if (!have_local_change && !have_incoming_change) {
1047 if (ancestor_target) {
1048 /* Both sides made the same change. */
1049 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1050 path);
1051 } else if (deriv_len == ondisk_len &&
1052 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1053 /* Both sides added the same symlink. */
1054 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1055 path);
1056 } else {
1057 /* Both sides added symlinks which don't match. */
1058 err = install_symlink_conflict(deriv_target,
1059 deriv_base_commit_id, ancestor_target,
1060 label_orig, ondisk_target, ondisk_path);
1061 if (err)
1062 goto done;
1063 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1064 path);
1066 } else if (!have_local_change && have_incoming_change) {
1067 /* Apply the incoming change. */
1068 err = update_symlink(ondisk_path, deriv_target,
1069 strlen(deriv_target));
1070 if (err)
1071 goto done;
1072 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1073 } else if (have_local_change && have_incoming_change) {
1074 err = install_symlink_conflict(deriv_target,
1075 deriv_base_commit_id, ancestor_target, label_orig,
1076 ondisk_target, ondisk_path);
1077 if (err)
1078 goto done;
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1080 path);
1083 done:
1084 free(ancestor_target);
1085 free(deriv_target);
1086 return err;
1090 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1091 * blob_deriv acts as the first derived version, and the file on disk
1092 * acts as the second derived version.
1094 static const struct got_error *
1095 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1096 struct got_blob_object *blob_orig, const char *ondisk_path,
1097 const char *path, uint16_t st_mode, const char *label_orig,
1098 struct got_blob_object *blob_deriv,
1099 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1100 got_worktree_checkout_cb progress_cb, void *progress_arg)
1102 const struct got_error *err = NULL;
1103 FILE *f_deriv = NULL;
1104 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1105 char *label_deriv = NULL, *parent;
1107 *local_changes_subsumed = 0;
1109 parent = dirname(ondisk_path);
1110 if (parent == NULL)
1111 return got_error_from_errno2("dirname", ondisk_path);
1113 free(base_path);
1114 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1115 err = got_error_from_errno("asprintf");
1116 base_path = NULL;
1117 goto done;
1120 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1121 if (err)
1122 goto done;
1123 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1124 blob_deriv);
1125 if (err)
1126 goto done;
1128 err = got_object_id_str(&id_str, deriv_base_commit_id);
1129 if (err)
1130 goto done;
1131 if (asprintf(&label_deriv, "%s: commit %s",
1132 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1133 err = got_error_from_errno("asprintf");
1134 goto done;
1137 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1138 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1139 label_deriv, repo, progress_cb, progress_arg);
1140 done:
1141 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1142 err = got_error_from_errno("fclose");
1143 free(base_path);
1144 if (blob_deriv_path) {
1145 unlink(blob_deriv_path);
1146 free(blob_deriv_path);
1148 free(id_str);
1149 free(label_deriv);
1150 return err;
1153 static const struct got_error *
1154 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1155 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1156 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1158 const struct got_error *err = NULL;
1159 struct got_fileindex_entry *new_ie;
1161 *new_iep = NULL;
1163 err = got_fileindex_entry_alloc(&new_ie, path);
1164 if (err)
1165 return err;
1167 err = got_fileindex_entry_update(new_ie, ondisk_path,
1168 blob_id->sha1, base_commit_id->sha1, 1);
1169 if (err)
1170 goto done;
1172 err = got_fileindex_entry_add(fileindex, new_ie);
1173 done:
1174 if (err)
1175 got_fileindex_entry_free(new_ie);
1176 else
1177 *new_iep = new_ie;
1178 return err;
1181 static mode_t
1182 get_ondisk_perms(int executable, mode_t st_mode)
1184 mode_t xbits = S_IXUSR;
1186 if (executable) {
1187 /* Map read bits to execute bits. */
1188 if (st_mode & S_IRGRP)
1189 xbits |= S_IXGRP;
1190 if (st_mode & S_IROTH)
1191 xbits |= S_IXOTH;
1192 return st_mode | xbits;
1195 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1198 /* forward declaration */
1199 static const struct got_error *
1200 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1201 const char *path, mode_t te_mode, mode_t st_mode,
1202 struct got_blob_object *blob, int restoring_missing_file,
1203 int reverting_versioned_file, int installing_bad_symlink,
1204 int path_is_unversioned, struct got_repository *repo,
1205 got_worktree_checkout_cb progress_cb, void *progress_arg);
1208 * This function assumes that the provided symlink target points at a
1209 * safe location in the work tree!
1211 static const struct got_error *
1212 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1213 size_t target_len)
1215 const struct got_error *err = NULL;
1216 ssize_t elen;
1217 char etarget[PATH_MAX];
1218 int fd;
1221 * "Bad" symlinks (those pointing outside the work tree or into the
1222 * .got directory) are installed in the work tree as a regular file
1223 * which contains the bad symlink target path.
1224 * The new symlink target has already been checked for safety by our
1225 * caller. If we can successfully open a regular file then we simply
1226 * replace this file with a symlink below.
1228 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1229 if (fd == -1) {
1230 if (errno != ELOOP)
1231 return got_error_from_errno2("open", ondisk_path);
1233 /* We are updating an existing on-disk symlink. */
1234 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1235 if (elen == -1)
1236 return got_error_from_errno2("readlink", ondisk_path);
1238 if (elen == target_len &&
1239 memcmp(etarget, target_path, target_len) == 0)
1240 return NULL; /* nothing to do */
1243 err = update_symlink(ondisk_path, target_path, target_len);
1244 if (fd != -1 && close(fd) == -1 && err == NULL)
1245 err = got_error_from_errno2("close", ondisk_path);
1246 return err;
1249 static const struct got_error *
1250 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1251 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1252 int restoring_missing_file, int reverting_versioned_file,
1253 int path_is_unversioned, struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *resolved_path = NULL, *abspath = NULL;
1260 char *path_got = NULL;
1261 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1262 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1264 *is_bad_symlink = 0;
1267 * Blob object content specifies the target path of the link.
1268 * If a symbolic link cannot be installed we instead create
1269 * a regular file which contains the link target path stored
1270 * in the blob object.
1272 do {
1273 err = got_object_blob_read_block(&len, blob);
1274 if (len + target_len >= sizeof(target_path)) {
1275 /* Path too long; install as a regular file. */
1276 *is_bad_symlink = 1;
1277 got_object_blob_rewind(blob);
1278 return install_blob(worktree, ondisk_path, path,
1279 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1280 restoring_missing_file, reverting_versioned_file,
1281 1, path_is_unversioned, repo, progress_cb,
1282 progress_arg);
1284 if (len > 0) {
1285 /* Skip blob object header first time around. */
1286 memcpy(target_path + target_len, buf + hdrlen,
1287 len - hdrlen);
1288 target_len += len - hdrlen;
1289 hdrlen = 0;
1291 } while (len != 0);
1292 target_path[target_len] = '\0';
1295 * Relative symlink target lookup should begin at the directory
1296 * in which the blob object is being installed.
1298 if (!got_path_is_absolute(target_path)) {
1299 char *parent = dirname(ondisk_path);
1300 if (parent == NULL) {
1301 err = got_error_from_errno2("dirname", ondisk_path);
1302 goto done;
1304 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1305 err = got_error_from_errno("asprintf");
1306 goto done;
1311 * unveil(2) restricts our view of paths in the filesystem.
1312 * ENOENT will occur if a link target path does not exist or
1313 * if it points outside our unveiled path space.
1315 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1316 if (resolved_path == NULL) {
1317 if (errno != ENOENT) {
1318 err = got_error_from_errno2("realpath", target_path);
1319 goto done;
1323 /* Only allow symlinks pointing at paths within the work tree. */
1324 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1325 abspath : target_path), worktree->root_path,
1326 strlen(worktree->root_path))) {
1327 /* install as a regular file */
1328 *is_bad_symlink = 1;
1329 got_object_blob_rewind(blob);
1330 err = install_blob(worktree, ondisk_path, path,
1331 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1332 restoring_missing_file, reverting_versioned_file, 1,
1333 path_is_unversioned, repo, progress_cb, progress_arg);
1334 goto done;
1337 /* Do not allow symlinks pointing into the .got directory. */
1338 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1339 GOT_WORKTREE_GOT_DIR) == -1) {
1340 err = got_error_from_errno("asprintf");
1341 goto done;
1343 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1344 abspath : target_path), path_got, strlen(path_got))) {
1345 /* install as a regular file */
1346 *is_bad_symlink = 1;
1347 got_object_blob_rewind(blob);
1348 err = install_blob(worktree, ondisk_path, path,
1349 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1350 restoring_missing_file, reverting_versioned_file, 1,
1351 path_is_unversioned, repo, progress_cb, progress_arg);
1352 goto done;
1355 if (symlink(target_path, ondisk_path) == -1) {
1356 if (errno == EEXIST) {
1357 if (path_is_unversioned) {
1358 err = (*progress_cb)(progress_arg,
1359 GOT_STATUS_UNVERSIONED, path);
1360 goto done;
1362 err = replace_existing_symlink(ondisk_path,
1363 target_path, target_len);
1364 if (err)
1365 goto done;
1366 if (progress_cb) {
1367 err = (*progress_cb)(progress_arg,
1368 GOT_STATUS_UPDATE, path);
1370 goto done; /* Nothing else to do. */
1373 if (errno == ENOENT) {
1374 char *parent = dirname(ondisk_path);
1375 if (parent == NULL) {
1376 err = got_error_from_errno2("dirname",
1377 ondisk_path);
1378 goto done;
1380 err = add_dir_on_disk(worktree, parent);
1381 if (err)
1382 goto done;
1384 * Retry, and fall through to error handling
1385 * below if this second attempt fails.
1387 if (symlink(target_path, ondisk_path) != -1) {
1388 err = NULL; /* success */
1389 goto done;
1393 /* Handle errors from first or second creation attempt. */
1394 if (errno == ENAMETOOLONG) {
1395 /* bad target path; install as a regular file */
1396 *is_bad_symlink = 1;
1397 got_object_blob_rewind(blob);
1398 err = install_blob(worktree, ondisk_path, path,
1399 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1400 restoring_missing_file, reverting_versioned_file, 1,
1401 path_is_unversioned, repo,
1402 progress_cb, progress_arg);
1403 } else if (errno == ENOTDIR) {
1404 err = got_error_path(ondisk_path,
1405 GOT_ERR_FILE_OBSTRUCTED);
1406 } else {
1407 err = got_error_from_errno3("symlink",
1408 target_path, ondisk_path);
1410 } else if (progress_cb)
1411 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1412 done:
1413 free(resolved_path);
1414 free(abspath);
1415 free(path_got);
1416 return err;
1419 static const struct got_error *
1420 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1421 const char *path, mode_t te_mode, mode_t st_mode,
1422 struct got_blob_object *blob, int restoring_missing_file,
1423 int reverting_versioned_file, int installing_bad_symlink,
1424 int path_is_unversioned, struct got_repository *repo,
1425 got_worktree_checkout_cb progress_cb, void *progress_arg)
1427 const struct got_error *err = NULL;
1428 int fd = -1;
1429 size_t len, hdrlen;
1430 int update = 0;
1431 char *tmppath = NULL;
1433 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1434 GOT_DEFAULT_FILE_MODE);
1435 if (fd == -1) {
1436 if (errno == ENOENT) {
1437 char *parent = dirname(path);
1438 if (parent == NULL)
1439 return got_error_from_errno2("dirname", path);
1440 err = add_dir_on_disk(worktree, parent);
1441 if (err)
1442 return err;
1443 fd = open(ondisk_path,
1444 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1445 GOT_DEFAULT_FILE_MODE);
1446 if (fd == -1)
1447 return got_error_from_errno2("open",
1448 ondisk_path);
1449 } else if (errno == EEXIST) {
1450 if (path_is_unversioned) {
1451 err = (*progress_cb)(progress_arg,
1452 GOT_STATUS_UNVERSIONED, path);
1453 goto done;
1455 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1456 /* TODO file is obstructed; do something */
1457 err = got_error_path(ondisk_path,
1458 GOT_ERR_FILE_OBSTRUCTED);
1459 goto done;
1460 } else {
1461 err = got_opentemp_named_fd(&tmppath, &fd,
1462 ondisk_path);
1463 if (err)
1464 goto done;
1465 update = 1;
1467 } else
1468 return got_error_from_errno2("open", ondisk_path);
1471 if (progress_cb) {
1472 if (restoring_missing_file)
1473 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1474 path);
1475 else if (reverting_versioned_file)
1476 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1477 path);
1478 else
1479 err = (*progress_cb)(progress_arg,
1480 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1481 if (err)
1482 goto done;
1485 hdrlen = got_object_blob_get_hdrlen(blob);
1486 do {
1487 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1488 err = got_object_blob_read_block(&len, blob);
1489 if (err)
1490 break;
1491 if (len > 0) {
1492 /* Skip blob object header first time around. */
1493 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1494 if (outlen == -1) {
1495 err = got_error_from_errno("write");
1496 goto done;
1497 } else if (outlen != len - hdrlen) {
1498 err = got_error(GOT_ERR_IO);
1499 goto done;
1501 hdrlen = 0;
1503 } while (len != 0);
1505 if (fsync(fd) != 0) {
1506 err = got_error_from_errno("fsync");
1507 goto done;
1510 if (update) {
1511 if (rename(tmppath, ondisk_path) != 0) {
1512 err = got_error_from_errno3("rename", tmppath,
1513 ondisk_path);
1514 unlink(tmppath);
1515 goto done;
1519 if (chmod(ondisk_path,
1520 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1521 err = got_error_from_errno2("chmod", ondisk_path);
1522 goto done;
1525 done:
1526 if (fd != -1 && close(fd) != 0 && err == NULL)
1527 err = got_error_from_errno("close");
1528 free(tmppath);
1529 return err;
1532 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1533 static const struct got_error *
1534 get_modified_file_content_status(unsigned char *status, FILE *f)
1536 const struct got_error *err = NULL;
1537 const char *markers[3] = {
1538 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1539 GOT_DIFF_CONFLICT_MARKER_SEP,
1540 GOT_DIFF_CONFLICT_MARKER_END
1542 int i = 0;
1543 char *line;
1544 size_t len;
1545 const char delim[3] = {'\0', '\0', '\0'};
1547 while (*status == GOT_STATUS_MODIFY) {
1548 line = fparseln(f, &len, NULL, delim, 0);
1549 if (line == NULL) {
1550 if (feof(f))
1551 break;
1552 err = got_ferror(f, GOT_ERR_IO);
1553 break;
1556 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1557 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1558 == 0)
1559 *status = GOT_STATUS_CONFLICT;
1560 else
1561 i++;
1565 return err;
1568 static int
1569 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1571 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1572 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1575 static int
1576 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1578 return !(ie->ctime_sec == sb->st_ctime &&
1579 ie->ctime_nsec == sb->st_ctimensec &&
1580 ie->mtime_sec == sb->st_mtime &&
1581 ie->mtime_nsec == sb->st_mtimensec &&
1582 ie->size == (sb->st_size & 0xffffffff) &&
1583 !xbit_differs(ie, sb->st_mode));
1586 static unsigned char
1587 get_staged_status(struct got_fileindex_entry *ie)
1589 switch (got_fileindex_entry_stage_get(ie)) {
1590 case GOT_FILEIDX_STAGE_ADD:
1591 return GOT_STATUS_ADD;
1592 case GOT_FILEIDX_STAGE_DELETE:
1593 return GOT_STATUS_DELETE;
1594 case GOT_FILEIDX_STAGE_MODIFY:
1595 return GOT_STATUS_MODIFY;
1596 default:
1597 return GOT_STATUS_NO_CHANGE;
1601 static const struct got_error *
1602 get_symlink_status(unsigned char *status, struct stat *sb,
1603 struct got_fileindex_entry *ie, const char *abspath,
1604 int dirfd, const char *de_name, struct got_blob_object *blob)
1606 const struct got_error *err = NULL;
1607 char target_path[PATH_MAX];
1608 char etarget[PATH_MAX];
1609 ssize_t elen;
1610 size_t len, target_len = 0;
1611 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1612 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1614 *status = GOT_STATUS_NO_CHANGE;
1616 /* Blob object content specifies the target path of the link. */
1617 do {
1618 err = got_object_blob_read_block(&len, blob);
1619 if (err)
1620 return err;
1621 if (len + target_len >= sizeof(target_path)) {
1623 * Should not happen. The blob contents were OK
1624 * when this symlink was installed.
1626 return got_error(GOT_ERR_NO_SPACE);
1628 if (len > 0) {
1629 /* Skip blob object header first time around. */
1630 memcpy(target_path + target_len, buf + hdrlen,
1631 len - hdrlen);
1632 target_len += len - hdrlen;
1633 hdrlen = 0;
1635 } while (len != 0);
1636 target_path[target_len] = '\0';
1638 if (dirfd != -1) {
1639 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1640 if (elen == -1)
1641 return got_error_from_errno2("readlinkat", abspath);
1642 } else {
1643 elen = readlink(abspath, etarget, sizeof(etarget));
1644 if (elen == -1)
1645 return got_error_from_errno2("readlinkat", abspath);
1648 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1649 *status = GOT_STATUS_MODIFY;
1651 return NULL;
1654 static const struct got_error *
1655 get_file_status(unsigned char *status, struct stat *sb,
1656 struct got_fileindex_entry *ie, const char *abspath,
1657 int dirfd, const char *de_name, struct got_repository *repo)
1659 const struct got_error *err = NULL;
1660 struct got_object_id id;
1661 size_t hdrlen;
1662 int fd = -1;
1663 FILE *f = NULL;
1664 uint8_t fbuf[8192];
1665 struct got_blob_object *blob = NULL;
1666 size_t flen, blen;
1667 unsigned char staged_status = get_staged_status(ie);
1669 *status = GOT_STATUS_NO_CHANGE;
1672 * Whenever the caller provides a directory descriptor and a
1673 * directory entry name for the file, use them! This prevents
1674 * race conditions if filesystem paths change beneath our feet.
1676 if (dirfd != -1) {
1677 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1678 if (errno == ENOENT) {
1679 if (got_fileindex_entry_has_file_on_disk(ie))
1680 *status = GOT_STATUS_MISSING;
1681 else
1682 *status = GOT_STATUS_DELETE;
1683 goto done;
1685 err = got_error_from_errno2("fstatat", abspath);
1686 goto done;
1688 } else {
1689 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1690 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1691 return got_error_from_errno2("open", abspath);
1692 else if (fd == -1 && errno == ELOOP) {
1693 if (lstat(abspath, sb) == -1)
1694 return got_error_from_errno2("lstat", abspath);
1695 } else if (fd == -1 || fstat(fd, sb) == -1) {
1696 if (errno == ENOENT) {
1697 if (got_fileindex_entry_has_file_on_disk(ie))
1698 *status = GOT_STATUS_MISSING;
1699 else
1700 *status = GOT_STATUS_DELETE;
1701 goto done;
1703 err = got_error_from_errno2("fstat", abspath);
1704 goto done;
1708 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1709 *status = GOT_STATUS_OBSTRUCTED;
1710 goto done;
1713 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1714 *status = GOT_STATUS_DELETE;
1715 goto done;
1716 } else if (!got_fileindex_entry_has_blob(ie) &&
1717 staged_status != GOT_STATUS_ADD) {
1718 *status = GOT_STATUS_ADD;
1719 goto done;
1722 if (!stat_info_differs(ie, sb))
1723 goto done;
1725 if (staged_status == GOT_STATUS_MODIFY ||
1726 staged_status == GOT_STATUS_ADD)
1727 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1728 else
1729 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1731 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1732 if (err)
1733 goto done;
1735 if (S_ISLNK(sb->st_mode)) {
1736 /* Staging changes to symlinks is not yet(?) supported. */
1737 if (staged_status != GOT_STATUS_NO_CHANGE) {
1738 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1739 goto done;
1741 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1742 de_name, blob);
1743 goto done;
1747 if (dirfd != -1) {
1748 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1749 if (fd == -1) {
1750 err = got_error_from_errno2("openat", abspath);
1751 goto done;
1755 f = fdopen(fd, "r");
1756 if (f == NULL) {
1757 err = got_error_from_errno2("fdopen", abspath);
1758 goto done;
1760 fd = -1;
1761 hdrlen = got_object_blob_get_hdrlen(blob);
1762 for (;;) {
1763 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1764 err = got_object_blob_read_block(&blen, blob);
1765 if (err)
1766 goto done;
1767 /* Skip length of blob object header first time around. */
1768 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1769 if (flen == 0 && ferror(f)) {
1770 err = got_error_from_errno("fread");
1771 goto done;
1773 if (blen == 0) {
1774 if (flen != 0)
1775 *status = GOT_STATUS_MODIFY;
1776 break;
1777 } else if (flen == 0) {
1778 if (blen != 0)
1779 *status = GOT_STATUS_MODIFY;
1780 break;
1781 } else if (blen - hdrlen == flen) {
1782 /* Skip blob object header first time around. */
1783 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1784 *status = GOT_STATUS_MODIFY;
1785 break;
1787 } else {
1788 *status = GOT_STATUS_MODIFY;
1789 break;
1791 hdrlen = 0;
1794 if (*status == GOT_STATUS_MODIFY) {
1795 rewind(f);
1796 err = get_modified_file_content_status(status, f);
1797 } else if (xbit_differs(ie, sb->st_mode))
1798 *status = GOT_STATUS_MODE_CHANGE;
1799 done:
1800 if (blob)
1801 got_object_blob_close(blob);
1802 if (f != NULL && fclose(f) == EOF && err == NULL)
1803 err = got_error_from_errno2("fclose", abspath);
1804 if (fd != -1 && close(fd) == -1 && err == NULL)
1805 err = got_error_from_errno2("close", abspath);
1806 return err;
1810 * Update timestamps in the file index if a file is unmodified and
1811 * we had to run a full content comparison to find out.
1813 static const struct got_error *
1814 sync_timestamps(char *ondisk_path, unsigned char status,
1815 struct got_fileindex_entry *ie, struct stat *sb)
1817 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1818 return got_fileindex_entry_update(ie, ondisk_path,
1819 ie->blob_sha1, ie->commit_sha1, 1);
1821 return NULL;
1824 static const struct got_error *
1825 update_blob(struct got_worktree *worktree,
1826 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1827 struct got_tree_entry *te, const char *path,
1828 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1829 void *progress_arg)
1831 const struct got_error *err = NULL;
1832 struct got_blob_object *blob = NULL;
1833 char *ondisk_path;
1834 unsigned char status = GOT_STATUS_NO_CHANGE;
1835 struct stat sb;
1837 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1838 return got_error_from_errno("asprintf");
1840 if (ie) {
1841 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1842 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1843 goto done;
1845 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1846 repo);
1847 if (err)
1848 goto done;
1849 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1850 sb.st_mode = got_fileindex_perms_to_st(ie);
1851 } else {
1852 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 status = GOT_STATUS_UNVERSIONED;
1856 if (status == GOT_STATUS_OBSTRUCTED) {
1857 err = (*progress_cb)(progress_arg, status, path);
1858 goto done;
1860 if (status == GOT_STATUS_CONFLICT) {
1861 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1862 path);
1863 goto done;
1866 if (ie && status != GOT_STATUS_MISSING &&
1867 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1868 if (got_fileindex_entry_has_commit(ie) &&
1869 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1870 SHA1_DIGEST_LENGTH) == 0) {
1871 err = sync_timestamps(ondisk_path, status, ie, &sb);
1872 if (err)
1873 goto done;
1874 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1875 path);
1876 goto done;
1878 if (got_fileindex_entry_has_blob(ie) &&
1879 memcmp(ie->blob_sha1, te->id.sha1,
1880 SHA1_DIGEST_LENGTH) == 0) {
1881 err = sync_timestamps(ondisk_path, status, ie, &sb);
1882 goto done;
1886 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1887 if (err)
1888 goto done;
1890 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 int update_timestamps;
1892 struct got_blob_object *blob2 = NULL;
1893 char *label_orig = NULL;
1894 if (got_fileindex_entry_has_blob(ie)) {
1895 struct got_object_id id2;
1896 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1898 if (err)
1899 goto done;
1901 if (got_fileindex_entry_has_commit(ie)) {
1902 char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 sizeof(id_str)) == NULL) {
1905 err = got_error_path(id_str,
1906 GOT_ERR_BAD_OBJ_ID_STR);
1907 goto done;
1909 if (asprintf(&label_orig, "%s: commit %s",
1910 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 err = got_error_from_errno("asprintf");
1912 goto done;
1915 if (S_ISLNK(te->mode)) {
1916 err = merge_symlink(worktree, blob2,
1917 ondisk_path, path, sb.st_mode, label_orig,
1918 blob, worktree->base_commit_id, repo,
1919 progress_cb, progress_arg);
1920 } else {
1921 err = merge_blob(&update_timestamps, worktree, blob2,
1922 ondisk_path, path, sb.st_mode, label_orig, blob,
1923 worktree->base_commit_id, repo,
1924 progress_cb, progress_arg);
1926 free(label_orig);
1927 if (blob2)
1928 got_object_blob_close(blob2);
1929 if (err)
1930 goto done;
1932 * Do not update timestamps of files with local changes.
1933 * Otherwise, a future status walk would treat them as
1934 * unmodified files again.
1936 err = got_fileindex_entry_update(ie, ondisk_path,
1937 blob->id.sha1, worktree->base_commit_id->sha1,
1938 update_timestamps);
1939 } else if (status == GOT_STATUS_MODE_CHANGE) {
1940 err = got_fileindex_entry_update(ie, ondisk_path,
1941 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1942 } else if (status == GOT_STATUS_DELETE) {
1943 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1944 if (err)
1945 goto done;
1946 err = got_fileindex_entry_update(ie, ondisk_path,
1947 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1948 if (err)
1949 goto done;
1950 } else {
1951 int is_bad_symlink = 0;
1952 if (S_ISLNK(te->mode)) {
1953 err = install_symlink(&is_bad_symlink, worktree,
1954 ondisk_path, path, blob,
1955 status == GOT_STATUS_MISSING, 0,
1956 status == GOT_STATUS_UNVERSIONED, repo,
1957 progress_cb, progress_arg);
1958 } else {
1959 err = install_blob(worktree, ondisk_path, path,
1960 te->mode, sb.st_mode, blob,
1961 status == GOT_STATUS_MISSING, 0, 0,
1962 status == GOT_STATUS_UNVERSIONED, repo,
1963 progress_cb, progress_arg);
1965 if (err)
1966 goto done;
1968 if (ie) {
1969 err = got_fileindex_entry_update(ie, ondisk_path,
1970 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1971 } else {
1972 err = create_fileindex_entry(&ie, fileindex,
1973 worktree->base_commit_id, ondisk_path, path,
1974 &blob->id);
1976 if (err)
1977 goto done;
1979 if (is_bad_symlink) {
1980 err = got_fileindex_entry_filetype_set(ie,
1981 GOT_FILEIDX_MODE_BAD_SYMLINK);
1982 if (err)
1983 goto done;
1986 got_object_blob_close(blob);
1987 done:
1988 free(ondisk_path);
1989 return err;
1992 static const struct got_error *
1993 remove_ondisk_file(const char *root_path, const char *path)
1995 const struct got_error *err = NULL;
1996 char *ondisk_path = NULL;
1998 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1999 return got_error_from_errno("asprintf");
2001 if (unlink(ondisk_path) == -1) {
2002 if (errno != ENOENT)
2003 err = got_error_from_errno2("unlink", ondisk_path);
2004 } else {
2005 char *parent = dirname(ondisk_path);
2006 while (parent && strcmp(parent, root_path) != 0) {
2007 if (rmdir(parent) == -1) {
2008 if (errno != ENOTEMPTY)
2009 err = got_error_from_errno2("rmdir",
2010 parent);
2011 break;
2013 parent = dirname(parent);
2016 free(ondisk_path);
2017 return err;
2020 static const struct got_error *
2021 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2022 struct got_fileindex_entry *ie, struct got_repository *repo,
2023 got_worktree_checkout_cb progress_cb, void *progress_arg)
2025 const struct got_error *err = NULL;
2026 unsigned char status;
2027 struct stat sb;
2028 char *ondisk_path;
2030 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2031 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2033 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2034 == -1)
2035 return got_error_from_errno("asprintf");
2037 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2038 if (err)
2039 goto done;
2041 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2042 char ondisk_target[PATH_MAX];
2043 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2044 sizeof(ondisk_target));
2045 if (ondisk_len == -1) {
2046 err = got_error_from_errno2("readlink", ondisk_path);
2047 goto done;
2049 ondisk_target[ondisk_len] = '\0';
2050 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2051 NULL, NULL, /* XXX pass common ancestor info? */
2052 ondisk_target, ondisk_path);
2053 if (err)
2054 goto done;
2055 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2056 ie->path);
2057 goto done;
2060 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2061 status == GOT_STATUS_ADD) {
2062 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2063 if (err)
2064 goto done;
2066 * Preserve the working file and change the deleted blob's
2067 * entry into a schedule-add entry.
2069 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2070 0);
2071 } else {
2072 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2073 if (err)
2074 goto done;
2075 if (status == GOT_STATUS_NO_CHANGE) {
2076 err = remove_ondisk_file(worktree->root_path, ie->path);
2077 if (err)
2078 goto done;
2080 got_fileindex_entry_remove(fileindex, ie);
2082 done:
2083 free(ondisk_path);
2084 return err;
2087 struct diff_cb_arg {
2088 struct got_fileindex *fileindex;
2089 struct got_worktree *worktree;
2090 struct got_repository *repo;
2091 got_worktree_checkout_cb progress_cb;
2092 void *progress_arg;
2093 got_cancel_cb cancel_cb;
2094 void *cancel_arg;
2097 static const struct got_error *
2098 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2099 struct got_tree_entry *te, const char *parent_path)
2101 struct diff_cb_arg *a = arg;
2103 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2104 return got_error(GOT_ERR_CANCELLED);
2106 return update_blob(a->worktree, a->fileindex, ie, te,
2107 ie->path, a->repo, a->progress_cb, a->progress_arg);
2110 static const struct got_error *
2111 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2113 struct diff_cb_arg *a = arg;
2115 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2116 return got_error(GOT_ERR_CANCELLED);
2118 return delete_blob(a->worktree, a->fileindex, ie,
2119 a->repo, a->progress_cb, a->progress_arg);
2122 static const struct got_error *
2123 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2125 struct diff_cb_arg *a = arg;
2126 const struct got_error *err;
2127 char *path;
2129 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2130 return got_error(GOT_ERR_CANCELLED);
2132 if (got_object_tree_entry_is_submodule(te))
2133 return NULL;
2135 if (asprintf(&path, "%s%s%s", parent_path,
2136 parent_path[0] ? "/" : "", te->name)
2137 == -1)
2138 return got_error_from_errno("asprintf");
2140 if (S_ISDIR(te->mode))
2141 err = add_dir_on_disk(a->worktree, path);
2142 else
2143 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2144 a->repo, a->progress_cb, a->progress_arg);
2146 free(path);
2147 return err;
2150 static const struct got_error *
2151 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2153 const struct got_error *err = NULL;
2154 char *uuidstr = NULL;
2155 uint32_t uuid_status;
2157 *refname = NULL;
2159 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2160 if (uuid_status != uuid_s_ok)
2161 return got_error_uuid(uuid_status, "uuid_to_string");
2163 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2164 == -1) {
2165 err = got_error_from_errno("asprintf");
2166 *refname = NULL;
2168 free(uuidstr);
2169 return err;
2172 const struct got_error *
2173 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2175 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2178 static const struct got_error *
2179 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2181 return get_ref_name(refname, worktree,
2182 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2185 static const struct got_error *
2186 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2188 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2191 static const struct got_error *
2192 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2194 return get_ref_name(refname, worktree,
2195 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2198 static const struct got_error *
2199 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2201 return get_ref_name(refname, worktree,
2202 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2205 static const struct got_error *
2206 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2208 return get_ref_name(refname, worktree,
2209 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2212 static const struct got_error *
2213 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2215 return get_ref_name(refname, worktree,
2216 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2219 static const struct got_error *
2220 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2222 return get_ref_name(refname, worktree,
2223 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2226 static const struct got_error *
2227 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2229 return get_ref_name(refname, worktree,
2230 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2233 const struct got_error *
2234 got_worktree_get_histedit_script_path(char **path,
2235 struct got_worktree *worktree)
2237 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2238 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2239 *path = NULL;
2240 return got_error_from_errno("asprintf");
2242 return NULL;
2246 * Prevent Git's garbage collector from deleting our base commit by
2247 * setting a reference to our base commit's ID.
2249 static const struct got_error *
2250 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2252 const struct got_error *err = NULL;
2253 struct got_reference *ref = NULL;
2254 char *refname;
2256 err = got_worktree_get_base_ref_name(&refname, worktree);
2257 if (err)
2258 return err;
2260 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2261 if (err)
2262 goto done;
2264 err = got_ref_write(ref, repo);
2265 done:
2266 free(refname);
2267 if (ref)
2268 got_ref_close(ref);
2269 return err;
2272 static const struct got_error *
2273 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2275 const struct got_error *err = NULL;
2277 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2278 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2279 err = got_error_from_errno("asprintf");
2280 *fileindex_path = NULL;
2282 return err;
2286 static const struct got_error *
2287 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2288 struct got_worktree *worktree)
2290 const struct got_error *err = NULL;
2291 FILE *index = NULL;
2293 *fileindex_path = NULL;
2294 *fileindex = got_fileindex_alloc();
2295 if (*fileindex == NULL)
2296 return got_error_from_errno("got_fileindex_alloc");
2298 err = get_fileindex_path(fileindex_path, worktree);
2299 if (err)
2300 goto done;
2302 index = fopen(*fileindex_path, "rb");
2303 if (index == NULL) {
2304 if (errno != ENOENT)
2305 err = got_error_from_errno2("fopen", *fileindex_path);
2306 } else {
2307 err = got_fileindex_read(*fileindex, index);
2308 if (fclose(index) != 0 && err == NULL)
2309 err = got_error_from_errno("fclose");
2311 done:
2312 if (err) {
2313 free(*fileindex_path);
2314 *fileindex_path = NULL;
2315 got_fileindex_free(*fileindex);
2316 *fileindex = NULL;
2318 return err;
2321 struct bump_base_commit_id_arg {
2322 struct got_object_id *base_commit_id;
2323 const char *path;
2324 size_t path_len;
2325 const char *entry_name;
2326 got_worktree_checkout_cb progress_cb;
2327 void *progress_arg;
2330 /* Bump base commit ID of all files within an updated part of the work tree. */
2331 static const struct got_error *
2332 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2334 const struct got_error *err;
2335 struct bump_base_commit_id_arg *a = arg;
2337 if (a->entry_name) {
2338 if (strcmp(ie->path, a->path) != 0)
2339 return NULL;
2340 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2341 return NULL;
2343 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2344 SHA1_DIGEST_LENGTH) == 0)
2345 return NULL;
2347 if (a->progress_cb) {
2348 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2349 ie->path);
2350 if (err)
2351 return err;
2353 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2354 return NULL;
2357 static const struct got_error *
2358 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2360 const struct got_error *err = NULL;
2361 char *new_fileindex_path = NULL;
2362 FILE *new_index = NULL;
2363 struct timespec timeout;
2365 err = got_opentemp_named(&new_fileindex_path, &new_index,
2366 fileindex_path);
2367 if (err)
2368 goto done;
2370 err = got_fileindex_write(fileindex, new_index);
2371 if (err)
2372 goto done;
2374 if (rename(new_fileindex_path, fileindex_path) != 0) {
2375 err = got_error_from_errno3("rename", new_fileindex_path,
2376 fileindex_path);
2377 unlink(new_fileindex_path);
2381 * Sleep for a short amount of time to ensure that files modified after
2382 * this program exits have a different time stamp from the one which
2383 * was recorded in the file index.
2385 timeout.tv_sec = 0;
2386 timeout.tv_nsec = 1;
2387 nanosleep(&timeout, NULL);
2388 done:
2389 if (new_index)
2390 fclose(new_index);
2391 free(new_fileindex_path);
2392 return err;
2395 static const struct got_error *
2396 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2397 struct got_object_id **tree_id, const char *wt_relpath,
2398 struct got_worktree *worktree, struct got_repository *repo)
2400 const struct got_error *err = NULL;
2401 struct got_object_id *id = NULL;
2402 char *in_repo_path = NULL;
2403 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2405 *entry_type = GOT_OBJ_TYPE_ANY;
2406 *tree_relpath = NULL;
2407 *tree_id = NULL;
2409 if (wt_relpath[0] == '\0') {
2410 /* Check out all files within the work tree. */
2411 *entry_type = GOT_OBJ_TYPE_TREE;
2412 *tree_relpath = strdup("");
2413 if (*tree_relpath == NULL) {
2414 err = got_error_from_errno("strdup");
2415 goto done;
2417 err = got_object_id_by_path(tree_id, repo,
2418 worktree->base_commit_id, worktree->path_prefix);
2419 if (err)
2420 goto done;
2421 return NULL;
2424 /* Check out a subset of files in the work tree. */
2426 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2427 is_root_wt ? "" : "/", wt_relpath) == -1) {
2428 err = got_error_from_errno("asprintf");
2429 goto done;
2432 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2433 in_repo_path);
2434 if (err)
2435 goto done;
2437 free(in_repo_path);
2438 in_repo_path = NULL;
2440 err = got_object_get_type(entry_type, repo, id);
2441 if (err)
2442 goto done;
2444 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2445 /* Check out a single file. */
2446 if (strchr(wt_relpath, '/') == NULL) {
2447 /* Check out a single file in work tree's root dir. */
2448 in_repo_path = strdup(worktree->path_prefix);
2449 if (in_repo_path == NULL) {
2450 err = got_error_from_errno("strdup");
2451 goto done;
2453 *tree_relpath = strdup("");
2454 if (*tree_relpath == NULL) {
2455 err = got_error_from_errno("strdup");
2456 goto done;
2458 } else {
2459 /* Check out a single file in a subdirectory. */
2460 err = got_path_dirname(tree_relpath, wt_relpath);
2461 if (err)
2462 return err;
2463 if (asprintf(&in_repo_path, "%s%s%s",
2464 worktree->path_prefix, is_root_wt ? "" : "/",
2465 *tree_relpath) == -1) {
2466 err = got_error_from_errno("asprintf");
2467 goto done;
2470 err = got_object_id_by_path(tree_id, repo,
2471 worktree->base_commit_id, in_repo_path);
2472 } else {
2473 /* Check out all files within a subdirectory. */
2474 *tree_id = got_object_id_dup(id);
2475 if (*tree_id == NULL) {
2476 err = got_error_from_errno("got_object_id_dup");
2477 goto done;
2479 *tree_relpath = strdup(wt_relpath);
2480 if (*tree_relpath == NULL) {
2481 err = got_error_from_errno("strdup");
2482 goto done;
2485 done:
2486 free(id);
2487 free(in_repo_path);
2488 if (err) {
2489 *entry_type = GOT_OBJ_TYPE_ANY;
2490 free(*tree_relpath);
2491 *tree_relpath = NULL;
2492 free(*tree_id);
2493 *tree_id = NULL;
2495 return err;
2498 static const struct got_error *
2499 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2500 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2501 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2502 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2504 const struct got_error *err = NULL;
2505 struct got_commit_object *commit = NULL;
2506 struct got_tree_object *tree = NULL;
2507 struct got_fileindex_diff_tree_cb diff_cb;
2508 struct diff_cb_arg arg;
2510 err = ref_base_commit(worktree, repo);
2511 if (err) {
2512 if (!(err->code == GOT_ERR_ERRNO &&
2513 (errno == EACCES || errno == EROFS)))
2514 goto done;
2515 err = (*progress_cb)(progress_arg,
2516 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2517 if (err)
2518 return err;
2521 err = got_object_open_as_commit(&commit, repo,
2522 worktree->base_commit_id);
2523 if (err)
2524 goto done;
2526 err = got_object_open_as_tree(&tree, repo, tree_id);
2527 if (err)
2528 goto done;
2530 if (entry_name &&
2531 got_object_tree_find_entry(tree, entry_name) == NULL) {
2532 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2533 goto done;
2536 diff_cb.diff_old_new = diff_old_new;
2537 diff_cb.diff_old = diff_old;
2538 diff_cb.diff_new = diff_new;
2539 arg.fileindex = fileindex;
2540 arg.worktree = worktree;
2541 arg.repo = repo;
2542 arg.progress_cb = progress_cb;
2543 arg.progress_arg = progress_arg;
2544 arg.cancel_cb = cancel_cb;
2545 arg.cancel_arg = cancel_arg;
2546 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2547 entry_name, repo, &diff_cb, &arg);
2548 done:
2549 if (tree)
2550 got_object_tree_close(tree);
2551 if (commit)
2552 got_object_commit_close(commit);
2553 return err;
2556 const struct got_error *
2557 got_worktree_checkout_files(struct got_worktree *worktree,
2558 struct got_pathlist_head *paths, struct got_repository *repo,
2559 got_worktree_checkout_cb progress_cb, void *progress_arg,
2560 got_cancel_cb cancel_cb, void *cancel_arg)
2562 const struct got_error *err = NULL, *sync_err, *unlockerr;
2563 struct got_commit_object *commit = NULL;
2564 struct got_tree_object *tree = NULL;
2565 struct got_fileindex *fileindex = NULL;
2566 char *fileindex_path = NULL;
2567 struct got_pathlist_entry *pe;
2568 struct tree_path_data {
2569 SIMPLEQ_ENTRY(tree_path_data) entry;
2570 struct got_object_id *tree_id;
2571 int entry_type;
2572 char *relpath;
2573 char *entry_name;
2574 } *tpd = NULL;
2575 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2577 SIMPLEQ_INIT(&tree_paths);
2579 err = lock_worktree(worktree, LOCK_EX);
2580 if (err)
2581 return err;
2583 /* Map all specified paths to in-repository trees. */
2584 TAILQ_FOREACH(pe, paths, entry) {
2585 tpd = malloc(sizeof(*tpd));
2586 if (tpd == NULL) {
2587 err = got_error_from_errno("malloc");
2588 goto done;
2591 err = find_tree_entry_for_checkout(&tpd->entry_type,
2592 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2593 if (err) {
2594 free(tpd);
2595 goto done;
2598 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2599 err = got_path_basename(&tpd->entry_name, pe->path);
2600 if (err) {
2601 free(tpd->relpath);
2602 free(tpd->tree_id);
2603 free(tpd);
2604 goto done;
2606 } else
2607 tpd->entry_name = NULL;
2609 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2613 * Read the file index.
2614 * Checking out files is supposed to be an idempotent operation.
2615 * If the on-disk file index is incomplete we will try to complete it.
2617 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2618 if (err)
2619 goto done;
2621 tpd = SIMPLEQ_FIRST(&tree_paths);
2622 TAILQ_FOREACH(pe, paths, entry) {
2623 struct bump_base_commit_id_arg bbc_arg;
2625 err = checkout_files(worktree, fileindex, tpd->relpath,
2626 tpd->tree_id, tpd->entry_name, repo,
2627 progress_cb, progress_arg, cancel_cb, cancel_arg);
2628 if (err)
2629 break;
2631 bbc_arg.base_commit_id = worktree->base_commit_id;
2632 bbc_arg.entry_name = tpd->entry_name;
2633 bbc_arg.path = pe->path;
2634 bbc_arg.path_len = pe->path_len;
2635 bbc_arg.progress_cb = progress_cb;
2636 bbc_arg.progress_arg = progress_arg;
2637 err = got_fileindex_for_each_entry_safe(fileindex,
2638 bump_base_commit_id, &bbc_arg);
2639 if (err)
2640 break;
2642 tpd = SIMPLEQ_NEXT(tpd, entry);
2644 sync_err = sync_fileindex(fileindex, fileindex_path);
2645 if (sync_err && err == NULL)
2646 err = sync_err;
2647 done:
2648 free(fileindex_path);
2649 if (tree)
2650 got_object_tree_close(tree);
2651 if (commit)
2652 got_object_commit_close(commit);
2653 if (fileindex)
2654 got_fileindex_free(fileindex);
2655 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2656 tpd = SIMPLEQ_FIRST(&tree_paths);
2657 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2658 free(tpd->relpath);
2659 free(tpd->tree_id);
2660 free(tpd);
2662 unlockerr = lock_worktree(worktree, LOCK_SH);
2663 if (unlockerr && err == NULL)
2664 err = unlockerr;
2665 return err;
2668 struct merge_file_cb_arg {
2669 struct got_worktree *worktree;
2670 struct got_fileindex *fileindex;
2671 got_worktree_checkout_cb progress_cb;
2672 void *progress_arg;
2673 got_cancel_cb cancel_cb;
2674 void *cancel_arg;
2675 const char *label_orig;
2676 struct got_object_id *commit_id2;
2679 static const struct got_error *
2680 merge_file_cb(void *arg, struct got_blob_object *blob1,
2681 struct got_blob_object *blob2, struct got_object_id *id1,
2682 struct got_object_id *id2, const char *path1, const char *path2,
2683 mode_t mode1, mode_t mode2, struct got_repository *repo)
2685 static const struct got_error *err = NULL;
2686 struct merge_file_cb_arg *a = arg;
2687 struct got_fileindex_entry *ie;
2688 char *ondisk_path = NULL;
2689 struct stat sb;
2690 unsigned char status;
2691 int local_changes_subsumed;
2693 if (blob1 && blob2) {
2694 ie = got_fileindex_entry_get(a->fileindex, path2,
2695 strlen(path2));
2696 if (ie == NULL)
2697 return (*a->progress_cb)(a->progress_arg,
2698 GOT_STATUS_MISSING, path2);
2700 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2701 path2) == -1)
2702 return got_error_from_errno("asprintf");
2704 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2705 repo);
2706 if (err)
2707 goto done;
2709 if (status == GOT_STATUS_DELETE) {
2710 err = (*a->progress_cb)(a->progress_arg,
2711 GOT_STATUS_MERGE, path2);
2712 goto done;
2714 if (status != GOT_STATUS_NO_CHANGE &&
2715 status != GOT_STATUS_MODIFY &&
2716 status != GOT_STATUS_CONFLICT &&
2717 status != GOT_STATUS_ADD) {
2718 err = (*a->progress_cb)(a->progress_arg, status, path2);
2719 goto done;
2722 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2723 err = merge_symlink(a->worktree, blob1,
2724 ondisk_path, path2, sb.st_mode, a->label_orig,
2725 blob2, a->commit_id2, repo, a->progress_cb,
2726 a->progress_arg);
2727 } else {
2728 err = merge_blob(&local_changes_subsumed, a->worktree,
2729 blob1, ondisk_path, path2, sb.st_mode,
2730 a->label_orig, blob2, a->commit_id2, repo,
2731 a->progress_cb, a->progress_arg);
2733 } else if (blob1) {
2734 ie = got_fileindex_entry_get(a->fileindex, path1,
2735 strlen(path1));
2736 if (ie == NULL)
2737 return (*a->progress_cb)(a->progress_arg,
2738 GOT_STATUS_MISSING, path1);
2740 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2741 path1) == -1)
2742 return got_error_from_errno("asprintf");
2744 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2745 repo);
2746 if (err)
2747 goto done;
2749 switch (status) {
2750 case GOT_STATUS_NO_CHANGE:
2751 err = (*a->progress_cb)(a->progress_arg,
2752 GOT_STATUS_DELETE, path1);
2753 if (err)
2754 goto done;
2755 err = remove_ondisk_file(a->worktree->root_path, path1);
2756 if (err)
2757 goto done;
2758 if (ie)
2759 got_fileindex_entry_mark_deleted_from_disk(ie);
2760 break;
2761 case GOT_STATUS_DELETE:
2762 case GOT_STATUS_MISSING:
2763 err = (*a->progress_cb)(a->progress_arg,
2764 GOT_STATUS_DELETE, path1);
2765 if (err)
2766 goto done;
2767 if (ie)
2768 got_fileindex_entry_mark_deleted_from_disk(ie);
2769 break;
2770 case GOT_STATUS_ADD:
2771 case GOT_STATUS_MODIFY:
2772 case GOT_STATUS_CONFLICT:
2773 err = (*a->progress_cb)(a->progress_arg,
2774 GOT_STATUS_CANNOT_DELETE, path1);
2775 if (err)
2776 goto done;
2777 break;
2778 case GOT_STATUS_OBSTRUCTED:
2779 err = (*a->progress_cb)(a->progress_arg, status, path1);
2780 if (err)
2781 goto done;
2782 break;
2783 default:
2784 break;
2786 } else if (blob2) {
2787 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2788 path2) == -1)
2789 return got_error_from_errno("asprintf");
2790 ie = got_fileindex_entry_get(a->fileindex, path2,
2791 strlen(path2));
2792 if (ie) {
2793 err = get_file_status(&status, &sb, ie, ondisk_path,
2794 -1, NULL, repo);
2795 if (err)
2796 goto done;
2797 if (status != GOT_STATUS_NO_CHANGE &&
2798 status != GOT_STATUS_MODIFY &&
2799 status != GOT_STATUS_CONFLICT &&
2800 status != GOT_STATUS_ADD) {
2801 err = (*a->progress_cb)(a->progress_arg,
2802 status, path2);
2803 goto done;
2805 if (S_ISLNK(mode2)) {
2806 err = merge_symlink(a->worktree, NULL,
2807 ondisk_path, path2, sb.st_mode,
2808 a->label_orig, blob2, a->commit_id2,
2809 repo, a->progress_cb, a->progress_arg);
2810 if (err)
2811 goto done;
2812 } else {
2813 err = merge_blob(&local_changes_subsumed,
2814 a->worktree, NULL, ondisk_path, path2,
2815 sb.st_mode, a->label_orig, blob2,
2816 a->commit_id2, repo, a->progress_cb,
2817 a->progress_arg);
2818 if (err)
2819 goto done;
2821 if (status == GOT_STATUS_DELETE) {
2822 err = got_fileindex_entry_update(ie,
2823 ondisk_path, blob2->id.sha1,
2824 a->worktree->base_commit_id->sha1, 0);
2825 if (err)
2826 goto done;
2828 } else {
2829 int is_bad_symlink = 0;
2830 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2831 if (S_ISLNK(mode2)) {
2832 err = install_symlink(&is_bad_symlink,
2833 a->worktree, ondisk_path, path2, blob2, 0,
2834 0, 1, repo, a->progress_cb, a->progress_arg);
2835 } else {
2836 err = install_blob(a->worktree, ondisk_path, path2,
2837 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2838 a->progress_cb, a->progress_arg);
2840 if (err)
2841 goto done;
2842 err = got_fileindex_entry_alloc(&ie, path2);
2843 if (err)
2844 goto done;
2845 err = got_fileindex_entry_update(ie, ondisk_path,
2846 NULL, NULL, 1);
2847 if (err) {
2848 got_fileindex_entry_free(ie);
2849 goto done;
2851 err = got_fileindex_entry_add(a->fileindex, ie);
2852 if (err) {
2853 got_fileindex_entry_free(ie);
2854 goto done;
2856 if (is_bad_symlink) {
2857 err = got_fileindex_entry_filetype_set(ie,
2858 GOT_FILEIDX_MODE_BAD_SYMLINK);
2859 if (err)
2860 goto done;
2864 done:
2865 free(ondisk_path);
2866 return err;
2869 struct check_merge_ok_arg {
2870 struct got_worktree *worktree;
2871 struct got_repository *repo;
2874 static const struct got_error *
2875 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2877 const struct got_error *err = NULL;
2878 struct check_merge_ok_arg *a = arg;
2879 unsigned char status;
2880 struct stat sb;
2881 char *ondisk_path;
2883 /* Reject merges into a work tree with mixed base commits. */
2884 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2885 SHA1_DIGEST_LENGTH))
2886 return got_error(GOT_ERR_MIXED_COMMITS);
2888 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2889 == -1)
2890 return got_error_from_errno("asprintf");
2892 /* Reject merges into a work tree with conflicted files. */
2893 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2894 if (err)
2895 return err;
2896 if (status == GOT_STATUS_CONFLICT)
2897 return got_error(GOT_ERR_CONFLICTS);
2899 return NULL;
2902 static const struct got_error *
2903 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2904 const char *fileindex_path, struct got_object_id *commit_id1,
2905 struct got_object_id *commit_id2, struct got_repository *repo,
2906 got_worktree_checkout_cb progress_cb, void *progress_arg,
2907 got_cancel_cb cancel_cb, void *cancel_arg)
2909 const struct got_error *err = NULL, *sync_err;
2910 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2911 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2912 struct merge_file_cb_arg arg;
2913 char *label_orig = NULL;
2915 if (commit_id1) {
2916 char *id_str;
2918 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2919 worktree->path_prefix);
2920 if (err)
2921 goto done;
2923 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2924 if (err)
2925 goto done;
2927 err = got_object_id_str(&id_str, commit_id1);
2928 if (err)
2929 goto done;
2931 if (asprintf(&label_orig, "%s: commit %s",
2932 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2933 err = got_error_from_errno("asprintf");
2934 free(id_str);
2935 goto done;
2937 free(id_str);
2940 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2941 worktree->path_prefix);
2942 if (err)
2943 goto done;
2945 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2946 if (err)
2947 goto done;
2949 arg.worktree = worktree;
2950 arg.fileindex = fileindex;
2951 arg.progress_cb = progress_cb;
2952 arg.progress_arg = progress_arg;
2953 arg.cancel_cb = cancel_cb;
2954 arg.cancel_arg = cancel_arg;
2955 arg.label_orig = label_orig;
2956 arg.commit_id2 = commit_id2;
2957 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2958 sync_err = sync_fileindex(fileindex, fileindex_path);
2959 if (sync_err && err == NULL)
2960 err = sync_err;
2961 done:
2962 if (tree1)
2963 got_object_tree_close(tree1);
2964 if (tree2)
2965 got_object_tree_close(tree2);
2966 free(label_orig);
2967 return err;
2970 const struct got_error *
2971 got_worktree_merge_files(struct got_worktree *worktree,
2972 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2973 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2974 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2976 const struct got_error *err, *unlockerr;
2977 char *fileindex_path = NULL;
2978 struct got_fileindex *fileindex = NULL;
2979 struct check_merge_ok_arg mok_arg;
2981 err = lock_worktree(worktree, LOCK_EX);
2982 if (err)
2983 return err;
2985 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2986 if (err)
2987 goto done;
2989 mok_arg.worktree = worktree;
2990 mok_arg.repo = repo;
2991 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2992 &mok_arg);
2993 if (err)
2994 goto done;
2996 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2997 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2998 done:
2999 if (fileindex)
3000 got_fileindex_free(fileindex);
3001 free(fileindex_path);
3002 unlockerr = lock_worktree(worktree, LOCK_SH);
3003 if (unlockerr && err == NULL)
3004 err = unlockerr;
3005 return err;
3008 struct diff_dir_cb_arg {
3009 struct got_fileindex *fileindex;
3010 struct got_worktree *worktree;
3011 const char *status_path;
3012 size_t status_path_len;
3013 struct got_repository *repo;
3014 got_worktree_status_cb status_cb;
3015 void *status_arg;
3016 got_cancel_cb cancel_cb;
3017 void *cancel_arg;
3018 /* A pathlist containing per-directory pathlists of ignore patterns. */
3019 struct got_pathlist_head ignores;
3020 int report_unchanged;
3021 int no_ignores;
3024 static const struct got_error *
3025 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3026 int dirfd, const char *de_name,
3027 got_worktree_status_cb status_cb, void *status_arg,
3028 struct got_repository *repo, int report_unchanged)
3030 const struct got_error *err = NULL;
3031 unsigned char status = GOT_STATUS_NO_CHANGE;
3032 unsigned char staged_status = get_staged_status(ie);
3033 struct stat sb;
3034 struct got_object_id blob_id, commit_id, staged_blob_id;
3035 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3036 struct got_object_id *staged_blob_idp = NULL;
3038 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3039 if (err)
3040 return err;
3042 if (status == GOT_STATUS_NO_CHANGE &&
3043 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3044 return NULL;
3046 if (got_fileindex_entry_has_blob(ie)) {
3047 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3048 blob_idp = &blob_id;
3050 if (got_fileindex_entry_has_commit(ie)) {
3051 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3052 commit_idp = &commit_id;
3054 if (staged_status == GOT_STATUS_ADD ||
3055 staged_status == GOT_STATUS_MODIFY) {
3056 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3057 SHA1_DIGEST_LENGTH);
3058 staged_blob_idp = &staged_blob_id;
3061 return (*status_cb)(status_arg, status, staged_status,
3062 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3065 static const struct got_error *
3066 status_old_new(void *arg, struct got_fileindex_entry *ie,
3067 struct dirent *de, const char *parent_path, int dirfd)
3069 const struct got_error *err = NULL;
3070 struct diff_dir_cb_arg *a = arg;
3071 char *abspath;
3073 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3074 return got_error(GOT_ERR_CANCELLED);
3076 if (got_path_cmp(parent_path, a->status_path,
3077 strlen(parent_path), a->status_path_len) != 0 &&
3078 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3079 return NULL;
3081 if (parent_path[0]) {
3082 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3083 parent_path, de->d_name) == -1)
3084 return got_error_from_errno("asprintf");
3085 } else {
3086 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3087 de->d_name) == -1)
3088 return got_error_from_errno("asprintf");
3091 err = report_file_status(ie, abspath, dirfd, de->d_name,
3092 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3093 free(abspath);
3094 return err;
3097 static const struct got_error *
3098 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3100 struct diff_dir_cb_arg *a = arg;
3101 struct got_object_id blob_id, commit_id;
3102 unsigned char status;
3104 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3105 return got_error(GOT_ERR_CANCELLED);
3107 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3108 return NULL;
3110 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3111 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3112 if (got_fileindex_entry_has_file_on_disk(ie))
3113 status = GOT_STATUS_MISSING;
3114 else
3115 status = GOT_STATUS_DELETE;
3116 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3117 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3120 void
3121 free_ignorelist(struct got_pathlist_head *ignorelist)
3123 struct got_pathlist_entry *pe;
3125 TAILQ_FOREACH(pe, ignorelist, entry)
3126 free((char *)pe->path);
3127 got_pathlist_free(ignorelist);
3130 void
3131 free_ignores(struct got_pathlist_head *ignores)
3133 struct got_pathlist_entry *pe;
3135 TAILQ_FOREACH(pe, ignores, entry) {
3136 struct got_pathlist_head *ignorelist = pe->data;
3137 free_ignorelist(ignorelist);
3138 free((char *)pe->path);
3140 got_pathlist_free(ignores);
3143 static const struct got_error *
3144 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3146 const struct got_error *err = NULL;
3147 struct got_pathlist_entry *pe = NULL;
3148 struct got_pathlist_head *ignorelist;
3149 char *line = NULL, *pattern, *dirpath = NULL;
3150 size_t linesize = 0;
3151 ssize_t linelen;
3153 ignorelist = calloc(1, sizeof(*ignorelist));
3154 if (ignorelist == NULL)
3155 return got_error_from_errno("calloc");
3156 TAILQ_INIT(ignorelist);
3158 while ((linelen = getline(&line, &linesize, f)) != -1) {
3159 if (linelen > 0 && line[linelen - 1] == '\n')
3160 line[linelen - 1] = '\0';
3162 /* Git's ignores may contain comments. */
3163 if (line[0] == '#')
3164 continue;
3166 /* Git's negated patterns are not (yet?) supported. */
3167 if (line[0] == '!')
3168 continue;
3170 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3171 line) == -1) {
3172 err = got_error_from_errno("asprintf");
3173 goto done;
3175 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3176 if (err)
3177 goto done;
3179 if (ferror(f)) {
3180 err = got_error_from_errno("getline");
3181 goto done;
3184 dirpath = strdup(path);
3185 if (dirpath == NULL) {
3186 err = got_error_from_errno("strdup");
3187 goto done;
3189 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3190 done:
3191 free(line);
3192 if (err || pe == NULL) {
3193 free(dirpath);
3194 free_ignorelist(ignorelist);
3196 return err;
3199 int
3200 match_ignores(struct got_pathlist_head *ignores, const char *path)
3202 struct got_pathlist_entry *pe;
3204 /* Handle patterns which match in all directories. */
3205 TAILQ_FOREACH(pe, ignores, entry) {
3206 struct got_pathlist_head *ignorelist = pe->data;
3207 struct got_pathlist_entry *pi;
3209 TAILQ_FOREACH(pi, ignorelist, entry) {
3210 const char *p, *pattern = pi->path;
3212 if (strncmp(pattern, "**/", 3) != 0)
3213 continue;
3214 pattern += 3;
3215 p = path;
3216 while (*p) {
3217 if (fnmatch(pattern, p,
3218 FNM_PATHNAME | FNM_LEADING_DIR)) {
3219 /* Retry in next directory. */
3220 while (*p && *p != '/')
3221 p++;
3222 while (*p == '/')
3223 p++;
3224 continue;
3226 return 1;
3232 * The ignores pathlist contains ignore lists from children before
3233 * parents, so we can find the most specific ignorelist by walking
3234 * ignores backwards.
3236 pe = TAILQ_LAST(ignores, got_pathlist_head);
3237 while (pe) {
3238 if (got_path_is_child(path, pe->path, pe->path_len)) {
3239 struct got_pathlist_head *ignorelist = pe->data;
3240 struct got_pathlist_entry *pi;
3241 TAILQ_FOREACH(pi, ignorelist, entry) {
3242 const char *pattern = pi->path;
3243 int flags = FNM_LEADING_DIR;
3244 if (strstr(pattern, "/**/") == NULL)
3245 flags |= FNM_PATHNAME;
3246 if (fnmatch(pattern, path, flags))
3247 continue;
3248 return 1;
3251 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3254 return 0;
3257 static const struct got_error *
3258 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3259 const char *path, int dirfd, const char *ignores_filename)
3261 const struct got_error *err = NULL;
3262 char *ignorespath;
3263 int fd = -1;
3264 FILE *ignoresfile = NULL;
3266 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3267 path[0] ? "/" : "", ignores_filename) == -1)
3268 return got_error_from_errno("asprintf");
3270 if (dirfd != -1) {
3271 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3272 if (fd == -1) {
3273 if (errno != ENOENT && errno != EACCES)
3274 err = got_error_from_errno2("openat",
3275 ignorespath);
3276 } else {
3277 ignoresfile = fdopen(fd, "r");
3278 if (ignoresfile == NULL)
3279 err = got_error_from_errno2("fdopen",
3280 ignorespath);
3281 else {
3282 fd = -1;
3283 err = read_ignores(ignores, path, ignoresfile);
3286 } else {
3287 ignoresfile = fopen(ignorespath, "r");
3288 if (ignoresfile == NULL) {
3289 if (errno != ENOENT && errno != EACCES)
3290 err = got_error_from_errno2("fopen",
3291 ignorespath);
3292 } else
3293 err = read_ignores(ignores, path, ignoresfile);
3296 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3297 err = got_error_from_errno2("fclose", path);
3298 if (fd != -1 && close(fd) == -1 && err == NULL)
3299 err = got_error_from_errno2("close", path);
3300 free(ignorespath);
3301 return err;
3304 static const struct got_error *
3305 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3307 const struct got_error *err = NULL;
3308 struct diff_dir_cb_arg *a = arg;
3309 char *path = NULL;
3311 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3312 return got_error(GOT_ERR_CANCELLED);
3314 if (parent_path[0]) {
3315 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3316 return got_error_from_errno("asprintf");
3317 } else {
3318 path = de->d_name;
3321 if (de->d_type != DT_DIR &&
3322 got_path_is_child(path, a->status_path, a->status_path_len)
3323 && !match_ignores(&a->ignores, path))
3324 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3325 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3326 if (parent_path[0])
3327 free(path);
3328 return err;
3331 static const struct got_error *
3332 status_traverse(void *arg, const char *path, int dirfd)
3334 const struct got_error *err = NULL;
3335 struct diff_dir_cb_arg *a = arg;
3337 if (a->no_ignores)
3338 return NULL;
3340 err = add_ignores(&a->ignores, a->worktree->root_path,
3341 path, dirfd, ".cvsignore");
3342 if (err)
3343 return err;
3345 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3346 dirfd, ".gitignore");
3348 return err;
3351 static const struct got_error *
3352 report_single_file_status(const char *path, const char *ondisk_path,
3353 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3354 void *status_arg, struct got_repository *repo, int report_unchanged)
3356 struct got_fileindex_entry *ie;
3357 struct stat sb;
3359 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3360 if (ie)
3361 return report_file_status(ie, ondisk_path, -1, NULL,
3362 status_cb, status_arg, repo, report_unchanged);
3364 if (lstat(ondisk_path, &sb) == -1) {
3365 if (errno != ENOENT)
3366 return got_error_from_errno2("lstat", ondisk_path);
3367 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3368 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3369 return NULL;
3372 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3373 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3374 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3376 return NULL;
3379 static const struct got_error *
3380 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3381 const char *root_path, const char *path)
3383 const struct got_error *err;
3384 char *parent_path, *next_parent_path = NULL;
3386 err = add_ignores(ignores, root_path, "", -1,
3387 ".cvsignore");
3388 if (err)
3389 return err;
3391 err = add_ignores(ignores, root_path, "", -1,
3392 ".gitignore");
3393 if (err)
3394 return err;
3396 err = got_path_dirname(&parent_path, path);
3397 if (err) {
3398 if (err->code == GOT_ERR_BAD_PATH)
3399 return NULL; /* cannot traverse parent */
3400 return err;
3402 for (;;) {
3403 err = add_ignores(ignores, root_path, parent_path, -1,
3404 ".cvsignore");
3405 if (err)
3406 break;
3407 err = add_ignores(ignores, root_path, parent_path, -1,
3408 ".gitignore");
3409 if (err)
3410 break;
3411 err = got_path_dirname(&next_parent_path, parent_path);
3412 if (err) {
3413 if (err->code == GOT_ERR_BAD_PATH)
3414 err = NULL; /* traversed everything */
3415 break;
3417 free(parent_path);
3418 parent_path = next_parent_path;
3419 next_parent_path = NULL;
3422 free(parent_path);
3423 free(next_parent_path);
3424 return err;
3427 static const struct got_error *
3428 worktree_status(struct got_worktree *worktree, const char *path,
3429 struct got_fileindex *fileindex, struct got_repository *repo,
3430 got_worktree_status_cb status_cb, void *status_arg,
3431 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3432 int report_unchanged)
3434 const struct got_error *err = NULL;
3435 int fd = -1;
3436 struct got_fileindex_diff_dir_cb fdiff_cb;
3437 struct diff_dir_cb_arg arg;
3438 char *ondisk_path = NULL;
3440 TAILQ_INIT(&arg.ignores);
3442 if (asprintf(&ondisk_path, "%s%s%s",
3443 worktree->root_path, path[0] ? "/" : "", path) == -1)
3444 return got_error_from_errno("asprintf");
3446 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3447 if (fd == -1) {
3448 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3449 errno != ELOOP)
3450 err = got_error_from_errno2("open", ondisk_path);
3451 else
3452 err = report_single_file_status(path, ondisk_path,
3453 fileindex, status_cb, status_arg, repo,
3454 report_unchanged);
3455 } else {
3456 fdiff_cb.diff_old_new = status_old_new;
3457 fdiff_cb.diff_old = status_old;
3458 fdiff_cb.diff_new = status_new;
3459 fdiff_cb.diff_traverse = status_traverse;
3460 arg.fileindex = fileindex;
3461 arg.worktree = worktree;
3462 arg.status_path = path;
3463 arg.status_path_len = strlen(path);
3464 arg.repo = repo;
3465 arg.status_cb = status_cb;
3466 arg.status_arg = status_arg;
3467 arg.cancel_cb = cancel_cb;
3468 arg.cancel_arg = cancel_arg;
3469 arg.report_unchanged = report_unchanged;
3470 arg.no_ignores = no_ignores;
3471 if (!no_ignores) {
3472 err = add_ignores_from_parent_paths(&arg.ignores,
3473 worktree->root_path, path);
3474 if (err)
3475 goto done;
3477 err = got_fileindex_diff_dir(fileindex, fd,
3478 worktree->root_path, path, repo, &fdiff_cb, &arg);
3480 done:
3481 free_ignores(&arg.ignores);
3482 if (fd != -1 && close(fd) != 0 && err == NULL)
3483 err = got_error_from_errno("close");
3484 free(ondisk_path);
3485 return err;
3488 const struct got_error *
3489 got_worktree_status(struct got_worktree *worktree,
3490 struct got_pathlist_head *paths, struct got_repository *repo,
3491 got_worktree_status_cb status_cb, void *status_arg,
3492 got_cancel_cb cancel_cb, void *cancel_arg)
3494 const struct got_error *err = NULL;
3495 char *fileindex_path = NULL;
3496 struct got_fileindex *fileindex = NULL;
3497 struct got_pathlist_entry *pe;
3499 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3500 if (err)
3501 return err;
3503 TAILQ_FOREACH(pe, paths, entry) {
3504 err = worktree_status(worktree, pe->path, fileindex, repo,
3505 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3506 if (err)
3507 break;
3509 free(fileindex_path);
3510 got_fileindex_free(fileindex);
3511 return err;
3514 const struct got_error *
3515 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3516 const char *arg)
3518 const struct got_error *err = NULL;
3519 char *resolved = NULL, *cwd = NULL, *path = NULL;
3520 size_t len;
3521 struct stat sb;
3523 *wt_path = NULL;
3525 cwd = getcwd(NULL, 0);
3526 if (cwd == NULL)
3527 return got_error_from_errno("getcwd");
3529 if (lstat(arg, &sb) == -1) {
3530 if (errno != ENOENT) {
3531 err = got_error_from_errno2("lstat", arg);
3532 goto done;
3535 if (S_ISLNK(sb.st_mode)) {
3537 * We cannot use realpath(3) with symlinks since we want to
3538 * operate on the symlink itself.
3539 * But we can make the path absolute, assuming it is relative
3540 * to the current working directory, and then canonicalize it.
3542 char *abspath = NULL;
3543 char canonpath[PATH_MAX];
3544 if (!got_path_is_absolute(arg)) {
3545 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3546 err = got_error_from_errno("asprintf");
3547 goto done;
3551 err = got_canonpath(abspath ? abspath : arg, canonpath,
3552 sizeof(canonpath));
3553 if (err)
3554 goto done;
3555 resolved = strdup(canonpath);
3556 if (resolved == NULL) {
3557 err = got_error_from_errno("strdup");
3558 goto done;
3560 } else {
3561 resolved = realpath(arg, NULL);
3562 if (resolved == NULL) {
3563 if (errno != ENOENT) {
3564 err = got_error_from_errno2("realpath", arg);
3565 goto done;
3567 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3568 err = got_error_from_errno("asprintf");
3569 goto done;
3574 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3575 strlen(got_worktree_get_root_path(worktree)))) {
3576 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3577 goto done;
3580 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3581 err = got_path_skip_common_ancestor(&path,
3582 got_worktree_get_root_path(worktree), resolved);
3583 if (err)
3584 goto done;
3585 } else {
3586 path = strdup("");
3587 if (path == NULL) {
3588 err = got_error_from_errno("strdup");
3589 goto done;
3593 /* XXX status walk can't deal with trailing slash! */
3594 len = strlen(path);
3595 while (len > 0 && path[len - 1] == '/') {
3596 path[len - 1] = '\0';
3597 len--;
3599 done:
3600 free(resolved);
3601 free(cwd);
3602 if (err == NULL)
3603 *wt_path = path;
3604 else
3605 free(path);
3606 return err;
3609 struct schedule_addition_args {
3610 struct got_worktree *worktree;
3611 struct got_fileindex *fileindex;
3612 got_worktree_checkout_cb progress_cb;
3613 void *progress_arg;
3614 struct got_repository *repo;
3617 static const struct got_error *
3618 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3619 const char *relpath, struct got_object_id *blob_id,
3620 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3621 int dirfd, const char *de_name)
3623 struct schedule_addition_args *a = arg;
3624 const struct got_error *err = NULL;
3625 struct got_fileindex_entry *ie;
3626 struct stat sb;
3627 char *ondisk_path;
3629 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3630 relpath) == -1)
3631 return got_error_from_errno("asprintf");
3633 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3634 if (ie) {
3635 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3636 de_name, a->repo);
3637 if (err)
3638 goto done;
3639 /* Re-adding an existing entry is a no-op. */
3640 if (status == GOT_STATUS_ADD)
3641 goto done;
3642 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3643 if (err)
3644 goto done;
3647 if (status != GOT_STATUS_UNVERSIONED) {
3648 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3649 goto done;
3652 err = got_fileindex_entry_alloc(&ie, relpath);
3653 if (err)
3654 goto done;
3655 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3656 if (err) {
3657 got_fileindex_entry_free(ie);
3658 goto done;
3660 err = got_fileindex_entry_add(a->fileindex, ie);
3661 if (err) {
3662 got_fileindex_entry_free(ie);
3663 goto done;
3665 done:
3666 free(ondisk_path);
3667 if (err)
3668 return err;
3669 if (status == GOT_STATUS_ADD)
3670 return NULL;
3671 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3674 const struct got_error *
3675 got_worktree_schedule_add(struct got_worktree *worktree,
3676 struct got_pathlist_head *paths,
3677 got_worktree_checkout_cb progress_cb, void *progress_arg,
3678 struct got_repository *repo, int no_ignores)
3680 struct got_fileindex *fileindex = NULL;
3681 char *fileindex_path = NULL;
3682 const struct got_error *err = NULL, *sync_err, *unlockerr;
3683 struct got_pathlist_entry *pe;
3684 struct schedule_addition_args saa;
3686 err = lock_worktree(worktree, LOCK_EX);
3687 if (err)
3688 return err;
3690 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3691 if (err)
3692 goto done;
3694 saa.worktree = worktree;
3695 saa.fileindex = fileindex;
3696 saa.progress_cb = progress_cb;
3697 saa.progress_arg = progress_arg;
3698 saa.repo = repo;
3700 TAILQ_FOREACH(pe, paths, entry) {
3701 err = worktree_status(worktree, pe->path, fileindex, repo,
3702 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3703 if (err)
3704 break;
3706 sync_err = sync_fileindex(fileindex, fileindex_path);
3707 if (sync_err && err == NULL)
3708 err = sync_err;
3709 done:
3710 free(fileindex_path);
3711 if (fileindex)
3712 got_fileindex_free(fileindex);
3713 unlockerr = lock_worktree(worktree, LOCK_SH);
3714 if (unlockerr && err == NULL)
3715 err = unlockerr;
3716 return err;
3719 struct schedule_deletion_args {
3720 struct got_worktree *worktree;
3721 struct got_fileindex *fileindex;
3722 got_worktree_delete_cb progress_cb;
3723 void *progress_arg;
3724 struct got_repository *repo;
3725 int delete_local_mods;
3726 int keep_on_disk;
3729 static const struct got_error *
3730 schedule_for_deletion(void *arg, unsigned char status,
3731 unsigned char staged_status, const char *relpath,
3732 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3733 struct got_object_id *commit_id, int dirfd, const char *de_name)
3735 struct schedule_deletion_args *a = arg;
3736 const struct got_error *err = NULL;
3737 struct got_fileindex_entry *ie = NULL;
3738 struct stat sb;
3739 char *ondisk_path, *parent = NULL;
3741 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3742 if (ie == NULL)
3743 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3745 staged_status = get_staged_status(ie);
3746 if (staged_status != GOT_STATUS_NO_CHANGE) {
3747 if (staged_status == GOT_STATUS_DELETE)
3748 return NULL;
3749 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3752 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3753 relpath) == -1)
3754 return got_error_from_errno("asprintf");
3756 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3757 a->repo);
3758 if (err)
3759 goto done;
3761 if (status != GOT_STATUS_NO_CHANGE) {
3762 if (status == GOT_STATUS_DELETE)
3763 goto done;
3764 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3765 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3766 goto done;
3768 if (status != GOT_STATUS_MODIFY &&
3769 status != GOT_STATUS_MISSING) {
3770 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3771 goto done;
3775 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3776 if (dirfd != -1) {
3777 if (unlinkat(dirfd, de_name, 0) != 0) {
3778 err = got_error_from_errno2("unlinkat",
3779 ondisk_path);
3780 goto done;
3782 } else if (unlink(ondisk_path) != 0) {
3783 err = got_error_from_errno2("unlink", ondisk_path);
3784 goto done;
3787 parent = dirname(ondisk_path);
3789 if (parent == NULL) {
3790 err = got_error_from_errno2("dirname", ondisk_path);
3791 goto done;
3793 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3794 if (rmdir(parent) == -1) {
3795 if (errno != ENOTEMPTY)
3796 err = got_error_from_errno2("rmdir",
3797 parent);
3798 break;
3800 parent = dirname(parent);
3801 if (parent == NULL) {
3802 err = got_error_from_errno2("dirname", parent);
3803 goto done;
3808 got_fileindex_entry_mark_deleted_from_disk(ie);
3809 done:
3810 free(ondisk_path);
3811 if (err)
3812 return err;
3813 if (status == GOT_STATUS_DELETE)
3814 return NULL;
3815 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3816 staged_status, relpath);
3819 const struct got_error *
3820 got_worktree_schedule_delete(struct got_worktree *worktree,
3821 struct got_pathlist_head *paths, int delete_local_mods,
3822 got_worktree_delete_cb progress_cb, void *progress_arg,
3823 struct got_repository *repo, int keep_on_disk)
3825 struct got_fileindex *fileindex = NULL;
3826 char *fileindex_path = NULL;
3827 const struct got_error *err = NULL, *sync_err, *unlockerr;
3828 struct got_pathlist_entry *pe;
3829 struct schedule_deletion_args sda;
3831 err = lock_worktree(worktree, LOCK_EX);
3832 if (err)
3833 return err;
3835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3836 if (err)
3837 goto done;
3839 sda.worktree = worktree;
3840 sda.fileindex = fileindex;
3841 sda.progress_cb = progress_cb;
3842 sda.progress_arg = progress_arg;
3843 sda.repo = repo;
3844 sda.delete_local_mods = delete_local_mods;
3845 sda.keep_on_disk = keep_on_disk;
3847 TAILQ_FOREACH(pe, paths, entry) {
3848 err = worktree_status(worktree, pe->path, fileindex, repo,
3849 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3850 if (err)
3851 break;
3853 sync_err = sync_fileindex(fileindex, fileindex_path);
3854 if (sync_err && err == NULL)
3855 err = sync_err;
3856 done:
3857 free(fileindex_path);
3858 if (fileindex)
3859 got_fileindex_free(fileindex);
3860 unlockerr = lock_worktree(worktree, LOCK_SH);
3861 if (unlockerr && err == NULL)
3862 err = unlockerr;
3863 return err;
3866 static const struct got_error *
3867 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3869 const struct got_error *err = NULL;
3870 char *line = NULL;
3871 size_t linesize = 0, n;
3872 ssize_t linelen;
3874 linelen = getline(&line, &linesize, infile);
3875 if (linelen == -1) {
3876 if (ferror(infile)) {
3877 err = got_error_from_errno("getline");
3878 goto done;
3880 return NULL;
3882 if (outfile) {
3883 n = fwrite(line, 1, linelen, outfile);
3884 if (n != linelen) {
3885 err = got_ferror(outfile, GOT_ERR_IO);
3886 goto done;
3889 if (rejectfile) {
3890 n = fwrite(line, 1, linelen, rejectfile);
3891 if (n != linelen)
3892 err = got_ferror(outfile, GOT_ERR_IO);
3894 done:
3895 free(line);
3896 return err;
3899 static const struct got_error *
3900 skip_one_line(FILE *f)
3902 char *line = NULL;
3903 size_t linesize = 0;
3904 ssize_t linelen;
3906 linelen = getline(&line, &linesize, f);
3907 if (linelen == -1) {
3908 if (ferror(f))
3909 return got_error_from_errno("getline");
3910 return NULL;
3912 free(line);
3913 return NULL;
3916 static const struct got_error *
3917 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3918 int start_old, int end_old, int start_new, int end_new,
3919 FILE *outfile, FILE *rejectfile)
3921 const struct got_error *err;
3923 /* Copy old file's lines leading up to patch. */
3924 while (!feof(f1) && *line_cur1 < start_old) {
3925 err = copy_one_line(f1, outfile, NULL);
3926 if (err)
3927 return err;
3928 (*line_cur1)++;
3930 /* Skip new file's lines leading up to patch. */
3931 while (!feof(f2) && *line_cur2 < start_new) {
3932 if (rejectfile)
3933 err = copy_one_line(f2, NULL, rejectfile);
3934 else
3935 err = skip_one_line(f2);
3936 if (err)
3937 return err;
3938 (*line_cur2)++;
3940 /* Copy patched lines. */
3941 while (!feof(f2) && *line_cur2 <= end_new) {
3942 err = copy_one_line(f2, outfile, NULL);
3943 if (err)
3944 return err;
3945 (*line_cur2)++;
3947 /* Skip over old file's replaced lines. */
3948 while (!feof(f1) && *line_cur1 <= end_old) {
3949 if (rejectfile)
3950 err = copy_one_line(f1, NULL, rejectfile);
3951 else
3952 err = skip_one_line(f1);
3953 if (err)
3954 return err;
3955 (*line_cur1)++;
3958 return NULL;
3961 static const struct got_error *
3962 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3963 FILE *outfile, FILE *rejectfile)
3965 const struct got_error *err;
3967 if (outfile) {
3968 /* Copy old file's lines until EOF. */
3969 while (!feof(f1)) {
3970 err = copy_one_line(f1, outfile, NULL);
3971 if (err)
3972 return err;
3973 (*line_cur1)++;
3976 if (rejectfile) {
3977 /* Copy new file's lines until EOF. */
3978 while (!feof(f2)) {
3979 err = copy_one_line(f2, NULL, rejectfile);
3980 if (err)
3981 return err;
3982 (*line_cur2)++;
3986 return NULL;
3989 static const struct got_error *
3990 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3991 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3992 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3993 int *line_cur2, FILE *outfile, FILE *rejectfile,
3994 got_worktree_patch_cb patch_cb, void *patch_arg)
3996 const struct got_error *err = NULL;
3997 int start_old = change->cv.a;
3998 int end_old = change->cv.b;
3999 int start_new = change->cv.c;
4000 int end_new = change->cv.d;
4001 long pos1, pos2;
4002 FILE *hunkfile;
4004 *choice = GOT_PATCH_CHOICE_NONE;
4006 hunkfile = got_opentemp();
4007 if (hunkfile == NULL)
4008 return got_error_from_errno("got_opentemp");
4010 pos1 = ftell(f1);
4011 pos2 = ftell(f2);
4013 /* XXX TODO needs error checking */
4014 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4016 if (fseek(f1, pos1, SEEK_SET) == -1) {
4017 err = got_ferror(f1, GOT_ERR_IO);
4018 goto done;
4020 if (fseek(f2, pos2, SEEK_SET) == -1) {
4021 err = got_ferror(f1, GOT_ERR_IO);
4022 goto done;
4024 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4025 err = got_ferror(hunkfile, GOT_ERR_IO);
4026 goto done;
4029 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4030 hunkfile, n, nchanges);
4031 if (err)
4032 goto done;
4034 switch (*choice) {
4035 case GOT_PATCH_CHOICE_YES:
4036 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4037 end_old, start_new, end_new, outfile, rejectfile);
4038 break;
4039 case GOT_PATCH_CHOICE_NO:
4040 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4041 end_old, start_new, end_new, rejectfile, outfile);
4042 break;
4043 case GOT_PATCH_CHOICE_QUIT:
4044 break;
4045 default:
4046 err = got_error(GOT_ERR_PATCH_CHOICE);
4047 break;
4049 done:
4050 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4051 err = got_error_from_errno("fclose");
4052 return err;
4055 struct revert_file_args {
4056 struct got_worktree *worktree;
4057 struct got_fileindex *fileindex;
4058 got_worktree_checkout_cb progress_cb;
4059 void *progress_arg;
4060 got_worktree_patch_cb patch_cb;
4061 void *patch_arg;
4062 struct got_repository *repo;
4065 static const struct got_error *
4066 create_patched_content(char **path_outfile, int reverse_patch,
4067 struct got_object_id *blob_id, const char *path2,
4068 int dirfd2, const char *de_name2,
4069 const char *relpath, struct got_repository *repo,
4070 got_worktree_patch_cb patch_cb, void *patch_arg)
4072 const struct got_error *err;
4073 struct got_blob_object *blob = NULL;
4074 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4075 int fd2 = -1;
4076 char *path1 = NULL, *id_str = NULL;
4077 struct stat sb1, sb2;
4078 struct got_diff_changes *changes = NULL;
4079 struct got_diff_state *ds = NULL;
4080 struct got_diff_args *args = NULL;
4081 struct got_diff_change *change;
4082 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4083 int n = 0;
4085 *path_outfile = NULL;
4087 err = got_object_id_str(&id_str, blob_id);
4088 if (err)
4089 return err;
4091 if (dirfd2 != -1) {
4092 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4093 if (fd2 == -1) {
4094 err = got_error_from_errno2("openat", path2);
4095 goto done;
4097 } else {
4098 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4099 if (fd2 == -1) {
4100 err = got_error_from_errno2("open", path2);
4101 goto done;
4104 if (fstat(fd2, &sb2) == -1) {
4105 err = got_error_from_errno2("fstat", path2);
4106 goto done;
4109 f2 = fdopen(fd2, "r");
4110 if (f2 == NULL) {
4111 err = got_error_from_errno2("fdopen", path2);
4112 goto done;
4114 fd2 = -1;
4116 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4117 if (err)
4118 goto done;
4120 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4121 if (err)
4122 goto done;
4124 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4125 if (err)
4126 goto done;
4128 if (stat(path1, &sb1) == -1) {
4129 err = got_error_from_errno2("stat", path1);
4130 goto done;
4133 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4134 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4135 if (err)
4136 goto done;
4138 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4139 if (err)
4140 goto done;
4142 if (fseek(f1, 0L, SEEK_SET) == -1)
4143 return got_ferror(f1, GOT_ERR_IO);
4144 if (fseek(f2, 0L, SEEK_SET) == -1)
4145 return got_ferror(f2, GOT_ERR_IO);
4146 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4147 int choice;
4148 err = apply_or_reject_change(&choice, change, ++n,
4149 changes->nchanges, ds, args, diff_flags, relpath,
4150 f1, f2, &line_cur1, &line_cur2,
4151 reverse_patch ? NULL : outfile,
4152 reverse_patch ? outfile : NULL,
4153 patch_cb, patch_arg);
4154 if (err)
4155 goto done;
4156 if (choice == GOT_PATCH_CHOICE_YES)
4157 have_content = 1;
4158 else if (choice == GOT_PATCH_CHOICE_QUIT)
4159 break;
4161 if (have_content) {
4162 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4163 reverse_patch ? NULL : outfile,
4164 reverse_patch ? outfile : NULL);
4165 if (err)
4166 goto done;
4168 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4169 err = got_error_from_errno2("chmod", path2);
4170 goto done;
4173 done:
4174 free(id_str);
4175 if (blob)
4176 got_object_blob_close(blob);
4177 if (f1 && fclose(f1) == EOF && err == NULL)
4178 err = got_error_from_errno2("fclose", path1);
4179 if (f2 && fclose(f2) == EOF && err == NULL)
4180 err = got_error_from_errno2("fclose", path2);
4181 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4182 err = got_error_from_errno2("close", path2);
4183 if (outfile && fclose(outfile) == EOF && err == NULL)
4184 err = got_error_from_errno2("fclose", *path_outfile);
4185 if (path1 && unlink(path1) == -1 && err == NULL)
4186 err = got_error_from_errno2("unlink", path1);
4187 if (err || !have_content) {
4188 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4189 err = got_error_from_errno2("unlink", *path_outfile);
4190 free(*path_outfile);
4191 *path_outfile = NULL;
4193 free(args);
4194 if (ds) {
4195 got_diff_state_free(ds);
4196 free(ds);
4198 if (changes)
4199 got_diff_free_changes(changes);
4200 free(path1);
4201 return err;
4204 static const struct got_error *
4205 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4206 const char *relpath, struct got_object_id *blob_id,
4207 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4208 int dirfd, const char *de_name)
4210 struct revert_file_args *a = arg;
4211 const struct got_error *err = NULL;
4212 char *parent_path = NULL;
4213 struct got_fileindex_entry *ie;
4214 struct got_tree_object *tree = NULL;
4215 struct got_object_id *tree_id = NULL;
4216 const struct got_tree_entry *te = NULL;
4217 char *tree_path = NULL, *te_name;
4218 char *ondisk_path = NULL, *path_content = NULL;
4219 struct got_blob_object *blob = NULL;
4221 /* Reverting a staged deletion is a no-op. */
4222 if (status == GOT_STATUS_DELETE &&
4223 staged_status != GOT_STATUS_NO_CHANGE)
4224 return NULL;
4226 if (status == GOT_STATUS_UNVERSIONED)
4227 return (*a->progress_cb)(a->progress_arg,
4228 GOT_STATUS_UNVERSIONED, relpath);
4230 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4231 if (ie == NULL)
4232 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4234 /* Construct in-repository path of tree which contains this blob. */
4235 err = got_path_dirname(&parent_path, ie->path);
4236 if (err) {
4237 if (err->code != GOT_ERR_BAD_PATH)
4238 goto done;
4239 parent_path = strdup("/");
4240 if (parent_path == NULL) {
4241 err = got_error_from_errno("strdup");
4242 goto done;
4245 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4246 tree_path = strdup(parent_path);
4247 if (tree_path == NULL) {
4248 err = got_error_from_errno("strdup");
4249 goto done;
4251 } else {
4252 if (got_path_is_root_dir(parent_path)) {
4253 tree_path = strdup(a->worktree->path_prefix);
4254 if (tree_path == NULL) {
4255 err = got_error_from_errno("strdup");
4256 goto done;
4258 } else {
4259 if (asprintf(&tree_path, "%s/%s",
4260 a->worktree->path_prefix, parent_path) == -1) {
4261 err = got_error_from_errno("asprintf");
4262 goto done;
4267 err = got_object_id_by_path(&tree_id, a->repo,
4268 a->worktree->base_commit_id, tree_path);
4269 if (err) {
4270 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4271 (status == GOT_STATUS_ADD ||
4272 staged_status == GOT_STATUS_ADD)))
4273 goto done;
4274 } else {
4275 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4276 if (err)
4277 goto done;
4279 te_name = basename(ie->path);
4280 if (te_name == NULL) {
4281 err = got_error_from_errno2("basename", ie->path);
4282 goto done;
4285 te = got_object_tree_find_entry(tree, te_name);
4286 if (te == NULL && status != GOT_STATUS_ADD &&
4287 staged_status != GOT_STATUS_ADD) {
4288 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4289 goto done;
4293 switch (status) {
4294 case GOT_STATUS_ADD:
4295 if (a->patch_cb) {
4296 int choice = GOT_PATCH_CHOICE_NONE;
4297 err = (*a->patch_cb)(&choice, a->patch_arg,
4298 status, ie->path, NULL, 1, 1);
4299 if (err)
4300 goto done;
4301 if (choice != GOT_PATCH_CHOICE_YES)
4302 break;
4304 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4305 ie->path);
4306 if (err)
4307 goto done;
4308 got_fileindex_entry_remove(a->fileindex, ie);
4309 break;
4310 case GOT_STATUS_DELETE:
4311 if (a->patch_cb) {
4312 int choice = GOT_PATCH_CHOICE_NONE;
4313 err = (*a->patch_cb)(&choice, a->patch_arg,
4314 status, ie->path, NULL, 1, 1);
4315 if (err)
4316 goto done;
4317 if (choice != GOT_PATCH_CHOICE_YES)
4318 break;
4320 /* fall through */
4321 case GOT_STATUS_MODIFY:
4322 case GOT_STATUS_MODE_CHANGE:
4323 case GOT_STATUS_CONFLICT:
4324 case GOT_STATUS_MISSING: {
4325 struct got_object_id id;
4326 if (staged_status == GOT_STATUS_ADD ||
4327 staged_status == GOT_STATUS_MODIFY) {
4328 memcpy(id.sha1, ie->staged_blob_sha1,
4329 SHA1_DIGEST_LENGTH);
4330 } else
4331 memcpy(id.sha1, ie->blob_sha1,
4332 SHA1_DIGEST_LENGTH);
4333 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4334 if (err)
4335 goto done;
4337 if (asprintf(&ondisk_path, "%s/%s",
4338 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4339 err = got_error_from_errno("asprintf");
4340 goto done;
4343 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4344 status == GOT_STATUS_CONFLICT)) {
4345 err = create_patched_content(&path_content, 1, &id,
4346 ondisk_path, dirfd, de_name, ie->path, a->repo,
4347 a->patch_cb, a->patch_arg);
4348 if (err || path_content == NULL)
4349 break;
4350 if (rename(path_content, ondisk_path) == -1) {
4351 err = got_error_from_errno3("rename",
4352 path_content, ondisk_path);
4353 goto done;
4355 } else {
4356 int is_bad_symlink = 0;
4357 if (te && S_ISLNK(te->mode)) {
4358 err = install_symlink(&is_bad_symlink,
4359 a->worktree, ondisk_path, ie->path,
4360 blob, 0, 1, 0, a->repo,
4361 a->progress_cb, a->progress_arg);
4362 } else {
4363 err = install_blob(a->worktree, ondisk_path,
4364 ie->path,
4365 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4366 got_fileindex_perms_to_st(ie), blob,
4367 0, 1, 0, 0, a->repo,
4368 a->progress_cb, a->progress_arg);
4370 if (err)
4371 goto done;
4372 if (status == GOT_STATUS_DELETE ||
4373 status == GOT_STATUS_MODE_CHANGE) {
4374 err = got_fileindex_entry_update(ie,
4375 ondisk_path, blob->id.sha1,
4376 a->worktree->base_commit_id->sha1, 1);
4377 if (err)
4378 goto done;
4380 if (is_bad_symlink) {
4381 err = got_fileindex_entry_filetype_set(ie,
4382 GOT_FILEIDX_MODE_BAD_SYMLINK);
4383 if (err)
4384 goto done;
4387 break;
4389 default:
4390 break;
4392 done:
4393 free(ondisk_path);
4394 free(path_content);
4395 free(parent_path);
4396 free(tree_path);
4397 if (blob)
4398 got_object_blob_close(blob);
4399 if (tree)
4400 got_object_tree_close(tree);
4401 free(tree_id);
4402 return err;
4405 const struct got_error *
4406 got_worktree_revert(struct got_worktree *worktree,
4407 struct got_pathlist_head *paths,
4408 got_worktree_checkout_cb progress_cb, void *progress_arg,
4409 got_worktree_patch_cb patch_cb, void *patch_arg,
4410 struct got_repository *repo)
4412 struct got_fileindex *fileindex = NULL;
4413 char *fileindex_path = NULL;
4414 const struct got_error *err = NULL, *unlockerr = NULL;
4415 const struct got_error *sync_err = NULL;
4416 struct got_pathlist_entry *pe;
4417 struct revert_file_args rfa;
4419 err = lock_worktree(worktree, LOCK_EX);
4420 if (err)
4421 return err;
4423 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4424 if (err)
4425 goto done;
4427 rfa.worktree = worktree;
4428 rfa.fileindex = fileindex;
4429 rfa.progress_cb = progress_cb;
4430 rfa.progress_arg = progress_arg;
4431 rfa.patch_cb = patch_cb;
4432 rfa.patch_arg = patch_arg;
4433 rfa.repo = repo;
4434 TAILQ_FOREACH(pe, paths, entry) {
4435 err = worktree_status(worktree, pe->path, fileindex, repo,
4436 revert_file, &rfa, NULL, NULL, 0, 0);
4437 if (err)
4438 break;
4440 sync_err = sync_fileindex(fileindex, fileindex_path);
4441 if (sync_err && err == NULL)
4442 err = sync_err;
4443 done:
4444 free(fileindex_path);
4445 if (fileindex)
4446 got_fileindex_free(fileindex);
4447 unlockerr = lock_worktree(worktree, LOCK_SH);
4448 if (unlockerr && err == NULL)
4449 err = unlockerr;
4450 return err;
4453 static void
4454 free_commitable(struct got_commitable *ct)
4456 free(ct->path);
4457 free(ct->in_repo_path);
4458 free(ct->ondisk_path);
4459 free(ct->blob_id);
4460 free(ct->base_blob_id);
4461 free(ct->staged_blob_id);
4462 free(ct->base_commit_id);
4463 free(ct);
4466 struct collect_commitables_arg {
4467 struct got_pathlist_head *commitable_paths;
4468 struct got_repository *repo;
4469 struct got_worktree *worktree;
4470 int have_staged_files;
4473 static const struct got_error *
4474 collect_commitables(void *arg, unsigned char status,
4475 unsigned char staged_status, const char *relpath,
4476 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4477 struct got_object_id *commit_id, int dirfd, const char *de_name)
4479 struct collect_commitables_arg *a = arg;
4480 const struct got_error *err = NULL;
4481 struct got_commitable *ct = NULL;
4482 struct got_pathlist_entry *new = NULL;
4483 char *parent_path = NULL, *path = NULL;
4484 struct stat sb;
4486 if (a->have_staged_files) {
4487 if (staged_status != GOT_STATUS_MODIFY &&
4488 staged_status != GOT_STATUS_ADD &&
4489 staged_status != GOT_STATUS_DELETE)
4490 return NULL;
4491 } else {
4492 if (status == GOT_STATUS_CONFLICT)
4493 return got_error(GOT_ERR_COMMIT_CONFLICT);
4495 if (status != GOT_STATUS_MODIFY &&
4496 status != GOT_STATUS_MODE_CHANGE &&
4497 status != GOT_STATUS_ADD &&
4498 status != GOT_STATUS_DELETE)
4499 return NULL;
4502 if (asprintf(&path, "/%s", relpath) == -1) {
4503 err = got_error_from_errno("asprintf");
4504 goto done;
4506 if (strcmp(path, "/") == 0) {
4507 parent_path = strdup("");
4508 if (parent_path == NULL)
4509 return got_error_from_errno("strdup");
4510 } else {
4511 err = got_path_dirname(&parent_path, path);
4512 if (err)
4513 return err;
4516 ct = calloc(1, sizeof(*ct));
4517 if (ct == NULL) {
4518 err = got_error_from_errno("calloc");
4519 goto done;
4522 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4523 relpath) == -1) {
4524 err = got_error_from_errno("asprintf");
4525 goto done;
4527 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4528 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4529 } else {
4530 if (dirfd != -1) {
4531 if (fstatat(dirfd, de_name, &sb,
4532 AT_SYMLINK_NOFOLLOW) == -1) {
4533 err = got_error_from_errno2("fstatat",
4534 ct->ondisk_path);
4535 goto done;
4537 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4538 err = got_error_from_errno2("lstat", ct->ondisk_path);
4539 goto done;
4541 ct->mode = sb.st_mode;
4544 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4545 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4546 relpath) == -1) {
4547 err = got_error_from_errno("asprintf");
4548 goto done;
4551 ct->status = status;
4552 ct->staged_status = staged_status;
4553 ct->blob_id = NULL; /* will be filled in when blob gets created */
4554 if (ct->status != GOT_STATUS_ADD &&
4555 ct->staged_status != GOT_STATUS_ADD) {
4556 ct->base_blob_id = got_object_id_dup(blob_id);
4557 if (ct->base_blob_id == NULL) {
4558 err = got_error_from_errno("got_object_id_dup");
4559 goto done;
4561 ct->base_commit_id = got_object_id_dup(commit_id);
4562 if (ct->base_commit_id == NULL) {
4563 err = got_error_from_errno("got_object_id_dup");
4564 goto done;
4567 if (ct->staged_status == GOT_STATUS_ADD ||
4568 ct->staged_status == GOT_STATUS_MODIFY) {
4569 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4570 if (ct->staged_blob_id == NULL) {
4571 err = got_error_from_errno("got_object_id_dup");
4572 goto done;
4575 ct->path = strdup(path);
4576 if (ct->path == NULL) {
4577 err = got_error_from_errno("strdup");
4578 goto done;
4580 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4581 done:
4582 if (ct && (err || new == NULL))
4583 free_commitable(ct);
4584 free(parent_path);
4585 free(path);
4586 return err;
4589 static const struct got_error *write_tree(struct got_object_id **, int *,
4590 struct got_tree_object *, const char *, struct got_pathlist_head *,
4591 got_worktree_status_cb status_cb, void *status_arg,
4592 struct got_repository *);
4594 static const struct got_error *
4595 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4596 struct got_tree_entry *te, const char *parent_path,
4597 struct got_pathlist_head *commitable_paths,
4598 got_worktree_status_cb status_cb, void *status_arg,
4599 struct got_repository *repo)
4601 const struct got_error *err = NULL;
4602 struct got_tree_object *subtree;
4603 char *subpath;
4605 if (asprintf(&subpath, "%s%s%s", parent_path,
4606 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4607 return got_error_from_errno("asprintf");
4609 err = got_object_open_as_tree(&subtree, repo, &te->id);
4610 if (err)
4611 return err;
4613 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4614 commitable_paths, status_cb, status_arg, repo);
4615 got_object_tree_close(subtree);
4616 free(subpath);
4617 return err;
4620 static const struct got_error *
4621 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4623 const struct got_error *err = NULL;
4624 char *ct_parent_path = NULL;
4626 *match = 0;
4628 if (strchr(ct->in_repo_path, '/') == NULL) {
4629 *match = got_path_is_root_dir(path);
4630 return NULL;
4633 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4634 if (err)
4635 return err;
4636 *match = (strcmp(path, ct_parent_path) == 0);
4637 free(ct_parent_path);
4638 return err;
4641 static mode_t
4642 get_ct_file_mode(struct got_commitable *ct)
4644 if (S_ISLNK(ct->mode))
4645 return S_IFLNK;
4647 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4650 static const struct got_error *
4651 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4652 struct got_tree_entry *te, struct got_commitable *ct)
4654 const struct got_error *err = NULL;
4656 *new_te = NULL;
4658 err = got_object_tree_entry_dup(new_te, te);
4659 if (err)
4660 goto done;
4662 (*new_te)->mode = get_ct_file_mode(ct);
4664 if (ct->staged_status == GOT_STATUS_MODIFY)
4665 memcpy(&(*new_te)->id, ct->staged_blob_id,
4666 sizeof((*new_te)->id));
4667 else
4668 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4669 done:
4670 if (err && *new_te) {
4671 free(*new_te);
4672 *new_te = NULL;
4674 return err;
4677 static const struct got_error *
4678 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4679 struct got_commitable *ct)
4681 const struct got_error *err = NULL;
4682 char *ct_name;
4684 *new_te = NULL;
4686 *new_te = calloc(1, sizeof(**new_te));
4687 if (*new_te == NULL)
4688 return got_error_from_errno("calloc");
4690 ct_name = basename(ct->path);
4691 if (ct_name == NULL) {
4692 err = got_error_from_errno2("basename", ct->path);
4693 goto done;
4695 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4696 sizeof((*new_te)->name)) {
4697 err = got_error(GOT_ERR_NO_SPACE);
4698 goto done;
4701 (*new_te)->mode = get_ct_file_mode(ct);
4703 if (ct->staged_status == GOT_STATUS_ADD)
4704 memcpy(&(*new_te)->id, ct->staged_blob_id,
4705 sizeof((*new_te)->id));
4706 else
4707 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4708 done:
4709 if (err && *new_te) {
4710 free(*new_te);
4711 *new_te = NULL;
4713 return err;
4716 static const struct got_error *
4717 insert_tree_entry(struct got_tree_entry *new_te,
4718 struct got_pathlist_head *paths)
4720 const struct got_error *err = NULL;
4721 struct got_pathlist_entry *new_pe;
4723 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4724 if (err)
4725 return err;
4726 if (new_pe == NULL)
4727 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4728 return NULL;
4731 static const struct got_error *
4732 report_ct_status(struct got_commitable *ct,
4733 got_worktree_status_cb status_cb, void *status_arg)
4735 const char *ct_path = ct->path;
4736 unsigned char status;
4738 while (ct_path[0] == '/')
4739 ct_path++;
4741 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4742 status = ct->staged_status;
4743 else
4744 status = ct->status;
4746 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4747 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4750 static const struct got_error *
4751 match_modified_subtree(int *modified, struct got_tree_entry *te,
4752 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4754 const struct got_error *err = NULL;
4755 struct got_pathlist_entry *pe;
4756 char *te_path;
4758 *modified = 0;
4760 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4761 got_path_is_root_dir(base_tree_path) ? "" : "/",
4762 te->name) == -1)
4763 return got_error_from_errno("asprintf");
4765 TAILQ_FOREACH(pe, commitable_paths, entry) {
4766 struct got_commitable *ct = pe->data;
4767 *modified = got_path_is_child(ct->in_repo_path, te_path,
4768 strlen(te_path));
4769 if (*modified)
4770 break;
4773 free(te_path);
4774 return err;
4777 static const struct got_error *
4778 match_deleted_or_modified_ct(struct got_commitable **ctp,
4779 struct got_tree_entry *te, const char *base_tree_path,
4780 struct got_pathlist_head *commitable_paths)
4782 const struct got_error *err = NULL;
4783 struct got_pathlist_entry *pe;
4785 *ctp = NULL;
4787 TAILQ_FOREACH(pe, commitable_paths, entry) {
4788 struct got_commitable *ct = pe->data;
4789 char *ct_name = NULL;
4790 int path_matches;
4792 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4793 if (ct->status != GOT_STATUS_MODIFY &&
4794 ct->status != GOT_STATUS_MODE_CHANGE &&
4795 ct->status != GOT_STATUS_DELETE)
4796 continue;
4797 } else {
4798 if (ct->staged_status != GOT_STATUS_MODIFY &&
4799 ct->staged_status != GOT_STATUS_DELETE)
4800 continue;
4803 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4804 continue;
4806 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4807 if (err)
4808 return err;
4809 if (!path_matches)
4810 continue;
4812 ct_name = basename(pe->path);
4813 if (ct_name == NULL)
4814 return got_error_from_errno2("basename", pe->path);
4816 if (strcmp(te->name, ct_name) != 0)
4817 continue;
4819 *ctp = ct;
4820 break;
4823 return err;
4826 static const struct got_error *
4827 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4828 const char *child_path, const char *path_base_tree,
4829 struct got_pathlist_head *commitable_paths,
4830 got_worktree_status_cb status_cb, void *status_arg,
4831 struct got_repository *repo)
4833 const struct got_error *err = NULL;
4834 struct got_tree_entry *new_te;
4835 char *subtree_path;
4836 struct got_object_id *id = NULL;
4837 int nentries;
4839 *new_tep = NULL;
4841 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4842 got_path_is_root_dir(path_base_tree) ? "" : "/",
4843 child_path) == -1)
4844 return got_error_from_errno("asprintf");
4846 new_te = calloc(1, sizeof(*new_te));
4847 if (new_te == NULL)
4848 return got_error_from_errno("calloc");
4849 new_te->mode = S_IFDIR;
4851 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4852 sizeof(new_te->name)) {
4853 err = got_error(GOT_ERR_NO_SPACE);
4854 goto done;
4856 err = write_tree(&id, &nentries, NULL, subtree_path,
4857 commitable_paths, status_cb, status_arg, repo);
4858 if (err) {
4859 free(new_te);
4860 goto done;
4862 memcpy(&new_te->id, id, sizeof(new_te->id));
4863 done:
4864 free(id);
4865 free(subtree_path);
4866 if (err == NULL)
4867 *new_tep = new_te;
4868 return err;
4871 static const struct got_error *
4872 write_tree(struct got_object_id **new_tree_id, int *nentries,
4873 struct got_tree_object *base_tree, const char *path_base_tree,
4874 struct got_pathlist_head *commitable_paths,
4875 got_worktree_status_cb status_cb, void *status_arg,
4876 struct got_repository *repo)
4878 const struct got_error *err = NULL;
4879 struct got_pathlist_head paths;
4880 struct got_tree_entry *te, *new_te = NULL;
4881 struct got_pathlist_entry *pe;
4883 TAILQ_INIT(&paths);
4884 *nentries = 0;
4886 /* Insert, and recurse into, newly added entries first. */
4887 TAILQ_FOREACH(pe, commitable_paths, entry) {
4888 struct got_commitable *ct = pe->data;
4889 char *child_path = NULL, *slash;
4891 if ((ct->status != GOT_STATUS_ADD &&
4892 ct->staged_status != GOT_STATUS_ADD) ||
4893 (ct->flags & GOT_COMMITABLE_ADDED))
4894 continue;
4896 if (!got_path_is_child(pe->path, path_base_tree,
4897 strlen(path_base_tree)))
4898 continue;
4900 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4901 pe->path);
4902 if (err)
4903 goto done;
4905 slash = strchr(child_path, '/');
4906 if (slash == NULL) {
4907 err = alloc_added_blob_tree_entry(&new_te, ct);
4908 if (err)
4909 goto done;
4910 err = report_ct_status(ct, status_cb, status_arg);
4911 if (err)
4912 goto done;
4913 ct->flags |= GOT_COMMITABLE_ADDED;
4914 err = insert_tree_entry(new_te, &paths);
4915 if (err)
4916 goto done;
4917 (*nentries)++;
4918 } else {
4919 *slash = '\0'; /* trim trailing path components */
4920 if (base_tree == NULL ||
4921 got_object_tree_find_entry(base_tree, child_path)
4922 == NULL) {
4923 err = make_subtree_for_added_blob(&new_te,
4924 child_path, path_base_tree,
4925 commitable_paths, status_cb, status_arg,
4926 repo);
4927 if (err)
4928 goto done;
4929 err = insert_tree_entry(new_te, &paths);
4930 if (err)
4931 goto done;
4932 (*nentries)++;
4937 if (base_tree) {
4938 int i, nbase_entries;
4939 /* Handle modified and deleted entries. */
4940 nbase_entries = got_object_tree_get_nentries(base_tree);
4941 for (i = 0; i < nbase_entries; i++) {
4942 struct got_commitable *ct = NULL;
4944 te = got_object_tree_get_entry(base_tree, i);
4945 if (got_object_tree_entry_is_submodule(te)) {
4946 /* Entry is a submodule; just copy it. */
4947 err = got_object_tree_entry_dup(&new_te, te);
4948 if (err)
4949 goto done;
4950 err = insert_tree_entry(new_te, &paths);
4951 if (err)
4952 goto done;
4953 (*nentries)++;
4954 continue;
4957 if (S_ISDIR(te->mode)) {
4958 int modified;
4959 err = got_object_tree_entry_dup(&new_te, te);
4960 if (err)
4961 goto done;
4962 err = match_modified_subtree(&modified, te,
4963 path_base_tree, commitable_paths);
4964 if (err)
4965 goto done;
4966 /* Avoid recursion into unmodified subtrees. */
4967 if (modified) {
4968 struct got_object_id *new_id;
4969 int nsubentries;
4970 err = write_subtree(&new_id,
4971 &nsubentries, te,
4972 path_base_tree, commitable_paths,
4973 status_cb, status_arg, repo);
4974 if (err)
4975 goto done;
4976 if (nsubentries == 0) {
4977 /* All entries were deleted. */
4978 free(new_id);
4979 continue;
4981 memcpy(&new_te->id, new_id,
4982 sizeof(new_te->id));
4983 free(new_id);
4985 err = insert_tree_entry(new_te, &paths);
4986 if (err)
4987 goto done;
4988 (*nentries)++;
4989 continue;
4992 err = match_deleted_or_modified_ct(&ct, te,
4993 path_base_tree, commitable_paths);
4994 if (err)
4995 goto done;
4996 if (ct) {
4997 /* NB: Deleted entries get dropped here. */
4998 if (ct->status == GOT_STATUS_MODIFY ||
4999 ct->status == GOT_STATUS_MODE_CHANGE ||
5000 ct->staged_status == GOT_STATUS_MODIFY) {
5001 err = alloc_modified_blob_tree_entry(
5002 &new_te, te, ct);
5003 if (err)
5004 goto done;
5005 err = insert_tree_entry(new_te, &paths);
5006 if (err)
5007 goto done;
5008 (*nentries)++;
5010 err = report_ct_status(ct, status_cb,
5011 status_arg);
5012 if (err)
5013 goto done;
5014 } else {
5015 /* Entry is unchanged; just copy it. */
5016 err = got_object_tree_entry_dup(&new_te, te);
5017 if (err)
5018 goto done;
5019 err = insert_tree_entry(new_te, &paths);
5020 if (err)
5021 goto done;
5022 (*nentries)++;
5027 /* Write new list of entries; deleted entries have been dropped. */
5028 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5029 done:
5030 got_pathlist_free(&paths);
5031 return err;
5034 static const struct got_error *
5035 reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
5036 struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
5037 struct got_repository *repo)
5039 const struct got_error *err = NULL;
5040 struct got_blob_object *blob = NULL;
5041 struct got_object_id *tree_id = NULL;
5042 char *tree_path = NULL;
5043 struct got_tree_object *tree = NULL;
5044 struct got_tree_entry *te;
5045 char *entry_name;
5047 err = got_object_open_as_blob(&blob, repo, ct->blob_id, PATH_MAX);
5048 if (err)
5049 return err;
5051 err = got_path_dirname(&tree_path, ct->in_repo_path);
5052 if (err) {
5053 if (err->code != GOT_ERR_BAD_PATH)
5054 goto done;
5055 err = got_object_id_by_path(&tree_id, repo,
5056 new_base_commit_id, "");
5057 if (err)
5058 goto done;
5059 } else {
5060 err = got_object_id_by_path(&tree_id, repo,
5061 new_base_commit_id, tree_path);
5062 if (err)
5063 goto done;
5066 err = got_object_open_as_tree(&tree, repo, tree_id);
5067 if (err)
5068 goto done;
5070 entry_name = basename(ct->path);
5071 if (entry_name == NULL) {
5072 err = got_error_from_errno2("basename", ct->path);
5073 goto done;
5076 te = got_object_tree_find_entry(tree, entry_name);
5077 if (te == NULL) {
5078 err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5079 goto done;
5082 err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5083 ct->path, blob, 0, 0, 0, repo, NULL, NULL);
5084 done:
5085 if (blob)
5086 got_object_blob_close(blob);
5087 if (tree)
5088 got_object_tree_close(tree);
5089 free(tree_id);
5090 free(tree_path);
5091 return err;
5095 * After comitting a symlink we have a chance to convert "bad" symlinks
5096 * (those which point outside the work tree or into .got) to regular files.
5097 * This way, the post-commit work tree state matches a fresh checkout of
5098 * the tree which was just committed. We also mark such newly committed
5099 * symlinks as "bad" in the work tree's fileindex.
5101 static const struct got_error *
5102 reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5103 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5104 struct got_worktree *worktree, struct got_repository *repo)
5106 const struct got_error *err = NULL;
5107 struct got_pathlist_entry *pe;
5109 TAILQ_FOREACH(pe, commitable_paths, entry) {
5110 struct got_commitable *ct = pe->data;
5111 struct got_fileindex_entry *ie;
5112 int is_bad_symlink = 0;
5114 if (!S_ISLNK(get_ct_file_mode(ct)))
5115 continue;
5117 err = reinstall_symlink_after_commit(&is_bad_symlink,
5118 ct, new_base_commit_id, worktree, repo);
5119 if (err)
5120 break;
5121 ie = got_fileindex_entry_get(fileindex, ct->path,
5122 strlen(ct->path));
5123 if (ie && is_bad_symlink) {
5124 err = got_fileindex_entry_filetype_set(ie,
5125 GOT_FILEIDX_MODE_BAD_SYMLINK);
5126 if (err)
5127 break;
5131 return err;
5134 static const struct got_error *
5135 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5136 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5137 int have_staged_files)
5139 const struct got_error *err = NULL;
5140 struct got_pathlist_entry *pe;
5142 TAILQ_FOREACH(pe, commitable_paths, entry) {
5143 struct got_fileindex_entry *ie;
5144 struct got_commitable *ct = pe->data;
5146 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5147 if (ie) {
5148 if (ct->status == GOT_STATUS_DELETE ||
5149 ct->staged_status == GOT_STATUS_DELETE) {
5150 got_fileindex_entry_remove(fileindex, ie);
5151 } else if (ct->staged_status == GOT_STATUS_ADD ||
5152 ct->staged_status == GOT_STATUS_MODIFY) {
5153 got_fileindex_entry_stage_set(ie,
5154 GOT_FILEIDX_STAGE_NONE);
5155 err = got_fileindex_entry_update(ie,
5156 ct->ondisk_path, ct->staged_blob_id->sha1,
5157 new_base_commit_id->sha1,
5158 !have_staged_files);
5159 } else
5160 err = got_fileindex_entry_update(ie,
5161 ct->ondisk_path, ct->blob_id->sha1,
5162 new_base_commit_id->sha1,
5163 !have_staged_files);
5164 } else {
5165 err = got_fileindex_entry_alloc(&ie, pe->path);
5166 if (err)
5167 break;
5168 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5169 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5170 if (err) {
5171 got_fileindex_entry_free(ie);
5172 break;
5174 err = got_fileindex_entry_add(fileindex, ie);
5175 if (err) {
5176 got_fileindex_entry_free(ie);
5177 break;
5181 return err;
5185 static const struct got_error *
5186 check_out_of_date(const char *in_repo_path, unsigned char status,
5187 unsigned char staged_status, struct got_object_id *base_blob_id,
5188 struct got_object_id *base_commit_id,
5189 struct got_object_id *head_commit_id, struct got_repository *repo,
5190 int ood_errcode)
5192 const struct got_error *err = NULL;
5193 struct got_object_id *id = NULL;
5195 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5196 /* Trivial case: base commit == head commit */
5197 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5198 return NULL;
5200 * Ensure file content which local changes were based
5201 * on matches file content in the branch head.
5203 err = got_object_id_by_path(&id, repo, head_commit_id,
5204 in_repo_path);
5205 if (err) {
5206 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5207 err = got_error(ood_errcode);
5208 goto done;
5209 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5210 err = got_error(ood_errcode);
5211 } else {
5212 /* Require that added files don't exist in the branch head. */
5213 err = got_object_id_by_path(&id, repo, head_commit_id,
5214 in_repo_path);
5215 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5216 goto done;
5217 err = id ? got_error(ood_errcode) : NULL;
5219 done:
5220 free(id);
5221 return err;
5224 const struct got_error *
5225 commit_worktree(struct got_object_id **new_commit_id,
5226 struct got_pathlist_head *commitable_paths,
5227 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5228 const char *author, const char *committer,
5229 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5230 got_worktree_status_cb status_cb, void *status_arg,
5231 struct got_repository *repo)
5233 const struct got_error *err = NULL, *unlockerr = NULL;
5234 struct got_pathlist_entry *pe;
5235 const char *head_ref_name = NULL;
5236 struct got_commit_object *head_commit = NULL;
5237 struct got_reference *head_ref2 = NULL;
5238 struct got_object_id *head_commit_id2 = NULL;
5239 struct got_tree_object *head_tree = NULL;
5240 struct got_object_id *new_tree_id = NULL;
5241 int nentries;
5242 struct got_object_id_queue parent_ids;
5243 struct got_object_qid *pid = NULL;
5244 char *logmsg = NULL;
5246 *new_commit_id = NULL;
5248 SIMPLEQ_INIT(&parent_ids);
5250 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5251 if (err)
5252 goto done;
5254 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5255 if (err)
5256 goto done;
5258 if (commit_msg_cb != NULL) {
5259 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5260 if (err)
5261 goto done;
5264 if (logmsg == NULL || strlen(logmsg) == 0) {
5265 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5266 goto done;
5269 /* Create blobs from added and modified files and record their IDs. */
5270 TAILQ_FOREACH(pe, commitable_paths, entry) {
5271 struct got_commitable *ct = pe->data;
5272 char *ondisk_path;
5274 /* Blobs for staged files already exist. */
5275 if (ct->staged_status == GOT_STATUS_ADD ||
5276 ct->staged_status == GOT_STATUS_MODIFY)
5277 continue;
5279 if (ct->status != GOT_STATUS_ADD &&
5280 ct->status != GOT_STATUS_MODIFY &&
5281 ct->status != GOT_STATUS_MODE_CHANGE)
5282 continue;
5284 if (asprintf(&ondisk_path, "%s/%s",
5285 worktree->root_path, pe->path) == -1) {
5286 err = got_error_from_errno("asprintf");
5287 goto done;
5289 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5290 free(ondisk_path);
5291 if (err)
5292 goto done;
5295 /* Recursively write new tree objects. */
5296 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5297 commitable_paths, status_cb, status_arg, repo);
5298 if (err)
5299 goto done;
5301 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5302 if (err)
5303 goto done;
5304 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5305 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5306 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5307 got_object_qid_free(pid);
5308 if (logmsg != NULL)
5309 free(logmsg);
5310 if (err)
5311 goto done;
5313 /* Check if a concurrent commit to our branch has occurred. */
5314 head_ref_name = got_worktree_get_head_ref_name(worktree);
5315 if (head_ref_name == NULL) {
5316 err = got_error_from_errno("got_worktree_get_head_ref_name");
5317 goto done;
5319 /* Lock the reference here to prevent concurrent modification. */
5320 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5321 if (err)
5322 goto done;
5323 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5324 if (err)
5325 goto done;
5326 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5327 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5328 goto done;
5330 /* Update branch head in repository. */
5331 err = got_ref_change_ref(head_ref2, *new_commit_id);
5332 if (err)
5333 goto done;
5334 err = got_ref_write(head_ref2, repo);
5335 if (err)
5336 goto done;
5338 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5339 if (err)
5340 goto done;
5342 err = ref_base_commit(worktree, repo);
5343 if (err)
5344 goto done;
5345 done:
5346 if (head_tree)
5347 got_object_tree_close(head_tree);
5348 if (head_commit)
5349 got_object_commit_close(head_commit);
5350 free(head_commit_id2);
5351 if (head_ref2) {
5352 unlockerr = got_ref_unlock(head_ref2);
5353 if (unlockerr && err == NULL)
5354 err = unlockerr;
5355 got_ref_close(head_ref2);
5357 return err;
5360 static const struct got_error *
5361 check_path_is_commitable(const char *path,
5362 struct got_pathlist_head *commitable_paths)
5364 struct got_pathlist_entry *cpe = NULL;
5365 size_t path_len = strlen(path);
5367 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5368 struct got_commitable *ct = cpe->data;
5369 const char *ct_path = ct->path;
5371 while (ct_path[0] == '/')
5372 ct_path++;
5374 if (strcmp(path, ct_path) == 0 ||
5375 got_path_is_child(ct_path, path, path_len))
5376 break;
5379 if (cpe == NULL)
5380 return got_error_path(path, GOT_ERR_BAD_PATH);
5382 return NULL;
5385 static const struct got_error *
5386 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5388 int *have_staged_files = arg;
5390 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5391 *have_staged_files = 1;
5392 return got_error(GOT_ERR_CANCELLED);
5395 return NULL;
5398 static const struct got_error *
5399 check_non_staged_files(struct got_fileindex *fileindex,
5400 struct got_pathlist_head *paths)
5402 struct got_pathlist_entry *pe;
5403 struct got_fileindex_entry *ie;
5405 TAILQ_FOREACH(pe, paths, entry) {
5406 if (pe->path[0] == '\0')
5407 continue;
5408 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5409 if (ie == NULL)
5410 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5411 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5412 return got_error_path(pe->path,
5413 GOT_ERR_FILE_NOT_STAGED);
5416 return NULL;
5419 const struct got_error *
5420 got_worktree_commit(struct got_object_id **new_commit_id,
5421 struct got_worktree *worktree, struct got_pathlist_head *paths,
5422 const char *author, const char *committer,
5423 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5424 got_worktree_status_cb status_cb, void *status_arg,
5425 struct got_repository *repo)
5427 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5428 struct got_fileindex *fileindex = NULL;
5429 char *fileindex_path = NULL;
5430 struct got_pathlist_head commitable_paths;
5431 struct collect_commitables_arg cc_arg;
5432 struct got_pathlist_entry *pe;
5433 struct got_reference *head_ref = NULL;
5434 struct got_object_id *head_commit_id = NULL;
5435 int have_staged_files = 0;
5437 *new_commit_id = NULL;
5439 TAILQ_INIT(&commitable_paths);
5441 err = lock_worktree(worktree, LOCK_EX);
5442 if (err)
5443 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 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5454 if (err)
5455 goto done;
5457 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5458 &have_staged_files);
5459 if (err && err->code != GOT_ERR_CANCELLED)
5460 goto done;
5461 if (have_staged_files) {
5462 err = check_non_staged_files(fileindex, paths);
5463 if (err)
5464 goto done;
5467 cc_arg.commitable_paths = &commitable_paths;
5468 cc_arg.worktree = worktree;
5469 cc_arg.repo = repo;
5470 cc_arg.have_staged_files = have_staged_files;
5471 TAILQ_FOREACH(pe, paths, entry) {
5472 err = worktree_status(worktree, pe->path, fileindex, repo,
5473 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5474 if (err)
5475 goto done;
5478 if (TAILQ_EMPTY(&commitable_paths)) {
5479 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5480 goto done;
5483 TAILQ_FOREACH(pe, paths, entry) {
5484 err = check_path_is_commitable(pe->path, &commitable_paths);
5485 if (err)
5486 goto done;
5489 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5490 struct got_commitable *ct = pe->data;
5491 const char *ct_path = ct->in_repo_path;
5493 while (ct_path[0] == '/')
5494 ct_path++;
5495 err = check_out_of_date(ct_path, ct->status,
5496 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5497 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5498 if (err)
5499 goto done;
5503 err = commit_worktree(new_commit_id, &commitable_paths,
5504 head_commit_id, worktree, author, committer,
5505 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5506 if (err)
5507 goto done;
5509 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5510 fileindex, have_staged_files);
5511 if (err == NULL) {
5512 err = reinstall_symlinks_after_commit(&commitable_paths,
5513 *new_commit_id, fileindex, worktree, repo);
5515 sync_err = sync_fileindex(fileindex, fileindex_path);
5516 if (sync_err && err == NULL)
5517 err = sync_err;
5518 done:
5519 if (fileindex)
5520 got_fileindex_free(fileindex);
5521 free(fileindex_path);
5522 unlockerr = lock_worktree(worktree, LOCK_SH);
5523 if (unlockerr && err == NULL)
5524 err = unlockerr;
5525 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5526 struct got_commitable *ct = pe->data;
5527 free_commitable(ct);
5529 got_pathlist_free(&commitable_paths);
5530 return err;
5533 const char *
5534 got_commitable_get_path(struct got_commitable *ct)
5536 return ct->path;
5539 unsigned int
5540 got_commitable_get_status(struct got_commitable *ct)
5542 return ct->status;
5545 struct check_rebase_ok_arg {
5546 struct got_worktree *worktree;
5547 struct got_repository *repo;
5550 static const struct got_error *
5551 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5553 const struct got_error *err = NULL;
5554 struct check_rebase_ok_arg *a = arg;
5555 unsigned char status;
5556 struct stat sb;
5557 char *ondisk_path;
5559 /* Reject rebase of a work tree with mixed base commits. */
5560 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5561 SHA1_DIGEST_LENGTH))
5562 return got_error(GOT_ERR_MIXED_COMMITS);
5564 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5565 == -1)
5566 return got_error_from_errno("asprintf");
5568 /* Reject rebase of a work tree with modified or staged files. */
5569 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5570 free(ondisk_path);
5571 if (err)
5572 return err;
5574 if (status != GOT_STATUS_NO_CHANGE)
5575 return got_error(GOT_ERR_MODIFIED);
5576 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5577 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5579 return NULL;
5582 const struct got_error *
5583 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5584 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5585 struct got_worktree *worktree, struct got_reference *branch,
5586 struct got_repository *repo)
5588 const struct got_error *err = NULL;
5589 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5590 char *branch_ref_name = NULL;
5591 char *fileindex_path = NULL;
5592 struct check_rebase_ok_arg ok_arg;
5593 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5594 struct got_object_id *wt_branch_tip = NULL;
5596 *new_base_branch_ref = NULL;
5597 *tmp_branch = NULL;
5598 *fileindex = NULL;
5600 err = lock_worktree(worktree, LOCK_EX);
5601 if (err)
5602 return err;
5604 err = open_fileindex(fileindex, &fileindex_path, worktree);
5605 if (err)
5606 goto done;
5608 ok_arg.worktree = worktree;
5609 ok_arg.repo = repo;
5610 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5611 &ok_arg);
5612 if (err)
5613 goto done;
5615 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5616 if (err)
5617 goto done;
5619 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5620 if (err)
5621 goto done;
5623 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5624 if (err)
5625 goto done;
5627 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5628 0);
5629 if (err)
5630 goto done;
5632 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5633 if (err)
5634 goto done;
5635 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5636 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5637 goto done;
5640 err = got_ref_alloc_symref(new_base_branch_ref,
5641 new_base_branch_ref_name, wt_branch);
5642 if (err)
5643 goto done;
5644 err = got_ref_write(*new_base_branch_ref, repo);
5645 if (err)
5646 goto done;
5648 /* TODO Lock original branch's ref while rebasing? */
5650 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5651 if (err)
5652 goto done;
5654 err = got_ref_write(branch_ref, repo);
5655 if (err)
5656 goto done;
5658 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5659 worktree->base_commit_id);
5660 if (err)
5661 goto done;
5662 err = got_ref_write(*tmp_branch, repo);
5663 if (err)
5664 goto done;
5666 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5667 if (err)
5668 goto done;
5669 done:
5670 free(fileindex_path);
5671 free(tmp_branch_name);
5672 free(new_base_branch_ref_name);
5673 free(branch_ref_name);
5674 if (branch_ref)
5675 got_ref_close(branch_ref);
5676 if (wt_branch)
5677 got_ref_close(wt_branch);
5678 free(wt_branch_tip);
5679 if (err) {
5680 if (*new_base_branch_ref) {
5681 got_ref_close(*new_base_branch_ref);
5682 *new_base_branch_ref = NULL;
5684 if (*tmp_branch) {
5685 got_ref_close(*tmp_branch);
5686 *tmp_branch = NULL;
5688 if (*fileindex) {
5689 got_fileindex_free(*fileindex);
5690 *fileindex = NULL;
5692 lock_worktree(worktree, LOCK_SH);
5694 return err;
5697 const struct got_error *
5698 got_worktree_rebase_continue(struct got_object_id **commit_id,
5699 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5700 struct got_reference **branch, struct got_fileindex **fileindex,
5701 struct got_worktree *worktree, struct got_repository *repo)
5703 const struct got_error *err;
5704 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5705 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5706 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5707 char *fileindex_path = NULL;
5708 int have_staged_files = 0;
5710 *commit_id = NULL;
5711 *new_base_branch = NULL;
5712 *tmp_branch = NULL;
5713 *branch = NULL;
5714 *fileindex = NULL;
5716 err = lock_worktree(worktree, LOCK_EX);
5717 if (err)
5718 return err;
5720 err = open_fileindex(fileindex, &fileindex_path, worktree);
5721 if (err)
5722 goto done;
5724 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5725 &have_staged_files);
5726 if (err && err->code != GOT_ERR_CANCELLED)
5727 goto done;
5728 if (have_staged_files) {
5729 err = got_error(GOT_ERR_STAGED_PATHS);
5730 goto done;
5733 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5734 if (err)
5735 goto done;
5737 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5738 if (err)
5739 goto done;
5741 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5742 if (err)
5743 goto done;
5745 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5746 if (err)
5747 goto done;
5749 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5750 if (err)
5751 goto done;
5753 err = got_ref_open(branch, repo,
5754 got_ref_get_symref_target(branch_ref), 0);
5755 if (err)
5756 goto done;
5758 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5759 if (err)
5760 goto done;
5762 err = got_ref_resolve(commit_id, repo, commit_ref);
5763 if (err)
5764 goto done;
5766 err = got_ref_open(new_base_branch, repo,
5767 new_base_branch_ref_name, 0);
5768 if (err)
5769 goto done;
5771 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5772 if (err)
5773 goto done;
5774 done:
5775 free(commit_ref_name);
5776 free(branch_ref_name);
5777 free(fileindex_path);
5778 if (commit_ref)
5779 got_ref_close(commit_ref);
5780 if (branch_ref)
5781 got_ref_close(branch_ref);
5782 if (err) {
5783 free(*commit_id);
5784 *commit_id = NULL;
5785 if (*tmp_branch) {
5786 got_ref_close(*tmp_branch);
5787 *tmp_branch = NULL;
5789 if (*new_base_branch) {
5790 got_ref_close(*new_base_branch);
5791 *new_base_branch = NULL;
5793 if (*branch) {
5794 got_ref_close(*branch);
5795 *branch = NULL;
5797 if (*fileindex) {
5798 got_fileindex_free(*fileindex);
5799 *fileindex = NULL;
5801 lock_worktree(worktree, LOCK_SH);
5803 return err;
5806 const struct got_error *
5807 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5809 const struct got_error *err;
5810 char *tmp_branch_name = NULL;
5812 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5813 if (err)
5814 return err;
5816 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5817 free(tmp_branch_name);
5818 return NULL;
5821 static const struct got_error *
5822 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5823 char **logmsg, void *arg)
5825 *logmsg = arg;
5826 return NULL;
5829 static const struct got_error *
5830 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5831 const char *path, struct got_object_id *blob_id,
5832 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5833 int dirfd, const char *de_name)
5835 return NULL;
5838 struct collect_merged_paths_arg {
5839 got_worktree_checkout_cb progress_cb;
5840 void *progress_arg;
5841 struct got_pathlist_head *merged_paths;
5844 static const struct got_error *
5845 collect_merged_paths(void *arg, unsigned char status, const char *path)
5847 const struct got_error *err;
5848 struct collect_merged_paths_arg *a = arg;
5849 char *p;
5850 struct got_pathlist_entry *new;
5852 err = (*a->progress_cb)(a->progress_arg, status, path);
5853 if (err)
5854 return err;
5856 if (status != GOT_STATUS_MERGE &&
5857 status != GOT_STATUS_ADD &&
5858 status != GOT_STATUS_DELETE &&
5859 status != GOT_STATUS_CONFLICT)
5860 return NULL;
5862 p = strdup(path);
5863 if (p == NULL)
5864 return got_error_from_errno("strdup");
5866 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5867 if (err || new == NULL)
5868 free(p);
5869 return err;
5872 void
5873 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5875 struct got_pathlist_entry *pe;
5877 TAILQ_FOREACH(pe, merged_paths, entry)
5878 free((char *)pe->path);
5880 got_pathlist_free(merged_paths);
5883 static const struct got_error *
5884 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5885 int is_rebase, struct got_repository *repo)
5887 const struct got_error *err;
5888 struct got_reference *commit_ref = NULL;
5890 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5891 if (err) {
5892 if (err->code != GOT_ERR_NOT_REF)
5893 goto done;
5894 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5895 if (err)
5896 goto done;
5897 err = got_ref_write(commit_ref, repo);
5898 if (err)
5899 goto done;
5900 } else if (is_rebase) {
5901 struct got_object_id *stored_id;
5902 int cmp;
5904 err = got_ref_resolve(&stored_id, repo, commit_ref);
5905 if (err)
5906 goto done;
5907 cmp = got_object_id_cmp(commit_id, stored_id);
5908 free(stored_id);
5909 if (cmp != 0) {
5910 err = got_error(GOT_ERR_REBASE_COMMITID);
5911 goto done;
5914 done:
5915 if (commit_ref)
5916 got_ref_close(commit_ref);
5917 return err;
5920 static const struct got_error *
5921 rebase_merge_files(struct got_pathlist_head *merged_paths,
5922 const char *commit_ref_name, struct got_worktree *worktree,
5923 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5924 struct got_object_id *commit_id, struct got_repository *repo,
5925 got_worktree_checkout_cb progress_cb, void *progress_arg,
5926 got_cancel_cb cancel_cb, void *cancel_arg)
5928 const struct got_error *err;
5929 struct got_reference *commit_ref = NULL;
5930 struct collect_merged_paths_arg cmp_arg;
5931 char *fileindex_path;
5933 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5935 err = get_fileindex_path(&fileindex_path, worktree);
5936 if (err)
5937 return err;
5939 cmp_arg.progress_cb = progress_cb;
5940 cmp_arg.progress_arg = progress_arg;
5941 cmp_arg.merged_paths = merged_paths;
5942 err = merge_files(worktree, fileindex, fileindex_path,
5943 parent_commit_id, commit_id, repo, collect_merged_paths,
5944 &cmp_arg, cancel_cb, cancel_arg);
5945 if (commit_ref)
5946 got_ref_close(commit_ref);
5947 return err;
5950 const struct got_error *
5951 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5952 struct got_worktree *worktree, struct got_fileindex *fileindex,
5953 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5954 struct got_repository *repo,
5955 got_worktree_checkout_cb progress_cb, void *progress_arg,
5956 got_cancel_cb cancel_cb, void *cancel_arg)
5958 const struct got_error *err;
5959 char *commit_ref_name;
5961 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5962 if (err)
5963 return err;
5965 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5966 if (err)
5967 goto done;
5969 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5970 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5971 progress_arg, cancel_cb, cancel_arg);
5972 done:
5973 free(commit_ref_name);
5974 return err;
5977 const struct got_error *
5978 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5979 struct got_worktree *worktree, struct got_fileindex *fileindex,
5980 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5981 struct got_repository *repo,
5982 got_worktree_checkout_cb progress_cb, void *progress_arg,
5983 got_cancel_cb cancel_cb, void *cancel_arg)
5985 const struct got_error *err;
5986 char *commit_ref_name;
5988 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5989 if (err)
5990 return err;
5992 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5993 if (err)
5994 goto done;
5996 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5997 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5998 progress_arg, cancel_cb, cancel_arg);
5999 done:
6000 free(commit_ref_name);
6001 return err;
6004 static const struct got_error *
6005 rebase_commit(struct got_object_id **new_commit_id,
6006 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6007 struct got_worktree *worktree, struct got_fileindex *fileindex,
6008 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6009 const char *new_logmsg, struct got_repository *repo)
6011 const struct got_error *err, *sync_err;
6012 struct got_pathlist_head commitable_paths;
6013 struct collect_commitables_arg cc_arg;
6014 char *fileindex_path = NULL;
6015 struct got_reference *head_ref = NULL;
6016 struct got_object_id *head_commit_id = NULL;
6017 char *logmsg = NULL;
6019 TAILQ_INIT(&commitable_paths);
6020 *new_commit_id = NULL;
6022 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6024 err = get_fileindex_path(&fileindex_path, worktree);
6025 if (err)
6026 return err;
6028 cc_arg.commitable_paths = &commitable_paths;
6029 cc_arg.worktree = worktree;
6030 cc_arg.repo = repo;
6031 cc_arg.have_staged_files = 0;
6033 * If possible get the status of individual files directly to
6034 * avoid crawling the entire work tree once per rebased commit.
6035 * TODO: Ideally, merged_paths would contain a list of commitables
6036 * we could use so we could skip worktree_status() entirely.
6038 if (merged_paths) {
6039 struct got_pathlist_entry *pe;
6040 TAILQ_FOREACH(pe, merged_paths, entry) {
6041 err = worktree_status(worktree, pe->path, fileindex,
6042 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6043 0);
6044 if (err)
6045 goto done;
6047 } else {
6048 err = worktree_status(worktree, "", fileindex, repo,
6049 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6050 if (err)
6051 goto done;
6054 if (TAILQ_EMPTY(&commitable_paths)) {
6055 /* No-op change; commit will be elided. */
6056 err = got_ref_delete(commit_ref, repo);
6057 if (err)
6058 goto done;
6059 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6060 goto done;
6063 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6064 if (err)
6065 goto done;
6067 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6068 if (err)
6069 goto done;
6071 if (new_logmsg) {
6072 logmsg = strdup(new_logmsg);
6073 if (logmsg == NULL) {
6074 err = got_error_from_errno("strdup");
6075 goto done;
6077 } else {
6078 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6079 if (err)
6080 goto done;
6083 /* NB: commit_worktree will call free(logmsg) */
6084 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6085 worktree, got_object_commit_get_author(orig_commit),
6086 got_object_commit_get_committer(orig_commit),
6087 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6088 if (err)
6089 goto done;
6091 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6092 if (err)
6093 goto done;
6095 err = got_ref_delete(commit_ref, repo);
6096 if (err)
6097 goto done;
6099 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6100 fileindex, 0);
6101 sync_err = sync_fileindex(fileindex, fileindex_path);
6102 if (sync_err && err == NULL)
6103 err = sync_err;
6104 done:
6105 free(fileindex_path);
6106 free(head_commit_id);
6107 if (head_ref)
6108 got_ref_close(head_ref);
6109 if (err) {
6110 free(*new_commit_id);
6111 *new_commit_id = NULL;
6113 return err;
6116 const struct got_error *
6117 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6118 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6119 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6120 struct got_commit_object *orig_commit,
6121 struct got_object_id *orig_commit_id, struct got_repository *repo)
6123 const struct got_error *err;
6124 char *commit_ref_name;
6125 struct got_reference *commit_ref = NULL;
6126 struct got_object_id *commit_id = NULL;
6128 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6129 if (err)
6130 return err;
6132 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6133 if (err)
6134 goto done;
6135 err = got_ref_resolve(&commit_id, repo, commit_ref);
6136 if (err)
6137 goto done;
6138 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6139 err = got_error(GOT_ERR_REBASE_COMMITID);
6140 goto done;
6143 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6144 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6145 done:
6146 if (commit_ref)
6147 got_ref_close(commit_ref);
6148 free(commit_ref_name);
6149 free(commit_id);
6150 return err;
6153 const struct got_error *
6154 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6155 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6156 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6157 struct got_commit_object *orig_commit,
6158 struct got_object_id *orig_commit_id, const char *new_logmsg,
6159 struct got_repository *repo)
6161 const struct got_error *err;
6162 char *commit_ref_name;
6163 struct got_reference *commit_ref = NULL;
6165 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6166 if (err)
6167 return err;
6169 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6170 if (err)
6171 goto done;
6173 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6174 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6175 done:
6176 if (commit_ref)
6177 got_ref_close(commit_ref);
6178 free(commit_ref_name);
6179 return err;
6182 const struct got_error *
6183 got_worktree_rebase_postpone(struct got_worktree *worktree,
6184 struct got_fileindex *fileindex)
6186 if (fileindex)
6187 got_fileindex_free(fileindex);
6188 return lock_worktree(worktree, LOCK_SH);
6191 static const struct got_error *
6192 delete_ref(const char *name, struct got_repository *repo)
6194 const struct got_error *err;
6195 struct got_reference *ref;
6197 err = got_ref_open(&ref, repo, name, 0);
6198 if (err) {
6199 if (err->code == GOT_ERR_NOT_REF)
6200 return NULL;
6201 return err;
6204 err = got_ref_delete(ref, repo);
6205 got_ref_close(ref);
6206 return err;
6209 static const struct got_error *
6210 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6212 const struct got_error *err;
6213 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6214 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6216 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6217 if (err)
6218 goto done;
6219 err = delete_ref(tmp_branch_name, repo);
6220 if (err)
6221 goto done;
6223 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6224 if (err)
6225 goto done;
6226 err = delete_ref(new_base_branch_ref_name, repo);
6227 if (err)
6228 goto done;
6230 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6231 if (err)
6232 goto done;
6233 err = delete_ref(branch_ref_name, repo);
6234 if (err)
6235 goto done;
6237 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6238 if (err)
6239 goto done;
6240 err = delete_ref(commit_ref_name, repo);
6241 if (err)
6242 goto done;
6244 done:
6245 free(tmp_branch_name);
6246 free(new_base_branch_ref_name);
6247 free(branch_ref_name);
6248 free(commit_ref_name);
6249 return err;
6252 const struct got_error *
6253 got_worktree_rebase_complete(struct got_worktree *worktree,
6254 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6255 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6256 struct got_repository *repo)
6258 const struct got_error *err, *unlockerr;
6259 struct got_object_id *new_head_commit_id = NULL;
6261 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6262 if (err)
6263 return err;
6265 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6266 if (err)
6267 goto done;
6269 err = got_ref_write(rebased_branch, repo);
6270 if (err)
6271 goto done;
6273 err = got_worktree_set_head_ref(worktree, rebased_branch);
6274 if (err)
6275 goto done;
6277 err = delete_rebase_refs(worktree, repo);
6278 done:
6279 if (fileindex)
6280 got_fileindex_free(fileindex);
6281 free(new_head_commit_id);
6282 unlockerr = lock_worktree(worktree, LOCK_SH);
6283 if (unlockerr && err == NULL)
6284 err = unlockerr;
6285 return err;
6288 const struct got_error *
6289 got_worktree_rebase_abort(struct got_worktree *worktree,
6290 struct got_fileindex *fileindex, struct got_repository *repo,
6291 struct got_reference *new_base_branch,
6292 got_worktree_checkout_cb progress_cb, void *progress_arg)
6294 const struct got_error *err, *unlockerr, *sync_err;
6295 struct got_reference *resolved = NULL;
6296 struct got_object_id *commit_id = NULL;
6297 char *fileindex_path = NULL;
6298 struct revert_file_args rfa;
6299 struct got_object_id *tree_id = NULL;
6301 err = lock_worktree(worktree, LOCK_EX);
6302 if (err)
6303 return err;
6305 err = got_ref_open(&resolved, repo,
6306 got_ref_get_symref_target(new_base_branch), 0);
6307 if (err)
6308 goto done;
6310 err = got_worktree_set_head_ref(worktree, resolved);
6311 if (err)
6312 goto done;
6315 * XXX commits to the base branch could have happened while
6316 * we were busy rebasing; should we store the original commit ID
6317 * when rebase begins and read it back here?
6319 err = got_ref_resolve(&commit_id, repo, resolved);
6320 if (err)
6321 goto done;
6323 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6324 if (err)
6325 goto done;
6327 err = got_object_id_by_path(&tree_id, repo,
6328 worktree->base_commit_id, worktree->path_prefix);
6329 if (err)
6330 goto done;
6332 err = delete_rebase_refs(worktree, repo);
6333 if (err)
6334 goto done;
6336 err = get_fileindex_path(&fileindex_path, worktree);
6337 if (err)
6338 goto done;
6340 rfa.worktree = worktree;
6341 rfa.fileindex = fileindex;
6342 rfa.progress_cb = progress_cb;
6343 rfa.progress_arg = progress_arg;
6344 rfa.patch_cb = NULL;
6345 rfa.patch_arg = NULL;
6346 rfa.repo = repo;
6347 err = worktree_status(worktree, "", fileindex, repo,
6348 revert_file, &rfa, NULL, NULL, 0, 0);
6349 if (err)
6350 goto sync;
6352 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6353 repo, progress_cb, progress_arg, NULL, NULL);
6354 sync:
6355 sync_err = sync_fileindex(fileindex, fileindex_path);
6356 if (sync_err && err == NULL)
6357 err = sync_err;
6358 done:
6359 got_ref_close(resolved);
6360 free(tree_id);
6361 free(commit_id);
6362 if (fileindex)
6363 got_fileindex_free(fileindex);
6364 free(fileindex_path);
6366 unlockerr = lock_worktree(worktree, LOCK_SH);
6367 if (unlockerr && err == NULL)
6368 err = unlockerr;
6369 return err;
6372 const struct got_error *
6373 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6374 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6375 struct got_fileindex **fileindex, struct got_worktree *worktree,
6376 struct got_repository *repo)
6378 const struct got_error *err = NULL;
6379 char *tmp_branch_name = NULL;
6380 char *branch_ref_name = NULL;
6381 char *base_commit_ref_name = NULL;
6382 char *fileindex_path = NULL;
6383 struct check_rebase_ok_arg ok_arg;
6384 struct got_reference *wt_branch = NULL;
6385 struct got_reference *base_commit_ref = NULL;
6387 *tmp_branch = NULL;
6388 *branch_ref = NULL;
6389 *base_commit_id = NULL;
6390 *fileindex = NULL;
6392 err = lock_worktree(worktree, LOCK_EX);
6393 if (err)
6394 return err;
6396 err = open_fileindex(fileindex, &fileindex_path, worktree);
6397 if (err)
6398 goto done;
6400 ok_arg.worktree = worktree;
6401 ok_arg.repo = repo;
6402 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6403 &ok_arg);
6404 if (err)
6405 goto done;
6407 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6408 if (err)
6409 goto done;
6411 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6412 if (err)
6413 goto done;
6415 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6416 worktree);
6417 if (err)
6418 goto done;
6420 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6421 0);
6422 if (err)
6423 goto done;
6425 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6426 if (err)
6427 goto done;
6429 err = got_ref_write(*branch_ref, repo);
6430 if (err)
6431 goto done;
6433 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6434 worktree->base_commit_id);
6435 if (err)
6436 goto done;
6437 err = got_ref_write(base_commit_ref, repo);
6438 if (err)
6439 goto done;
6440 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6441 if (*base_commit_id == NULL) {
6442 err = got_error_from_errno("got_object_id_dup");
6443 goto done;
6446 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6447 worktree->base_commit_id);
6448 if (err)
6449 goto done;
6450 err = got_ref_write(*tmp_branch, repo);
6451 if (err)
6452 goto done;
6454 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6455 if (err)
6456 goto done;
6457 done:
6458 free(fileindex_path);
6459 free(tmp_branch_name);
6460 free(branch_ref_name);
6461 free(base_commit_ref_name);
6462 if (wt_branch)
6463 got_ref_close(wt_branch);
6464 if (err) {
6465 if (*branch_ref) {
6466 got_ref_close(*branch_ref);
6467 *branch_ref = NULL;
6469 if (*tmp_branch) {
6470 got_ref_close(*tmp_branch);
6471 *tmp_branch = NULL;
6473 free(*base_commit_id);
6474 if (*fileindex) {
6475 got_fileindex_free(*fileindex);
6476 *fileindex = NULL;
6478 lock_worktree(worktree, LOCK_SH);
6480 return err;
6483 const struct got_error *
6484 got_worktree_histedit_postpone(struct got_worktree *worktree,
6485 struct got_fileindex *fileindex)
6487 if (fileindex)
6488 got_fileindex_free(fileindex);
6489 return lock_worktree(worktree, LOCK_SH);
6492 const struct got_error *
6493 got_worktree_histedit_in_progress(int *in_progress,
6494 struct got_worktree *worktree)
6496 const struct got_error *err;
6497 char *tmp_branch_name = NULL;
6499 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6500 if (err)
6501 return err;
6503 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6504 free(tmp_branch_name);
6505 return NULL;
6508 const struct got_error *
6509 got_worktree_histedit_continue(struct got_object_id **commit_id,
6510 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6511 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6512 struct got_worktree *worktree, struct got_repository *repo)
6514 const struct got_error *err;
6515 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6516 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6517 struct got_reference *commit_ref = NULL;
6518 struct got_reference *base_commit_ref = NULL;
6519 char *fileindex_path = NULL;
6520 int have_staged_files = 0;
6522 *commit_id = NULL;
6523 *tmp_branch = NULL;
6524 *base_commit_id = NULL;
6525 *fileindex = NULL;
6527 err = lock_worktree(worktree, LOCK_EX);
6528 if (err)
6529 return err;
6531 err = open_fileindex(fileindex, &fileindex_path, worktree);
6532 if (err)
6533 goto done;
6535 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6536 &have_staged_files);
6537 if (err && err->code != GOT_ERR_CANCELLED)
6538 goto done;
6539 if (have_staged_files) {
6540 err = got_error(GOT_ERR_STAGED_PATHS);
6541 goto done;
6544 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6545 if (err)
6546 goto done;
6548 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6549 if (err)
6550 goto done;
6552 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6553 if (err)
6554 goto done;
6556 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6557 worktree);
6558 if (err)
6559 goto done;
6561 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6562 if (err)
6563 goto done;
6565 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6566 if (err)
6567 goto done;
6568 err = got_ref_resolve(commit_id, repo, commit_ref);
6569 if (err)
6570 goto done;
6572 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6573 if (err)
6574 goto done;
6575 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6576 if (err)
6577 goto done;
6579 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6580 if (err)
6581 goto done;
6582 done:
6583 free(commit_ref_name);
6584 free(branch_ref_name);
6585 free(fileindex_path);
6586 if (commit_ref)
6587 got_ref_close(commit_ref);
6588 if (base_commit_ref)
6589 got_ref_close(base_commit_ref);
6590 if (err) {
6591 free(*commit_id);
6592 *commit_id = NULL;
6593 free(*base_commit_id);
6594 *base_commit_id = NULL;
6595 if (*tmp_branch) {
6596 got_ref_close(*tmp_branch);
6597 *tmp_branch = NULL;
6599 if (*fileindex) {
6600 got_fileindex_free(*fileindex);
6601 *fileindex = NULL;
6603 lock_worktree(worktree, LOCK_EX);
6605 return err;
6608 static const struct got_error *
6609 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6611 const struct got_error *err;
6612 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6613 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6615 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6616 if (err)
6617 goto done;
6618 err = delete_ref(tmp_branch_name, repo);
6619 if (err)
6620 goto done;
6622 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6623 worktree);
6624 if (err)
6625 goto done;
6626 err = delete_ref(base_commit_ref_name, repo);
6627 if (err)
6628 goto done;
6630 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6631 if (err)
6632 goto done;
6633 err = delete_ref(branch_ref_name, repo);
6634 if (err)
6635 goto done;
6637 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6638 if (err)
6639 goto done;
6640 err = delete_ref(commit_ref_name, repo);
6641 if (err)
6642 goto done;
6643 done:
6644 free(tmp_branch_name);
6645 free(base_commit_ref_name);
6646 free(branch_ref_name);
6647 free(commit_ref_name);
6648 return err;
6651 const struct got_error *
6652 got_worktree_histedit_abort(struct got_worktree *worktree,
6653 struct got_fileindex *fileindex, struct got_repository *repo,
6654 struct got_reference *branch, struct got_object_id *base_commit_id,
6655 got_worktree_checkout_cb progress_cb, void *progress_arg)
6657 const struct got_error *err, *unlockerr, *sync_err;
6658 struct got_reference *resolved = NULL;
6659 char *fileindex_path = NULL;
6660 struct got_object_id *tree_id = NULL;
6661 struct revert_file_args rfa;
6663 err = lock_worktree(worktree, LOCK_EX);
6664 if (err)
6665 return err;
6667 err = got_ref_open(&resolved, repo,
6668 got_ref_get_symref_target(branch), 0);
6669 if (err)
6670 goto done;
6672 err = got_worktree_set_head_ref(worktree, resolved);
6673 if (err)
6674 goto done;
6676 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6677 if (err)
6678 goto done;
6680 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6681 worktree->path_prefix);
6682 if (err)
6683 goto done;
6685 err = delete_histedit_refs(worktree, repo);
6686 if (err)
6687 goto done;
6689 err = get_fileindex_path(&fileindex_path, worktree);
6690 if (err)
6691 goto done;
6693 rfa.worktree = worktree;
6694 rfa.fileindex = fileindex;
6695 rfa.progress_cb = progress_cb;
6696 rfa.progress_arg = progress_arg;
6697 rfa.patch_cb = NULL;
6698 rfa.patch_arg = NULL;
6699 rfa.repo = repo;
6700 err = worktree_status(worktree, "", fileindex, repo,
6701 revert_file, &rfa, NULL, NULL, 0, 0);
6702 if (err)
6703 goto sync;
6705 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6706 repo, progress_cb, progress_arg, NULL, NULL);
6707 sync:
6708 sync_err = sync_fileindex(fileindex, fileindex_path);
6709 if (sync_err && err == NULL)
6710 err = sync_err;
6711 done:
6712 got_ref_close(resolved);
6713 free(tree_id);
6714 free(fileindex_path);
6716 unlockerr = lock_worktree(worktree, LOCK_SH);
6717 if (unlockerr && err == NULL)
6718 err = unlockerr;
6719 return err;
6722 const struct got_error *
6723 got_worktree_histedit_complete(struct got_worktree *worktree,
6724 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6725 struct got_reference *edited_branch, struct got_repository *repo)
6727 const struct got_error *err, *unlockerr;
6728 struct got_object_id *new_head_commit_id = NULL;
6729 struct got_reference *resolved = NULL;
6731 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6732 if (err)
6733 return err;
6735 err = got_ref_open(&resolved, repo,
6736 got_ref_get_symref_target(edited_branch), 0);
6737 if (err)
6738 goto done;
6740 err = got_ref_change_ref(resolved, new_head_commit_id);
6741 if (err)
6742 goto done;
6744 err = got_ref_write(resolved, repo);
6745 if (err)
6746 goto done;
6748 err = got_worktree_set_head_ref(worktree, resolved);
6749 if (err)
6750 goto done;
6752 err = delete_histedit_refs(worktree, repo);
6753 done:
6754 if (fileindex)
6755 got_fileindex_free(fileindex);
6756 free(new_head_commit_id);
6757 unlockerr = lock_worktree(worktree, LOCK_SH);
6758 if (unlockerr && err == NULL)
6759 err = unlockerr;
6760 return err;
6763 const struct got_error *
6764 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6765 struct got_object_id *commit_id, struct got_repository *repo)
6767 const struct got_error *err;
6768 char *commit_ref_name;
6770 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6771 if (err)
6772 return err;
6774 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6775 if (err)
6776 goto done;
6778 err = delete_ref(commit_ref_name, repo);
6779 done:
6780 free(commit_ref_name);
6781 return err;
6784 const struct got_error *
6785 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6786 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6787 struct got_worktree *worktree, const char *refname,
6788 struct got_repository *repo)
6790 const struct got_error *err = NULL;
6791 char *fileindex_path = NULL;
6792 struct check_rebase_ok_arg ok_arg;
6794 *fileindex = NULL;
6795 *branch_ref = NULL;
6796 *base_branch_ref = NULL;
6798 err = lock_worktree(worktree, LOCK_EX);
6799 if (err)
6800 return err;
6802 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6803 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6804 "cannot integrate a branch into itself; "
6805 "update -b or different branch name required");
6806 goto done;
6809 err = open_fileindex(fileindex, &fileindex_path, worktree);
6810 if (err)
6811 goto done;
6813 /* Preconditions are the same as for rebase. */
6814 ok_arg.worktree = worktree;
6815 ok_arg.repo = repo;
6816 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6817 &ok_arg);
6818 if (err)
6819 goto done;
6821 err = got_ref_open(branch_ref, repo, refname, 1);
6822 if (err)
6823 goto done;
6825 err = got_ref_open(base_branch_ref, repo,
6826 got_worktree_get_head_ref_name(worktree), 1);
6827 done:
6828 if (err) {
6829 if (*branch_ref) {
6830 got_ref_close(*branch_ref);
6831 *branch_ref = NULL;
6833 if (*base_branch_ref) {
6834 got_ref_close(*base_branch_ref);
6835 *base_branch_ref = NULL;
6837 if (*fileindex) {
6838 got_fileindex_free(*fileindex);
6839 *fileindex = NULL;
6841 lock_worktree(worktree, LOCK_SH);
6843 return err;
6846 const struct got_error *
6847 got_worktree_integrate_continue(struct got_worktree *worktree,
6848 struct got_fileindex *fileindex, struct got_repository *repo,
6849 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6850 got_worktree_checkout_cb progress_cb, void *progress_arg,
6851 got_cancel_cb cancel_cb, void *cancel_arg)
6853 const struct got_error *err = NULL, *sync_err, *unlockerr;
6854 char *fileindex_path = NULL;
6855 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6857 err = get_fileindex_path(&fileindex_path, worktree);
6858 if (err)
6859 goto done;
6861 err = got_ref_resolve(&commit_id, repo, branch_ref);
6862 if (err)
6863 goto done;
6865 err = got_object_id_by_path(&tree_id, repo, commit_id,
6866 worktree->path_prefix);
6867 if (err)
6868 goto done;
6870 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6871 if (err)
6872 goto done;
6874 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6875 progress_cb, progress_arg, cancel_cb, cancel_arg);
6876 if (err)
6877 goto sync;
6879 err = got_ref_change_ref(base_branch_ref, commit_id);
6880 if (err)
6881 goto sync;
6883 err = got_ref_write(base_branch_ref, repo);
6884 sync:
6885 sync_err = sync_fileindex(fileindex, fileindex_path);
6886 if (sync_err && err == NULL)
6887 err = sync_err;
6889 done:
6890 unlockerr = got_ref_unlock(branch_ref);
6891 if (unlockerr && err == NULL)
6892 err = unlockerr;
6893 got_ref_close(branch_ref);
6895 unlockerr = got_ref_unlock(base_branch_ref);
6896 if (unlockerr && err == NULL)
6897 err = unlockerr;
6898 got_ref_close(base_branch_ref);
6900 got_fileindex_free(fileindex);
6901 free(fileindex_path);
6902 free(tree_id);
6904 unlockerr = lock_worktree(worktree, LOCK_SH);
6905 if (unlockerr && err == NULL)
6906 err = unlockerr;
6907 return err;
6910 const struct got_error *
6911 got_worktree_integrate_abort(struct got_worktree *worktree,
6912 struct got_fileindex *fileindex, struct got_repository *repo,
6913 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6915 const struct got_error *err = NULL, *unlockerr = NULL;
6917 got_fileindex_free(fileindex);
6919 err = lock_worktree(worktree, LOCK_SH);
6921 unlockerr = got_ref_unlock(branch_ref);
6922 if (unlockerr && err == NULL)
6923 err = unlockerr;
6924 got_ref_close(branch_ref);
6926 unlockerr = got_ref_unlock(base_branch_ref);
6927 if (unlockerr && err == NULL)
6928 err = unlockerr;
6929 got_ref_close(base_branch_ref);
6931 return err;
6934 struct check_stage_ok_arg {
6935 struct got_object_id *head_commit_id;
6936 struct got_worktree *worktree;
6937 struct got_fileindex *fileindex;
6938 struct got_repository *repo;
6939 int have_changes;
6942 const struct got_error *
6943 check_stage_ok(void *arg, unsigned char status,
6944 unsigned char staged_status, const char *relpath,
6945 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6946 struct got_object_id *commit_id, int dirfd, const char *de_name)
6948 struct check_stage_ok_arg *a = arg;
6949 const struct got_error *err = NULL;
6950 struct got_fileindex_entry *ie;
6951 struct got_object_id base_commit_id;
6952 struct got_object_id *base_commit_idp = NULL;
6953 char *in_repo_path = NULL, *p;
6955 if (status == GOT_STATUS_UNVERSIONED ||
6956 status == GOT_STATUS_NO_CHANGE)
6957 return NULL;
6958 if (status == GOT_STATUS_NONEXISTENT)
6959 return got_error_set_errno(ENOENT, relpath);
6961 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6962 if (ie == NULL)
6963 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6965 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6966 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6967 relpath) == -1)
6968 return got_error_from_errno("asprintf");
6970 if (got_fileindex_entry_has_commit(ie)) {
6971 memcpy(base_commit_id.sha1, ie->commit_sha1,
6972 SHA1_DIGEST_LENGTH);
6973 base_commit_idp = &base_commit_id;
6976 if (status == GOT_STATUS_CONFLICT) {
6977 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6978 goto done;
6979 } else if (status != GOT_STATUS_ADD &&
6980 status != GOT_STATUS_MODIFY &&
6981 status != GOT_STATUS_DELETE) {
6982 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6983 goto done;
6986 a->have_changes = 1;
6988 p = in_repo_path;
6989 while (p[0] == '/')
6990 p++;
6991 err = check_out_of_date(p, status, staged_status,
6992 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6993 GOT_ERR_STAGE_OUT_OF_DATE);
6994 done:
6995 free(in_repo_path);
6996 return err;
6999 struct stage_path_arg {
7000 struct got_worktree *worktree;
7001 struct got_fileindex *fileindex;
7002 struct got_repository *repo;
7003 got_worktree_status_cb status_cb;
7004 void *status_arg;
7005 got_worktree_patch_cb patch_cb;
7006 void *patch_arg;
7007 int staged_something;
7010 static const struct got_error *
7011 stage_path(void *arg, unsigned char status,
7012 unsigned char staged_status, const char *relpath,
7013 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7014 struct got_object_id *commit_id, int dirfd, const char *de_name)
7016 struct stage_path_arg *a = arg;
7017 const struct got_error *err = NULL;
7018 struct got_fileindex_entry *ie;
7019 char *ondisk_path = NULL, *path_content = NULL;
7020 uint32_t stage;
7021 struct got_object_id *new_staged_blob_id = NULL;
7023 if (status == GOT_STATUS_UNVERSIONED)
7024 return NULL;
7026 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7027 if (ie == NULL)
7028 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7030 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7031 relpath)== -1)
7032 return got_error_from_errno("asprintf");
7034 switch (status) {
7035 case GOT_STATUS_ADD:
7036 case GOT_STATUS_MODIFY:
7037 if (a->patch_cb) {
7038 if (status == GOT_STATUS_ADD) {
7039 int choice = GOT_PATCH_CHOICE_NONE;
7040 err = (*a->patch_cb)(&choice, a->patch_arg,
7041 status, ie->path, NULL, 1, 1);
7042 if (err)
7043 break;
7044 if (choice != GOT_PATCH_CHOICE_YES)
7045 break;
7046 } else {
7047 err = create_patched_content(&path_content, 0,
7048 staged_blob_id ? staged_blob_id : blob_id,
7049 ondisk_path, dirfd, de_name, ie->path,
7050 a->repo, a->patch_cb, a->patch_arg);
7051 if (err || path_content == NULL)
7052 break;
7055 err = got_object_blob_create(&new_staged_blob_id,
7056 path_content ? path_content : ondisk_path, a->repo);
7057 if (err)
7058 break;
7059 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7060 SHA1_DIGEST_LENGTH);
7061 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7062 stage = GOT_FILEIDX_STAGE_ADD;
7063 else
7064 stage = GOT_FILEIDX_STAGE_MODIFY;
7065 got_fileindex_entry_stage_set(ie, stage);
7066 a->staged_something = 1;
7067 if (a->status_cb == NULL)
7068 break;
7069 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7070 get_staged_status(ie), relpath, blob_id,
7071 new_staged_blob_id, NULL, dirfd, de_name);
7072 break;
7073 case GOT_STATUS_DELETE:
7074 if (staged_status == GOT_STATUS_DELETE)
7075 break;
7076 if (a->patch_cb) {
7077 int choice = GOT_PATCH_CHOICE_NONE;
7078 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7079 ie->path, NULL, 1, 1);
7080 if (err)
7081 break;
7082 if (choice == GOT_PATCH_CHOICE_NO)
7083 break;
7084 if (choice != GOT_PATCH_CHOICE_YES) {
7085 err = got_error(GOT_ERR_PATCH_CHOICE);
7086 break;
7089 stage = GOT_FILEIDX_STAGE_DELETE;
7090 got_fileindex_entry_stage_set(ie, stage);
7091 a->staged_something = 1;
7092 if (a->status_cb == NULL)
7093 break;
7094 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7095 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7096 de_name);
7097 break;
7098 case GOT_STATUS_NO_CHANGE:
7099 break;
7100 case GOT_STATUS_CONFLICT:
7101 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7102 break;
7103 case GOT_STATUS_NONEXISTENT:
7104 err = got_error_set_errno(ENOENT, relpath);
7105 break;
7106 default:
7107 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7108 break;
7111 if (path_content && unlink(path_content) == -1 && err == NULL)
7112 err = got_error_from_errno2("unlink", path_content);
7113 free(path_content);
7114 free(ondisk_path);
7115 free(new_staged_blob_id);
7116 return err;
7119 const struct got_error *
7120 got_worktree_stage(struct got_worktree *worktree,
7121 struct got_pathlist_head *paths,
7122 got_worktree_status_cb status_cb, void *status_arg,
7123 got_worktree_patch_cb patch_cb, void *patch_arg,
7124 struct got_repository *repo)
7126 const struct got_error *err = NULL, *sync_err, *unlockerr;
7127 struct got_pathlist_entry *pe;
7128 struct got_fileindex *fileindex = NULL;
7129 char *fileindex_path = NULL;
7130 struct got_reference *head_ref = NULL;
7131 struct got_object_id *head_commit_id = NULL;
7132 struct check_stage_ok_arg oka;
7133 struct stage_path_arg spa;
7135 err = lock_worktree(worktree, LOCK_EX);
7136 if (err)
7137 return err;
7139 err = got_ref_open(&head_ref, repo,
7140 got_worktree_get_head_ref_name(worktree), 0);
7141 if (err)
7142 goto done;
7143 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7144 if (err)
7145 goto done;
7146 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7147 if (err)
7148 goto done;
7150 /* Check pre-conditions before staging anything. */
7151 oka.head_commit_id = head_commit_id;
7152 oka.worktree = worktree;
7153 oka.fileindex = fileindex;
7154 oka.repo = repo;
7155 oka.have_changes = 0;
7156 TAILQ_FOREACH(pe, paths, entry) {
7157 err = worktree_status(worktree, pe->path, fileindex, repo,
7158 check_stage_ok, &oka, NULL, NULL, 0, 0);
7159 if (err)
7160 goto done;
7162 if (!oka.have_changes) {
7163 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7164 goto done;
7167 spa.worktree = worktree;
7168 spa.fileindex = fileindex;
7169 spa.repo = repo;
7170 spa.patch_cb = patch_cb;
7171 spa.patch_arg = patch_arg;
7172 spa.status_cb = status_cb;
7173 spa.status_arg = status_arg;
7174 spa.staged_something = 0;
7175 TAILQ_FOREACH(pe, paths, entry) {
7176 err = worktree_status(worktree, pe->path, fileindex, repo,
7177 stage_path, &spa, NULL, NULL, 0, 0);
7178 if (err)
7179 goto done;
7181 if (!spa.staged_something) {
7182 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7183 goto done;
7186 sync_err = sync_fileindex(fileindex, fileindex_path);
7187 if (sync_err && err == NULL)
7188 err = sync_err;
7189 done:
7190 if (head_ref)
7191 got_ref_close(head_ref);
7192 free(head_commit_id);
7193 free(fileindex_path);
7194 if (fileindex)
7195 got_fileindex_free(fileindex);
7196 unlockerr = lock_worktree(worktree, LOCK_SH);
7197 if (unlockerr && err == NULL)
7198 err = unlockerr;
7199 return err;
7202 struct unstage_path_arg {
7203 struct got_worktree *worktree;
7204 struct got_fileindex *fileindex;
7205 struct got_repository *repo;
7206 got_worktree_checkout_cb progress_cb;
7207 void *progress_arg;
7208 got_worktree_patch_cb patch_cb;
7209 void *patch_arg;
7212 static const struct got_error *
7213 create_unstaged_content(char **path_unstaged_content,
7214 char **path_new_staged_content, struct got_object_id *blob_id,
7215 struct got_object_id *staged_blob_id, const char *relpath,
7216 struct got_repository *repo,
7217 got_worktree_patch_cb patch_cb, void *patch_arg)
7219 const struct got_error *err;
7220 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7221 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7222 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7223 struct stat sb1, sb2;
7224 struct got_diff_changes *changes = NULL;
7225 struct got_diff_state *ds = NULL;
7226 struct got_diff_args *args = NULL;
7227 struct got_diff_change *change;
7228 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7229 int have_content = 0, have_rejected_content = 0;
7231 *path_unstaged_content = NULL;
7232 *path_new_staged_content = NULL;
7234 err = got_object_id_str(&label1, blob_id);
7235 if (err)
7236 return err;
7237 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7238 if (err)
7239 goto done;
7241 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7242 if (err)
7243 goto done;
7245 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7246 if (err)
7247 goto done;
7249 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7250 if (err)
7251 goto done;
7253 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7254 if (err)
7255 goto done;
7257 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7258 if (err)
7259 goto done;
7261 if (stat(path1, &sb1) == -1) {
7262 err = got_error_from_errno2("stat", path1);
7263 goto done;
7266 if (stat(path2, &sb2) == -1) {
7267 err = got_error_from_errno2("stat", path2);
7268 goto done;
7271 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7272 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7273 if (err)
7274 goto done;
7276 err = got_opentemp_named(path_unstaged_content, &outfile,
7277 "got-unstaged-content");
7278 if (err)
7279 goto done;
7280 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7281 "got-new-staged-content");
7282 if (err)
7283 goto done;
7285 if (fseek(f1, 0L, SEEK_SET) == -1) {
7286 err = got_ferror(f1, GOT_ERR_IO);
7287 goto done;
7289 if (fseek(f2, 0L, SEEK_SET) == -1) {
7290 err = got_ferror(f2, GOT_ERR_IO);
7291 goto done;
7293 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7294 int choice;
7295 err = apply_or_reject_change(&choice, change, ++n,
7296 changes->nchanges, ds, args, diff_flags, relpath,
7297 f1, f2, &line_cur1, &line_cur2,
7298 outfile, rejectfile, patch_cb, patch_arg);
7299 if (err)
7300 goto done;
7301 if (choice == GOT_PATCH_CHOICE_YES)
7302 have_content = 1;
7303 else
7304 have_rejected_content = 1;
7305 if (choice == GOT_PATCH_CHOICE_QUIT)
7306 break;
7308 if (have_content || have_rejected_content)
7309 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7310 outfile, rejectfile);
7311 done:
7312 free(label1);
7313 if (blob)
7314 got_object_blob_close(blob);
7315 if (staged_blob)
7316 got_object_blob_close(staged_blob);
7317 if (f1 && fclose(f1) == EOF && err == NULL)
7318 err = got_error_from_errno2("fclose", path1);
7319 if (f2 && fclose(f2) == EOF && err == NULL)
7320 err = got_error_from_errno2("fclose", path2);
7321 if (outfile && fclose(outfile) == EOF && err == NULL)
7322 err = got_error_from_errno2("fclose", *path_unstaged_content);
7323 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7324 err = got_error_from_errno2("fclose", *path_new_staged_content);
7325 if (path1 && unlink(path1) == -1 && err == NULL)
7326 err = got_error_from_errno2("unlink", path1);
7327 if (path2 && unlink(path2) == -1 && err == NULL)
7328 err = got_error_from_errno2("unlink", path2);
7329 if (err || !have_content) {
7330 if (*path_unstaged_content &&
7331 unlink(*path_unstaged_content) == -1 && err == NULL)
7332 err = got_error_from_errno2("unlink",
7333 *path_unstaged_content);
7334 free(*path_unstaged_content);
7335 *path_unstaged_content = NULL;
7337 if (err || !have_rejected_content) {
7338 if (*path_new_staged_content &&
7339 unlink(*path_new_staged_content) == -1 && err == NULL)
7340 err = got_error_from_errno2("unlink",
7341 *path_new_staged_content);
7342 free(*path_new_staged_content);
7343 *path_new_staged_content = NULL;
7345 free(args);
7346 if (ds) {
7347 got_diff_state_free(ds);
7348 free(ds);
7350 if (changes)
7351 got_diff_free_changes(changes);
7352 free(path1);
7353 free(path2);
7354 return err;
7357 static const struct got_error *
7358 unstage_path(void *arg, unsigned char status,
7359 unsigned char staged_status, const char *relpath,
7360 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7361 struct got_object_id *commit_id, int dirfd, const char *de_name)
7363 const struct got_error *err = NULL;
7364 struct unstage_path_arg *a = arg;
7365 struct got_fileindex_entry *ie;
7366 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7367 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7368 char *path_new_staged_content = NULL;
7369 char *id_str = NULL, *label_orig = NULL;
7370 int local_changes_subsumed;
7371 struct stat sb;
7373 if (staged_status != GOT_STATUS_ADD &&
7374 staged_status != GOT_STATUS_MODIFY &&
7375 staged_status != GOT_STATUS_DELETE)
7376 return NULL;
7378 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7379 if (ie == NULL)
7380 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7382 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7383 == -1)
7384 return got_error_from_errno("asprintf");
7386 err = got_object_id_str(&id_str,
7387 commit_id ? commit_id : a->worktree->base_commit_id);
7388 if (err)
7389 goto done;
7390 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7391 id_str) == -1) {
7392 err = got_error_from_errno("asprintf");
7393 goto done;
7396 switch (staged_status) {
7397 case GOT_STATUS_MODIFY:
7398 err = got_object_open_as_blob(&blob_base, a->repo,
7399 blob_id, 8192);
7400 if (err)
7401 break;
7402 /* fall through */
7403 case GOT_STATUS_ADD:
7404 if (a->patch_cb) {
7405 if (staged_status == GOT_STATUS_ADD) {
7406 int choice = GOT_PATCH_CHOICE_NONE;
7407 err = (*a->patch_cb)(&choice, a->patch_arg,
7408 staged_status, ie->path, NULL, 1, 1);
7409 if (err)
7410 break;
7411 if (choice != GOT_PATCH_CHOICE_YES)
7412 break;
7413 } else {
7414 err = create_unstaged_content(
7415 &path_unstaged_content,
7416 &path_new_staged_content, blob_id,
7417 staged_blob_id, ie->path, a->repo,
7418 a->patch_cb, a->patch_arg);
7419 if (err || path_unstaged_content == NULL)
7420 break;
7421 if (path_new_staged_content) {
7422 err = got_object_blob_create(
7423 &staged_blob_id,
7424 path_new_staged_content,
7425 a->repo);
7426 if (err)
7427 break;
7428 memcpy(ie->staged_blob_sha1,
7429 staged_blob_id->sha1,
7430 SHA1_DIGEST_LENGTH);
7432 err = merge_file(&local_changes_subsumed,
7433 a->worktree, blob_base, ondisk_path,
7434 relpath, got_fileindex_perms_to_st(ie),
7435 path_unstaged_content, label_orig,
7436 "unstaged", a->repo, a->progress_cb,
7437 a->progress_arg);
7438 if (err == NULL &&
7439 path_new_staged_content == NULL)
7440 got_fileindex_entry_stage_set(ie,
7441 GOT_FILEIDX_STAGE_NONE);
7442 break; /* Done with this file. */
7445 err = got_object_open_as_blob(&blob_staged, a->repo,
7446 staged_blob_id, 8192);
7447 if (err)
7448 break;
7449 err = merge_blob(&local_changes_subsumed, a->worktree,
7450 blob_base, ondisk_path, relpath,
7451 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7452 commit_id ? commit_id : a->worktree->base_commit_id,
7453 a->repo, a->progress_cb, a->progress_arg);
7454 if (err == NULL)
7455 got_fileindex_entry_stage_set(ie,
7456 GOT_FILEIDX_STAGE_NONE);
7457 break;
7458 case GOT_STATUS_DELETE:
7459 if (a->patch_cb) {
7460 int choice = GOT_PATCH_CHOICE_NONE;
7461 err = (*a->patch_cb)(&choice, a->patch_arg,
7462 staged_status, ie->path, NULL, 1, 1);
7463 if (err)
7464 break;
7465 if (choice == GOT_PATCH_CHOICE_NO)
7466 break;
7467 if (choice != GOT_PATCH_CHOICE_YES) {
7468 err = got_error(GOT_ERR_PATCH_CHOICE);
7469 break;
7472 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7473 err = get_file_status(&status, &sb, ie, ondisk_path,
7474 dirfd, de_name, a->repo);
7475 if (err)
7476 break;
7477 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7478 break;
7480 done:
7481 free(ondisk_path);
7482 if (path_unstaged_content &&
7483 unlink(path_unstaged_content) == -1 && err == NULL)
7484 err = got_error_from_errno2("unlink", path_unstaged_content);
7485 if (path_new_staged_content &&
7486 unlink(path_new_staged_content) == -1 && err == NULL)
7487 err = got_error_from_errno2("unlink", path_new_staged_content);
7488 free(path_unstaged_content);
7489 free(path_new_staged_content);
7490 if (blob_base)
7491 got_object_blob_close(blob_base);
7492 if (blob_staged)
7493 got_object_blob_close(blob_staged);
7494 free(id_str);
7495 free(label_orig);
7496 return err;
7499 const struct got_error *
7500 got_worktree_unstage(struct got_worktree *worktree,
7501 struct got_pathlist_head *paths,
7502 got_worktree_checkout_cb progress_cb, void *progress_arg,
7503 got_worktree_patch_cb patch_cb, void *patch_arg,
7504 struct got_repository *repo)
7506 const struct got_error *err = NULL, *sync_err, *unlockerr;
7507 struct got_pathlist_entry *pe;
7508 struct got_fileindex *fileindex = NULL;
7509 char *fileindex_path = NULL;
7510 struct unstage_path_arg upa;
7512 err = lock_worktree(worktree, LOCK_EX);
7513 if (err)
7514 return err;
7516 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7517 if (err)
7518 goto done;
7520 upa.worktree = worktree;
7521 upa.fileindex = fileindex;
7522 upa.repo = repo;
7523 upa.progress_cb = progress_cb;
7524 upa.progress_arg = progress_arg;
7525 upa.patch_cb = patch_cb;
7526 upa.patch_arg = patch_arg;
7527 TAILQ_FOREACH(pe, paths, entry) {
7528 err = worktree_status(worktree, pe->path, fileindex, repo,
7529 unstage_path, &upa, NULL, NULL, 0, 0);
7530 if (err)
7531 goto done;
7534 sync_err = sync_fileindex(fileindex, fileindex_path);
7535 if (sync_err && err == NULL)
7536 err = sync_err;
7537 done:
7538 free(fileindex_path);
7539 if (fileindex)
7540 got_fileindex_free(fileindex);
7541 unlockerr = lock_worktree(worktree, LOCK_SH);
7542 if (unlockerr && err == NULL)
7543 err = unlockerr;
7544 return err;