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, 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 ondisk_len = readlink(ondisk_path, ondisk_target,
999 sizeof(ondisk_target));
1000 if (ondisk_len == -1) {
1001 err = got_error_from_errno2("readlink",
1002 ondisk_path);
1003 goto done;
1005 ondisk_target[ondisk_len] = '\0';
1007 if (blob_orig) {
1008 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1009 if (err)
1010 goto done;
1013 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
1014 if (err)
1015 goto done;
1017 if (ancestor_target == NULL ||
1018 (ondisk_len != strlen(ancestor_target) ||
1019 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1020 have_local_change = 1;
1022 deriv_len = strlen(deriv_target);
1023 if (ancestor_target == NULL ||
1024 (deriv_len != strlen(ancestor_target) ||
1025 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1026 have_incoming_change = 1;
1028 if (!have_local_change && !have_incoming_change) {
1029 if (ancestor_target) {
1030 /* Both sides made the same change. */
1031 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1032 path);
1033 } else if (deriv_len == ondisk_len &&
1034 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1035 /* Both sides added the same symlink. */
1036 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1037 path);
1038 } else {
1039 /* Both sides added symlinks which don't match. */
1040 err = install_symlink_conflict(deriv_target,
1041 deriv_base_commit_id, ancestor_target,
1042 label_orig, ondisk_target, ondisk_path);
1043 if (err)
1044 goto done;
1045 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1046 path);
1048 } else if (!have_local_change && have_incoming_change) {
1049 /* Apply the incoming change. */
1050 err = update_symlink(ondisk_path, deriv_target,
1051 strlen(deriv_target));
1052 if (err)
1053 goto done;
1054 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1055 } else if (have_local_change && have_incoming_change) {
1056 if (deriv_len == ondisk_len &&
1057 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1058 /* Both sides made the same change. */
1059 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1060 path);
1061 } else {
1062 err = install_symlink_conflict(deriv_target,
1063 deriv_base_commit_id, ancestor_target, label_orig,
1064 ondisk_target, ondisk_path);
1065 if (err)
1066 goto done;
1067 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1068 path);
1072 done:
1073 free(ancestor_target);
1074 free(deriv_target);
1075 return err;
1079 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1080 * blob_deriv acts as the first derived version, and the file on disk
1081 * acts as the second derived version.
1083 static const struct got_error *
1084 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1085 struct got_blob_object *blob_orig, const char *ondisk_path,
1086 const char *path, uint16_t st_mode, const char *label_orig,
1087 struct got_blob_object *blob_deriv,
1088 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1089 got_worktree_checkout_cb progress_cb, void *progress_arg)
1091 const struct got_error *err = NULL;
1092 FILE *f_deriv = NULL;
1093 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1094 char *label_deriv = NULL, *parent;
1096 *local_changes_subsumed = 0;
1098 parent = dirname(ondisk_path);
1099 if (parent == NULL)
1100 return got_error_from_errno2("dirname", ondisk_path);
1102 free(base_path);
1103 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1104 err = got_error_from_errno("asprintf");
1105 base_path = NULL;
1106 goto done;
1109 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1110 if (err)
1111 goto done;
1112 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1113 blob_deriv);
1114 if (err)
1115 goto done;
1117 err = got_object_id_str(&id_str, deriv_base_commit_id);
1118 if (err)
1119 goto done;
1120 if (asprintf(&label_deriv, "%s: commit %s",
1121 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1122 err = got_error_from_errno("asprintf");
1123 goto done;
1126 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1127 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1128 label_deriv, repo, progress_cb, progress_arg);
1129 done:
1130 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1131 err = got_error_from_errno("fclose");
1132 free(base_path);
1133 if (blob_deriv_path) {
1134 unlink(blob_deriv_path);
1135 free(blob_deriv_path);
1137 free(id_str);
1138 free(label_deriv);
1139 return err;
1142 static const struct got_error *
1143 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1144 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1145 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1147 const struct got_error *err = NULL;
1148 struct got_fileindex_entry *new_ie;
1150 *new_iep = NULL;
1152 err = got_fileindex_entry_alloc(&new_ie, path);
1153 if (err)
1154 return err;
1156 err = got_fileindex_entry_update(new_ie, ondisk_path,
1157 blob_id->sha1, base_commit_id->sha1, 1);
1158 if (err)
1159 goto done;
1161 err = got_fileindex_entry_add(fileindex, new_ie);
1162 done:
1163 if (err)
1164 got_fileindex_entry_free(new_ie);
1165 else
1166 *new_iep = new_ie;
1167 return err;
1170 static mode_t
1171 get_ondisk_perms(int executable, mode_t st_mode)
1173 mode_t xbits = S_IXUSR;
1175 if (executable) {
1176 /* Map read bits to execute bits. */
1177 if (st_mode & S_IRGRP)
1178 xbits |= S_IXGRP;
1179 if (st_mode & S_IROTH)
1180 xbits |= S_IXOTH;
1181 return st_mode | xbits;
1184 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1187 /* forward declaration */
1188 static const struct got_error *
1189 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1190 const char *path, mode_t te_mode, mode_t st_mode,
1191 struct got_blob_object *blob, int restoring_missing_file,
1192 int reverting_versioned_file, int installing_bad_symlink,
1193 int path_is_unversioned, struct got_repository *repo,
1194 got_worktree_checkout_cb progress_cb, void *progress_arg);
1197 * This function assumes that the provided symlink target points at a
1198 * safe location in the work tree!
1200 static const struct got_error *
1201 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1202 size_t target_len)
1204 const struct got_error *err = NULL;
1205 ssize_t elen;
1206 char etarget[PATH_MAX];
1207 int fd;
1210 * "Bad" symlinks (those pointing outside the work tree or into the
1211 * .got directory) are installed in the work tree as a regular file
1212 * which contains the bad symlink target path.
1213 * The new symlink target has already been checked for safety by our
1214 * caller. If we can successfully open a regular file then we simply
1215 * replace this file with a symlink below.
1217 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1218 if (fd == -1) {
1219 if (errno != ELOOP)
1220 return got_error_from_errno2("open", ondisk_path);
1222 /* We are updating an existing on-disk symlink. */
1223 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1224 if (elen == -1)
1225 return got_error_from_errno2("readlink", ondisk_path);
1227 if (elen == target_len &&
1228 memcmp(etarget, target_path, target_len) == 0)
1229 return NULL; /* nothing to do */
1232 err = update_symlink(ondisk_path, target_path, target_len);
1233 if (fd != -1 && close(fd) == -1 && err == NULL)
1234 err = got_error_from_errno2("close", ondisk_path);
1235 return err;
1238 static const struct got_error *
1239 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1240 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1242 const struct got_error *err = NULL;
1243 char canonpath[PATH_MAX];
1244 char *path_got = NULL;
1246 *is_bad_symlink = 0;
1248 if (target_len >= sizeof(canonpath)) {
1249 *is_bad_symlink = 1;
1250 return NULL;
1254 * We do not use realpath(3) to resolve the symlink's target
1255 * path because we don't want to resolve symlinks recursively.
1256 * Instead we make the path absolute and then canonicalize it.
1257 * Relative symlink target lookup should begin at the directory
1258 * in which the blob object is being installed.
1260 if (!got_path_is_absolute(target_path)) {
1261 char *abspath;
1262 char *parent = dirname(ondisk_path);
1263 if (parent == NULL)
1264 return got_error_from_errno2("dirname", ondisk_path);
1265 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1266 return got_error_from_errno("asprintf");
1267 if (strlen(abspath) >= sizeof(canonpath)) {
1268 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1269 free(abspath);
1270 return err;
1272 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1273 free(abspath);
1274 if (err)
1275 return err;
1276 } else {
1277 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1278 if (err)
1279 return err;
1282 /* Only allow symlinks pointing at paths within the work tree. */
1283 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1284 *is_bad_symlink = 1;
1285 return NULL;
1288 /* Do not allow symlinks pointing into the .got directory. */
1289 if (asprintf(&path_got, "%s/%s", wtroot_path,
1290 GOT_WORKTREE_GOT_DIR) == -1)
1291 return got_error_from_errno("asprintf");
1292 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1293 *is_bad_symlink = 1;
1295 free(path_got);
1296 return NULL;
1299 static const struct got_error *
1300 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1301 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1302 int restoring_missing_file, int reverting_versioned_file,
1303 int path_is_unversioned, struct got_repository *repo,
1304 got_worktree_checkout_cb progress_cb, void *progress_arg)
1306 const struct got_error *err = NULL;
1307 char target_path[PATH_MAX];
1308 size_t len, target_len = 0;
1309 char *path_got = NULL;
1310 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1311 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1313 *is_bad_symlink = 0;
1316 * Blob object content specifies the target path of the link.
1317 * If a symbolic link cannot be installed we instead create
1318 * a regular file which contains the link target path stored
1319 * in the blob object.
1321 do {
1322 err = got_object_blob_read_block(&len, blob);
1323 if (len + target_len >= sizeof(target_path)) {
1324 /* Path too long; install as a regular file. */
1325 *is_bad_symlink = 1;
1326 got_object_blob_rewind(blob);
1327 return install_blob(worktree, ondisk_path, path,
1328 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1329 restoring_missing_file, reverting_versioned_file,
1330 1, path_is_unversioned, repo, progress_cb,
1331 progress_arg);
1333 if (len > 0) {
1334 /* Skip blob object header first time around. */
1335 memcpy(target_path + target_len, buf + hdrlen,
1336 len - hdrlen);
1337 target_len += len - hdrlen;
1338 hdrlen = 0;
1340 } while (len != 0);
1341 target_path[target_len] = '\0';
1343 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1344 ondisk_path, worktree->root_path);
1345 if (err)
1346 return err;
1348 if (*is_bad_symlink) {
1349 /* install as a regular file */
1350 *is_bad_symlink = 1;
1351 got_object_blob_rewind(blob);
1352 err = install_blob(worktree, ondisk_path, path,
1353 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1354 restoring_missing_file, reverting_versioned_file, 1,
1355 path_is_unversioned, repo, progress_cb, progress_arg);
1356 goto done;
1359 if (symlink(target_path, ondisk_path) == -1) {
1360 if (errno == EEXIST) {
1361 if (path_is_unversioned) {
1362 err = (*progress_cb)(progress_arg,
1363 GOT_STATUS_UNVERSIONED, path);
1364 goto done;
1366 err = replace_existing_symlink(ondisk_path,
1367 target_path, target_len);
1368 if (err)
1369 goto done;
1370 if (progress_cb) {
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1374 path);
1376 goto done; /* Nothing else to do. */
1379 if (errno == ENOENT) {
1380 char *parent = dirname(ondisk_path);
1381 if (parent == NULL) {
1382 err = got_error_from_errno2("dirname",
1383 ondisk_path);
1384 goto done;
1386 err = add_dir_on_disk(worktree, parent);
1387 if (err)
1388 goto done;
1390 * Retry, and fall through to error handling
1391 * below if this second attempt fails.
1393 if (symlink(target_path, ondisk_path) != -1) {
1394 err = NULL; /* success */
1395 goto done;
1399 /* Handle errors from first or second creation attempt. */
1400 if (errno == ENAMETOOLONG) {
1401 /* bad target path; install as a regular file */
1402 *is_bad_symlink = 1;
1403 got_object_blob_rewind(blob);
1404 err = install_blob(worktree, ondisk_path, path,
1405 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1406 restoring_missing_file, reverting_versioned_file, 1,
1407 path_is_unversioned, repo,
1408 progress_cb, progress_arg);
1409 } else if (errno == ENOTDIR) {
1410 err = got_error_path(ondisk_path,
1411 GOT_ERR_FILE_OBSTRUCTED);
1412 } else {
1413 err = got_error_from_errno3("symlink",
1414 target_path, ondisk_path);
1416 } else if (progress_cb)
1417 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1418 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1419 done:
1420 free(path_got);
1421 return err;
1424 static const struct got_error *
1425 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1426 const char *path, mode_t te_mode, mode_t st_mode,
1427 struct got_blob_object *blob, int restoring_missing_file,
1428 int reverting_versioned_file, int installing_bad_symlink,
1429 int path_is_unversioned, struct got_repository *repo,
1430 got_worktree_checkout_cb progress_cb, void *progress_arg)
1432 const struct got_error *err = NULL;
1433 int fd = -1;
1434 size_t len, hdrlen;
1435 int update = 0;
1436 char *tmppath = NULL;
1438 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 GOT_DEFAULT_FILE_MODE);
1440 if (fd == -1) {
1441 if (errno == ENOENT) {
1442 char *parent = dirname(path);
1443 if (parent == NULL)
1444 return got_error_from_errno2("dirname", path);
1445 err = add_dir_on_disk(worktree, parent);
1446 if (err)
1447 return err;
1448 fd = open(ondisk_path,
1449 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1450 GOT_DEFAULT_FILE_MODE);
1451 if (fd == -1)
1452 return got_error_from_errno2("open",
1453 ondisk_path);
1454 } else if (errno == EEXIST) {
1455 if (path_is_unversioned) {
1456 err = (*progress_cb)(progress_arg,
1457 GOT_STATUS_UNVERSIONED, path);
1458 goto done;
1460 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1461 /* TODO file is obstructed; do something */
1462 err = got_error_path(ondisk_path,
1463 GOT_ERR_FILE_OBSTRUCTED);
1464 goto done;
1465 } else {
1466 err = got_opentemp_named_fd(&tmppath, &fd,
1467 ondisk_path);
1468 if (err)
1469 goto done;
1470 update = 1;
1472 } else
1473 return got_error_from_errno2("open", ondisk_path);
1476 if (progress_cb) {
1477 if (restoring_missing_file)
1478 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1479 path);
1480 else if (reverting_versioned_file)
1481 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1482 path);
1483 else
1484 err = (*progress_cb)(progress_arg,
1485 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1486 if (err)
1487 goto done;
1490 hdrlen = got_object_blob_get_hdrlen(blob);
1491 do {
1492 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1493 err = got_object_blob_read_block(&len, blob);
1494 if (err)
1495 break;
1496 if (len > 0) {
1497 /* Skip blob object header first time around. */
1498 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1499 if (outlen == -1) {
1500 err = got_error_from_errno("write");
1501 goto done;
1502 } else if (outlen != len - hdrlen) {
1503 err = got_error(GOT_ERR_IO);
1504 goto done;
1506 hdrlen = 0;
1508 } while (len != 0);
1510 if (fsync(fd) != 0) {
1511 err = got_error_from_errno("fsync");
1512 goto done;
1515 if (update) {
1516 if (rename(tmppath, ondisk_path) != 0) {
1517 err = got_error_from_errno3("rename", tmppath,
1518 ondisk_path);
1519 unlink(tmppath);
1520 goto done;
1524 if (chmod(ondisk_path,
1525 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1526 err = got_error_from_errno2("chmod", ondisk_path);
1527 goto done;
1530 done:
1531 if (fd != -1 && close(fd) != 0 && err == NULL)
1532 err = got_error_from_errno("close");
1533 free(tmppath);
1534 return err;
1537 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1538 static const struct got_error *
1539 get_modified_file_content_status(unsigned char *status, FILE *f)
1541 const struct got_error *err = NULL;
1542 const char *markers[3] = {
1543 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1544 GOT_DIFF_CONFLICT_MARKER_SEP,
1545 GOT_DIFF_CONFLICT_MARKER_END
1547 int i = 0;
1548 char *line;
1549 size_t len;
1550 const char delim[3] = {'\0', '\0', '\0'};
1552 while (*status == GOT_STATUS_MODIFY) {
1553 line = fparseln(f, &len, NULL, delim, 0);
1554 if (line == NULL) {
1555 if (feof(f))
1556 break;
1557 err = got_ferror(f, GOT_ERR_IO);
1558 break;
1561 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1562 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1563 == 0)
1564 *status = GOT_STATUS_CONFLICT;
1565 else
1566 i++;
1570 return err;
1573 static int
1574 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1576 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1577 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1580 static int
1581 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1583 return !(ie->ctime_sec == sb->st_ctime &&
1584 ie->ctime_nsec == sb->st_ctimensec &&
1585 ie->mtime_sec == sb->st_mtime &&
1586 ie->mtime_nsec == sb->st_mtimensec &&
1587 ie->size == (sb->st_size & 0xffffffff) &&
1588 !xbit_differs(ie, sb->st_mode));
1591 static unsigned char
1592 get_staged_status(struct got_fileindex_entry *ie)
1594 switch (got_fileindex_entry_stage_get(ie)) {
1595 case GOT_FILEIDX_STAGE_ADD:
1596 return GOT_STATUS_ADD;
1597 case GOT_FILEIDX_STAGE_DELETE:
1598 return GOT_STATUS_DELETE;
1599 case GOT_FILEIDX_STAGE_MODIFY:
1600 return GOT_STATUS_MODIFY;
1601 default:
1602 return GOT_STATUS_NO_CHANGE;
1606 static const struct got_error *
1607 get_symlink_status(unsigned char *status, struct stat *sb,
1608 struct got_fileindex_entry *ie, const char *abspath,
1609 int dirfd, const char *de_name, struct got_blob_object *blob)
1611 const struct got_error *err = NULL;
1612 char target_path[PATH_MAX];
1613 char etarget[PATH_MAX];
1614 ssize_t elen;
1615 size_t len, target_len = 0;
1616 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1617 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1619 *status = GOT_STATUS_NO_CHANGE;
1621 /* Blob object content specifies the target path of the link. */
1622 do {
1623 err = got_object_blob_read_block(&len, blob);
1624 if (err)
1625 return err;
1626 if (len + target_len >= sizeof(target_path)) {
1628 * Should not happen. The blob contents were OK
1629 * when this symlink was installed.
1631 return got_error(GOT_ERR_NO_SPACE);
1633 if (len > 0) {
1634 /* Skip blob object header first time around. */
1635 memcpy(target_path + target_len, buf + hdrlen,
1636 len - hdrlen);
1637 target_len += len - hdrlen;
1638 hdrlen = 0;
1640 } while (len != 0);
1641 target_path[target_len] = '\0';
1643 if (dirfd != -1) {
1644 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1645 if (elen == -1)
1646 return got_error_from_errno2("readlinkat", abspath);
1647 } else {
1648 elen = readlink(abspath, etarget, sizeof(etarget));
1649 if (elen == -1)
1650 return got_error_from_errno2("readlink", abspath);
1653 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1654 *status = GOT_STATUS_MODIFY;
1656 return NULL;
1659 static const struct got_error *
1660 get_file_status(unsigned char *status, struct stat *sb,
1661 struct got_fileindex_entry *ie, const char *abspath,
1662 int dirfd, const char *de_name, struct got_repository *repo)
1664 const struct got_error *err = NULL;
1665 struct got_object_id id;
1666 size_t hdrlen;
1667 int fd = -1;
1668 FILE *f = NULL;
1669 uint8_t fbuf[8192];
1670 struct got_blob_object *blob = NULL;
1671 size_t flen, blen;
1672 unsigned char staged_status = get_staged_status(ie);
1674 *status = GOT_STATUS_NO_CHANGE;
1677 * Whenever the caller provides a directory descriptor and a
1678 * directory entry name for the file, use them! This prevents
1679 * race conditions if filesystem paths change beneath our feet.
1681 if (dirfd != -1) {
1682 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1683 if (errno == ENOENT) {
1684 if (got_fileindex_entry_has_file_on_disk(ie))
1685 *status = GOT_STATUS_MISSING;
1686 else
1687 *status = GOT_STATUS_DELETE;
1688 goto done;
1690 err = got_error_from_errno2("fstatat", abspath);
1691 goto done;
1693 } else {
1694 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1695 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1696 return got_error_from_errno2("open", abspath);
1697 else if (fd == -1 && errno == ELOOP) {
1698 if (lstat(abspath, sb) == -1)
1699 return got_error_from_errno2("lstat", abspath);
1700 } else if (fd == -1 || fstat(fd, sb) == -1) {
1701 if (errno == ENOENT) {
1702 if (got_fileindex_entry_has_file_on_disk(ie))
1703 *status = GOT_STATUS_MISSING;
1704 else
1705 *status = GOT_STATUS_DELETE;
1706 goto done;
1708 err = got_error_from_errno2("fstat", abspath);
1709 goto done;
1713 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1714 *status = GOT_STATUS_OBSTRUCTED;
1715 goto done;
1718 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1719 *status = GOT_STATUS_DELETE;
1720 goto done;
1721 } else if (!got_fileindex_entry_has_blob(ie) &&
1722 staged_status != GOT_STATUS_ADD) {
1723 *status = GOT_STATUS_ADD;
1724 goto done;
1727 if (!stat_info_differs(ie, sb))
1728 goto done;
1730 if (staged_status == GOT_STATUS_MODIFY ||
1731 staged_status == GOT_STATUS_ADD)
1732 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1733 else
1734 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1736 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1737 if (err)
1738 goto done;
1740 if (S_ISLNK(sb->st_mode)) {
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) && S_ISLNK(sb.st_mode)) {
1916 err = merge_symlink(worktree, blob2,
1917 ondisk_path, path, label_orig, blob,
1918 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 got_fileindex_entry_filetype_set(ie,
1981 GOT_FILEIDX_MODE_BAD_SYMLINK);
1984 got_object_blob_close(blob);
1985 done:
1986 free(ondisk_path);
1987 return err;
1990 static const struct got_error *
1991 remove_ondisk_file(const char *root_path, const char *path)
1993 const struct got_error *err = NULL;
1994 char *ondisk_path = NULL;
1996 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1997 return got_error_from_errno("asprintf");
1999 if (unlink(ondisk_path) == -1) {
2000 if (errno != ENOENT)
2001 err = got_error_from_errno2("unlink", ondisk_path);
2002 } else {
2003 char *parent = dirname(ondisk_path);
2004 while (parent && strcmp(parent, root_path) != 0) {
2005 if (rmdir(parent) == -1) {
2006 if (errno != ENOTEMPTY)
2007 err = got_error_from_errno2("rmdir",
2008 parent);
2009 break;
2011 parent = dirname(parent);
2014 free(ondisk_path);
2015 return err;
2018 static const struct got_error *
2019 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2020 struct got_fileindex_entry *ie, struct got_repository *repo,
2021 got_worktree_checkout_cb progress_cb, void *progress_arg)
2023 const struct got_error *err = NULL;
2024 unsigned char status;
2025 struct stat sb;
2026 char *ondisk_path;
2028 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2029 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2031 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2032 == -1)
2033 return got_error_from_errno("asprintf");
2035 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2036 if (err)
2037 goto done;
2039 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2040 char ondisk_target[PATH_MAX];
2041 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2042 sizeof(ondisk_target));
2043 if (ondisk_len == -1) {
2044 err = got_error_from_errno2("readlink", ondisk_path);
2045 goto done;
2047 ondisk_target[ondisk_len] = '\0';
2048 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2049 NULL, NULL, /* XXX pass common ancestor info? */
2050 ondisk_target, ondisk_path);
2051 if (err)
2052 goto done;
2053 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2054 ie->path);
2055 goto done;
2058 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2059 status == GOT_STATUS_ADD) {
2060 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2061 if (err)
2062 goto done;
2064 * Preserve the working file and change the deleted blob's
2065 * entry into a schedule-add entry.
2067 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2068 0);
2069 } else {
2070 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2071 if (err)
2072 goto done;
2073 if (status == GOT_STATUS_NO_CHANGE) {
2074 err = remove_ondisk_file(worktree->root_path, ie->path);
2075 if (err)
2076 goto done;
2078 got_fileindex_entry_remove(fileindex, ie);
2080 done:
2081 free(ondisk_path);
2082 return err;
2085 struct diff_cb_arg {
2086 struct got_fileindex *fileindex;
2087 struct got_worktree *worktree;
2088 struct got_repository *repo;
2089 got_worktree_checkout_cb progress_cb;
2090 void *progress_arg;
2091 got_cancel_cb cancel_cb;
2092 void *cancel_arg;
2095 static const struct got_error *
2096 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2097 struct got_tree_entry *te, const char *parent_path)
2099 struct diff_cb_arg *a = arg;
2101 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2102 return got_error(GOT_ERR_CANCELLED);
2104 return update_blob(a->worktree, a->fileindex, ie, te,
2105 ie->path, a->repo, a->progress_cb, a->progress_arg);
2108 static const struct got_error *
2109 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2111 struct diff_cb_arg *a = arg;
2113 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2114 return got_error(GOT_ERR_CANCELLED);
2116 return delete_blob(a->worktree, a->fileindex, ie,
2117 a->repo, a->progress_cb, a->progress_arg);
2120 static const struct got_error *
2121 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2123 struct diff_cb_arg *a = arg;
2124 const struct got_error *err;
2125 char *path;
2127 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2128 return got_error(GOT_ERR_CANCELLED);
2130 if (got_object_tree_entry_is_submodule(te))
2131 return NULL;
2133 if (asprintf(&path, "%s%s%s", parent_path,
2134 parent_path[0] ? "/" : "", te->name)
2135 == -1)
2136 return got_error_from_errno("asprintf");
2138 if (S_ISDIR(te->mode))
2139 err = add_dir_on_disk(a->worktree, path);
2140 else
2141 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2142 a->repo, a->progress_cb, a->progress_arg);
2144 free(path);
2145 return err;
2148 static const struct got_error *
2149 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2151 const struct got_error *err = NULL;
2152 char *uuidstr = NULL;
2153 uint32_t uuid_status;
2155 *refname = NULL;
2157 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2158 if (uuid_status != uuid_s_ok)
2159 return got_error_uuid(uuid_status, "uuid_to_string");
2161 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2162 == -1) {
2163 err = got_error_from_errno("asprintf");
2164 *refname = NULL;
2166 free(uuidstr);
2167 return err;
2170 const struct got_error *
2171 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2173 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2176 static const struct got_error *
2177 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2179 return get_ref_name(refname, worktree,
2180 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2183 static const struct got_error *
2184 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2186 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2189 static const struct got_error *
2190 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2192 return get_ref_name(refname, worktree,
2193 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2196 static const struct got_error *
2197 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2199 return get_ref_name(refname, worktree,
2200 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2203 static const struct got_error *
2204 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2206 return get_ref_name(refname, worktree,
2207 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2210 static const struct got_error *
2211 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2213 return get_ref_name(refname, worktree,
2214 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2217 static const struct got_error *
2218 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2220 return get_ref_name(refname, worktree,
2221 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2224 static const struct got_error *
2225 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 return get_ref_name(refname, worktree,
2228 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2231 const struct got_error *
2232 got_worktree_get_histedit_script_path(char **path,
2233 struct got_worktree *worktree)
2235 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2236 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2237 *path = NULL;
2238 return got_error_from_errno("asprintf");
2240 return NULL;
2244 * Prevent Git's garbage collector from deleting our base commit by
2245 * setting a reference to our base commit's ID.
2247 static const struct got_error *
2248 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2250 const struct got_error *err = NULL;
2251 struct got_reference *ref = NULL;
2252 char *refname;
2254 err = got_worktree_get_base_ref_name(&refname, worktree);
2255 if (err)
2256 return err;
2258 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2259 if (err)
2260 goto done;
2262 err = got_ref_write(ref, repo);
2263 done:
2264 free(refname);
2265 if (ref)
2266 got_ref_close(ref);
2267 return err;
2270 static const struct got_error *
2271 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2273 const struct got_error *err = NULL;
2275 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2276 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2277 err = got_error_from_errno("asprintf");
2278 *fileindex_path = NULL;
2280 return err;
2284 static const struct got_error *
2285 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2286 struct got_worktree *worktree)
2288 const struct got_error *err = NULL;
2289 FILE *index = NULL;
2291 *fileindex_path = NULL;
2292 *fileindex = got_fileindex_alloc();
2293 if (*fileindex == NULL)
2294 return got_error_from_errno("got_fileindex_alloc");
2296 err = get_fileindex_path(fileindex_path, worktree);
2297 if (err)
2298 goto done;
2300 index = fopen(*fileindex_path, "rb");
2301 if (index == NULL) {
2302 if (errno != ENOENT)
2303 err = got_error_from_errno2("fopen", *fileindex_path);
2304 } else {
2305 err = got_fileindex_read(*fileindex, index);
2306 if (fclose(index) != 0 && err == NULL)
2307 err = got_error_from_errno("fclose");
2309 done:
2310 if (err) {
2311 free(*fileindex_path);
2312 *fileindex_path = NULL;
2313 got_fileindex_free(*fileindex);
2314 *fileindex = NULL;
2316 return err;
2319 struct bump_base_commit_id_arg {
2320 struct got_object_id *base_commit_id;
2321 const char *path;
2322 size_t path_len;
2323 const char *entry_name;
2324 got_worktree_checkout_cb progress_cb;
2325 void *progress_arg;
2328 /* Bump base commit ID of all files within an updated part of the work tree. */
2329 static const struct got_error *
2330 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2332 const struct got_error *err;
2333 struct bump_base_commit_id_arg *a = arg;
2335 if (a->entry_name) {
2336 if (strcmp(ie->path, a->path) != 0)
2337 return NULL;
2338 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2339 return NULL;
2341 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2342 SHA1_DIGEST_LENGTH) == 0)
2343 return NULL;
2345 if (a->progress_cb) {
2346 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2347 ie->path);
2348 if (err)
2349 return err;
2351 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2352 return NULL;
2355 static const struct got_error *
2356 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2358 const struct got_error *err = NULL;
2359 char *new_fileindex_path = NULL;
2360 FILE *new_index = NULL;
2361 struct timespec timeout;
2363 err = got_opentemp_named(&new_fileindex_path, &new_index,
2364 fileindex_path);
2365 if (err)
2366 goto done;
2368 err = got_fileindex_write(fileindex, new_index);
2369 if (err)
2370 goto done;
2372 if (rename(new_fileindex_path, fileindex_path) != 0) {
2373 err = got_error_from_errno3("rename", new_fileindex_path,
2374 fileindex_path);
2375 unlink(new_fileindex_path);
2379 * Sleep for a short amount of time to ensure that files modified after
2380 * this program exits have a different time stamp from the one which
2381 * was recorded in the file index.
2383 timeout.tv_sec = 0;
2384 timeout.tv_nsec = 1;
2385 nanosleep(&timeout, NULL);
2386 done:
2387 if (new_index)
2388 fclose(new_index);
2389 free(new_fileindex_path);
2390 return err;
2393 static const struct got_error *
2394 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2395 struct got_object_id **tree_id, const char *wt_relpath,
2396 struct got_worktree *worktree, struct got_repository *repo)
2398 const struct got_error *err = NULL;
2399 struct got_object_id *id = NULL;
2400 char *in_repo_path = NULL;
2401 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2403 *entry_type = GOT_OBJ_TYPE_ANY;
2404 *tree_relpath = NULL;
2405 *tree_id = NULL;
2407 if (wt_relpath[0] == '\0') {
2408 /* Check out all files within the work tree. */
2409 *entry_type = GOT_OBJ_TYPE_TREE;
2410 *tree_relpath = strdup("");
2411 if (*tree_relpath == NULL) {
2412 err = got_error_from_errno("strdup");
2413 goto done;
2415 err = got_object_id_by_path(tree_id, repo,
2416 worktree->base_commit_id, worktree->path_prefix);
2417 if (err)
2418 goto done;
2419 return NULL;
2422 /* Check out a subset of files in the work tree. */
2424 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2425 is_root_wt ? "" : "/", wt_relpath) == -1) {
2426 err = got_error_from_errno("asprintf");
2427 goto done;
2430 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2431 in_repo_path);
2432 if (err)
2433 goto done;
2435 free(in_repo_path);
2436 in_repo_path = NULL;
2438 err = got_object_get_type(entry_type, repo, id);
2439 if (err)
2440 goto done;
2442 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2443 /* Check out a single file. */
2444 if (strchr(wt_relpath, '/') == NULL) {
2445 /* Check out a single file in work tree's root dir. */
2446 in_repo_path = strdup(worktree->path_prefix);
2447 if (in_repo_path == NULL) {
2448 err = got_error_from_errno("strdup");
2449 goto done;
2451 *tree_relpath = strdup("");
2452 if (*tree_relpath == NULL) {
2453 err = got_error_from_errno("strdup");
2454 goto done;
2456 } else {
2457 /* Check out a single file in a subdirectory. */
2458 err = got_path_dirname(tree_relpath, wt_relpath);
2459 if (err)
2460 return err;
2461 if (asprintf(&in_repo_path, "%s%s%s",
2462 worktree->path_prefix, is_root_wt ? "" : "/",
2463 *tree_relpath) == -1) {
2464 err = got_error_from_errno("asprintf");
2465 goto done;
2468 err = got_object_id_by_path(tree_id, repo,
2469 worktree->base_commit_id, in_repo_path);
2470 } else {
2471 /* Check out all files within a subdirectory. */
2472 *tree_id = got_object_id_dup(id);
2473 if (*tree_id == NULL) {
2474 err = got_error_from_errno("got_object_id_dup");
2475 goto done;
2477 *tree_relpath = strdup(wt_relpath);
2478 if (*tree_relpath == NULL) {
2479 err = got_error_from_errno("strdup");
2480 goto done;
2483 done:
2484 free(id);
2485 free(in_repo_path);
2486 if (err) {
2487 *entry_type = GOT_OBJ_TYPE_ANY;
2488 free(*tree_relpath);
2489 *tree_relpath = NULL;
2490 free(*tree_id);
2491 *tree_id = NULL;
2493 return err;
2496 static const struct got_error *
2497 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2498 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2499 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2500 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2502 const struct got_error *err = NULL;
2503 struct got_commit_object *commit = NULL;
2504 struct got_tree_object *tree = NULL;
2505 struct got_fileindex_diff_tree_cb diff_cb;
2506 struct diff_cb_arg arg;
2508 err = ref_base_commit(worktree, repo);
2509 if (err) {
2510 if (!(err->code == GOT_ERR_ERRNO &&
2511 (errno == EACCES || errno == EROFS)))
2512 goto done;
2513 err = (*progress_cb)(progress_arg,
2514 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2515 if (err)
2516 return err;
2519 err = got_object_open_as_commit(&commit, repo,
2520 worktree->base_commit_id);
2521 if (err)
2522 goto done;
2524 err = got_object_open_as_tree(&tree, repo, tree_id);
2525 if (err)
2526 goto done;
2528 if (entry_name &&
2529 got_object_tree_find_entry(tree, entry_name) == NULL) {
2530 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2531 goto done;
2534 diff_cb.diff_old_new = diff_old_new;
2535 diff_cb.diff_old = diff_old;
2536 diff_cb.diff_new = diff_new;
2537 arg.fileindex = fileindex;
2538 arg.worktree = worktree;
2539 arg.repo = repo;
2540 arg.progress_cb = progress_cb;
2541 arg.progress_arg = progress_arg;
2542 arg.cancel_cb = cancel_cb;
2543 arg.cancel_arg = cancel_arg;
2544 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2545 entry_name, repo, &diff_cb, &arg);
2546 done:
2547 if (tree)
2548 got_object_tree_close(tree);
2549 if (commit)
2550 got_object_commit_close(commit);
2551 return err;
2554 const struct got_error *
2555 got_worktree_checkout_files(struct got_worktree *worktree,
2556 struct got_pathlist_head *paths, struct got_repository *repo,
2557 got_worktree_checkout_cb progress_cb, void *progress_arg,
2558 got_cancel_cb cancel_cb, void *cancel_arg)
2560 const struct got_error *err = NULL, *sync_err, *unlockerr;
2561 struct got_commit_object *commit = NULL;
2562 struct got_tree_object *tree = NULL;
2563 struct got_fileindex *fileindex = NULL;
2564 char *fileindex_path = NULL;
2565 struct got_pathlist_entry *pe;
2566 struct tree_path_data {
2567 SIMPLEQ_ENTRY(tree_path_data) entry;
2568 struct got_object_id *tree_id;
2569 int entry_type;
2570 char *relpath;
2571 char *entry_name;
2572 } *tpd = NULL;
2573 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2575 SIMPLEQ_INIT(&tree_paths);
2577 err = lock_worktree(worktree, LOCK_EX);
2578 if (err)
2579 return err;
2581 /* Map all specified paths to in-repository trees. */
2582 TAILQ_FOREACH(pe, paths, entry) {
2583 tpd = malloc(sizeof(*tpd));
2584 if (tpd == NULL) {
2585 err = got_error_from_errno("malloc");
2586 goto done;
2589 err = find_tree_entry_for_checkout(&tpd->entry_type,
2590 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2591 if (err) {
2592 free(tpd);
2593 goto done;
2596 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2597 err = got_path_basename(&tpd->entry_name, pe->path);
2598 if (err) {
2599 free(tpd->relpath);
2600 free(tpd->tree_id);
2601 free(tpd);
2602 goto done;
2604 } else
2605 tpd->entry_name = NULL;
2607 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2611 * Read the file index.
2612 * Checking out files is supposed to be an idempotent operation.
2613 * If the on-disk file index is incomplete we will try to complete it.
2615 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2616 if (err)
2617 goto done;
2619 tpd = SIMPLEQ_FIRST(&tree_paths);
2620 TAILQ_FOREACH(pe, paths, entry) {
2621 struct bump_base_commit_id_arg bbc_arg;
2623 err = checkout_files(worktree, fileindex, tpd->relpath,
2624 tpd->tree_id, tpd->entry_name, repo,
2625 progress_cb, progress_arg, cancel_cb, cancel_arg);
2626 if (err)
2627 break;
2629 bbc_arg.base_commit_id = worktree->base_commit_id;
2630 bbc_arg.entry_name = tpd->entry_name;
2631 bbc_arg.path = pe->path;
2632 bbc_arg.path_len = pe->path_len;
2633 bbc_arg.progress_cb = progress_cb;
2634 bbc_arg.progress_arg = progress_arg;
2635 err = got_fileindex_for_each_entry_safe(fileindex,
2636 bump_base_commit_id, &bbc_arg);
2637 if (err)
2638 break;
2640 tpd = SIMPLEQ_NEXT(tpd, entry);
2642 sync_err = sync_fileindex(fileindex, fileindex_path);
2643 if (sync_err && err == NULL)
2644 err = sync_err;
2645 done:
2646 free(fileindex_path);
2647 if (tree)
2648 got_object_tree_close(tree);
2649 if (commit)
2650 got_object_commit_close(commit);
2651 if (fileindex)
2652 got_fileindex_free(fileindex);
2653 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2654 tpd = SIMPLEQ_FIRST(&tree_paths);
2655 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2656 free(tpd->relpath);
2657 free(tpd->tree_id);
2658 free(tpd);
2660 unlockerr = lock_worktree(worktree, LOCK_SH);
2661 if (unlockerr && err == NULL)
2662 err = unlockerr;
2663 return err;
2666 struct merge_file_cb_arg {
2667 struct got_worktree *worktree;
2668 struct got_fileindex *fileindex;
2669 got_worktree_checkout_cb progress_cb;
2670 void *progress_arg;
2671 got_cancel_cb cancel_cb;
2672 void *cancel_arg;
2673 const char *label_orig;
2674 struct got_object_id *commit_id2;
2677 static const struct got_error *
2678 merge_file_cb(void *arg, struct got_blob_object *blob1,
2679 struct got_blob_object *blob2, struct got_object_id *id1,
2680 struct got_object_id *id2, const char *path1, const char *path2,
2681 mode_t mode1, mode_t mode2, struct got_repository *repo)
2683 static const struct got_error *err = NULL;
2684 struct merge_file_cb_arg *a = arg;
2685 struct got_fileindex_entry *ie;
2686 char *ondisk_path = NULL;
2687 struct stat sb;
2688 unsigned char status;
2689 int local_changes_subsumed;
2691 if (blob1 && blob2) {
2692 ie = got_fileindex_entry_get(a->fileindex, path2,
2693 strlen(path2));
2694 if (ie == NULL)
2695 return (*a->progress_cb)(a->progress_arg,
2696 GOT_STATUS_MISSING, path2);
2698 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2699 path2) == -1)
2700 return got_error_from_errno("asprintf");
2702 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2703 repo);
2704 if (err)
2705 goto done;
2707 if (status == GOT_STATUS_DELETE) {
2708 err = (*a->progress_cb)(a->progress_arg,
2709 GOT_STATUS_MERGE, path2);
2710 goto done;
2712 if (status != GOT_STATUS_NO_CHANGE &&
2713 status != GOT_STATUS_MODIFY &&
2714 status != GOT_STATUS_CONFLICT &&
2715 status != GOT_STATUS_ADD) {
2716 err = (*a->progress_cb)(a->progress_arg, status, path2);
2717 goto done;
2720 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2721 err = merge_symlink(a->worktree, blob1, ondisk_path,
2722 path2, a->label_orig, blob2, a->commit_id2, repo,
2723 a->progress_cb, a->progress_arg);
2724 } else {
2725 err = merge_blob(&local_changes_subsumed, a->worktree,
2726 blob1, ondisk_path, path2, sb.st_mode,
2727 a->label_orig, blob2, a->commit_id2, repo,
2728 a->progress_cb, a->progress_arg);
2730 } else if (blob1) {
2731 ie = got_fileindex_entry_get(a->fileindex, path1,
2732 strlen(path1));
2733 if (ie == NULL)
2734 return (*a->progress_cb)(a->progress_arg,
2735 GOT_STATUS_MISSING, path1);
2737 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2738 path1) == -1)
2739 return got_error_from_errno("asprintf");
2741 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2742 repo);
2743 if (err)
2744 goto done;
2746 switch (status) {
2747 case GOT_STATUS_NO_CHANGE:
2748 err = (*a->progress_cb)(a->progress_arg,
2749 GOT_STATUS_DELETE, path1);
2750 if (err)
2751 goto done;
2752 err = remove_ondisk_file(a->worktree->root_path, path1);
2753 if (err)
2754 goto done;
2755 if (ie)
2756 got_fileindex_entry_mark_deleted_from_disk(ie);
2757 break;
2758 case GOT_STATUS_DELETE:
2759 case GOT_STATUS_MISSING:
2760 err = (*a->progress_cb)(a->progress_arg,
2761 GOT_STATUS_DELETE, path1);
2762 if (err)
2763 goto done;
2764 if (ie)
2765 got_fileindex_entry_mark_deleted_from_disk(ie);
2766 break;
2767 case GOT_STATUS_ADD:
2768 case GOT_STATUS_MODIFY:
2769 case GOT_STATUS_CONFLICT:
2770 err = (*a->progress_cb)(a->progress_arg,
2771 GOT_STATUS_CANNOT_DELETE, path1);
2772 if (err)
2773 goto done;
2774 break;
2775 case GOT_STATUS_OBSTRUCTED:
2776 err = (*a->progress_cb)(a->progress_arg, status, path1);
2777 if (err)
2778 goto done;
2779 break;
2780 default:
2781 break;
2783 } else if (blob2) {
2784 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2785 path2) == -1)
2786 return got_error_from_errno("asprintf");
2787 ie = got_fileindex_entry_get(a->fileindex, path2,
2788 strlen(path2));
2789 if (ie) {
2790 err = get_file_status(&status, &sb, ie, ondisk_path,
2791 -1, NULL, repo);
2792 if (err)
2793 goto done;
2794 if (status != GOT_STATUS_NO_CHANGE &&
2795 status != GOT_STATUS_MODIFY &&
2796 status != GOT_STATUS_CONFLICT &&
2797 status != GOT_STATUS_ADD) {
2798 err = (*a->progress_cb)(a->progress_arg,
2799 status, path2);
2800 goto done;
2802 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2803 err = merge_symlink(a->worktree, NULL,
2804 ondisk_path, path2, a->label_orig,
2805 blob2, a->commit_id2, repo,
2806 a->progress_cb, a->progress_arg);
2807 } else if (S_ISREG(sb.st_mode)) {
2808 err = merge_blob(&local_changes_subsumed,
2809 a->worktree, NULL, ondisk_path, path2,
2810 sb.st_mode, a->label_orig, blob2,
2811 a->commit_id2, repo, a->progress_cb,
2812 a->progress_arg);
2813 } else {
2814 err = got_error_path(ondisk_path,
2815 GOT_ERR_FILE_OBSTRUCTED);
2817 if (err)
2818 goto done;
2819 if (status == GOT_STATUS_DELETE) {
2820 err = got_fileindex_entry_update(ie,
2821 ondisk_path, blob2->id.sha1,
2822 a->worktree->base_commit_id->sha1, 0);
2823 if (err)
2824 goto done;
2826 } else {
2827 int is_bad_symlink = 0;
2828 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2829 if (S_ISLNK(mode2)) {
2830 err = install_symlink(&is_bad_symlink,
2831 a->worktree, ondisk_path, path2, blob2, 0,
2832 0, 1, repo, a->progress_cb, a->progress_arg);
2833 } else {
2834 err = install_blob(a->worktree, ondisk_path, path2,
2835 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2836 a->progress_cb, a->progress_arg);
2838 if (err)
2839 goto done;
2840 err = got_fileindex_entry_alloc(&ie, path2);
2841 if (err)
2842 goto done;
2843 err = got_fileindex_entry_update(ie, ondisk_path,
2844 NULL, NULL, 1);
2845 if (err) {
2846 got_fileindex_entry_free(ie);
2847 goto done;
2849 err = got_fileindex_entry_add(a->fileindex, ie);
2850 if (err) {
2851 got_fileindex_entry_free(ie);
2852 goto done;
2854 if (is_bad_symlink) {
2855 got_fileindex_entry_filetype_set(ie,
2856 GOT_FILEIDX_MODE_BAD_SYMLINK);
2860 done:
2861 free(ondisk_path);
2862 return err;
2865 struct check_merge_ok_arg {
2866 struct got_worktree *worktree;
2867 struct got_repository *repo;
2870 static const struct got_error *
2871 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2873 const struct got_error *err = NULL;
2874 struct check_merge_ok_arg *a = arg;
2875 unsigned char status;
2876 struct stat sb;
2877 char *ondisk_path;
2879 /* Reject merges into a work tree with mixed base commits. */
2880 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2881 SHA1_DIGEST_LENGTH))
2882 return got_error(GOT_ERR_MIXED_COMMITS);
2884 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2885 == -1)
2886 return got_error_from_errno("asprintf");
2888 /* Reject merges into a work tree with conflicted files. */
2889 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2890 if (err)
2891 return err;
2892 if (status == GOT_STATUS_CONFLICT)
2893 return got_error(GOT_ERR_CONFLICTS);
2895 return NULL;
2898 static const struct got_error *
2899 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2900 const char *fileindex_path, struct got_object_id *commit_id1,
2901 struct got_object_id *commit_id2, struct got_repository *repo,
2902 got_worktree_checkout_cb progress_cb, void *progress_arg,
2903 got_cancel_cb cancel_cb, void *cancel_arg)
2905 const struct got_error *err = NULL, *sync_err;
2906 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2907 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2908 struct merge_file_cb_arg arg;
2909 char *label_orig = NULL;
2911 if (commit_id1) {
2912 char *id_str;
2914 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2915 worktree->path_prefix);
2916 if (err)
2917 goto done;
2919 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2920 if (err)
2921 goto done;
2923 err = got_object_id_str(&id_str, commit_id1);
2924 if (err)
2925 goto done;
2927 if (asprintf(&label_orig, "%s: commit %s",
2928 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2929 err = got_error_from_errno("asprintf");
2930 free(id_str);
2931 goto done;
2933 free(id_str);
2936 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2937 worktree->path_prefix);
2938 if (err)
2939 goto done;
2941 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2942 if (err)
2943 goto done;
2945 arg.worktree = worktree;
2946 arg.fileindex = fileindex;
2947 arg.progress_cb = progress_cb;
2948 arg.progress_arg = progress_arg;
2949 arg.cancel_cb = cancel_cb;
2950 arg.cancel_arg = cancel_arg;
2951 arg.label_orig = label_orig;
2952 arg.commit_id2 = commit_id2;
2953 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2954 sync_err = sync_fileindex(fileindex, fileindex_path);
2955 if (sync_err && err == NULL)
2956 err = sync_err;
2957 done:
2958 if (tree1)
2959 got_object_tree_close(tree1);
2960 if (tree2)
2961 got_object_tree_close(tree2);
2962 free(label_orig);
2963 return err;
2966 const struct got_error *
2967 got_worktree_merge_files(struct got_worktree *worktree,
2968 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2969 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2970 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2972 const struct got_error *err, *unlockerr;
2973 char *fileindex_path = NULL;
2974 struct got_fileindex *fileindex = NULL;
2975 struct check_merge_ok_arg mok_arg;
2977 err = lock_worktree(worktree, LOCK_EX);
2978 if (err)
2979 return err;
2981 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2982 if (err)
2983 goto done;
2985 mok_arg.worktree = worktree;
2986 mok_arg.repo = repo;
2987 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2988 &mok_arg);
2989 if (err)
2990 goto done;
2992 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2993 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2994 done:
2995 if (fileindex)
2996 got_fileindex_free(fileindex);
2997 free(fileindex_path);
2998 unlockerr = lock_worktree(worktree, LOCK_SH);
2999 if (unlockerr && err == NULL)
3000 err = unlockerr;
3001 return err;
3004 struct diff_dir_cb_arg {
3005 struct got_fileindex *fileindex;
3006 struct got_worktree *worktree;
3007 const char *status_path;
3008 size_t status_path_len;
3009 struct got_repository *repo;
3010 got_worktree_status_cb status_cb;
3011 void *status_arg;
3012 got_cancel_cb cancel_cb;
3013 void *cancel_arg;
3014 /* A pathlist containing per-directory pathlists of ignore patterns. */
3015 struct got_pathlist_head ignores;
3016 int report_unchanged;
3017 int no_ignores;
3020 static const struct got_error *
3021 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3022 int dirfd, const char *de_name,
3023 got_worktree_status_cb status_cb, void *status_arg,
3024 struct got_repository *repo, int report_unchanged)
3026 const struct got_error *err = NULL;
3027 unsigned char status = GOT_STATUS_NO_CHANGE;
3028 unsigned char staged_status = get_staged_status(ie);
3029 struct stat sb;
3030 struct got_object_id blob_id, commit_id, staged_blob_id;
3031 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3032 struct got_object_id *staged_blob_idp = NULL;
3034 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3035 if (err)
3036 return err;
3038 if (status == GOT_STATUS_NO_CHANGE &&
3039 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3040 return NULL;
3042 if (got_fileindex_entry_has_blob(ie)) {
3043 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3044 blob_idp = &blob_id;
3046 if (got_fileindex_entry_has_commit(ie)) {
3047 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3048 commit_idp = &commit_id;
3050 if (staged_status == GOT_STATUS_ADD ||
3051 staged_status == GOT_STATUS_MODIFY) {
3052 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3053 SHA1_DIGEST_LENGTH);
3054 staged_blob_idp = &staged_blob_id;
3057 return (*status_cb)(status_arg, status, staged_status,
3058 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3061 static const struct got_error *
3062 status_old_new(void *arg, struct got_fileindex_entry *ie,
3063 struct dirent *de, const char *parent_path, int dirfd)
3065 const struct got_error *err = NULL;
3066 struct diff_dir_cb_arg *a = arg;
3067 char *abspath;
3069 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3070 return got_error(GOT_ERR_CANCELLED);
3072 if (got_path_cmp(parent_path, a->status_path,
3073 strlen(parent_path), a->status_path_len) != 0 &&
3074 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3075 return NULL;
3077 if (parent_path[0]) {
3078 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3079 parent_path, de->d_name) == -1)
3080 return got_error_from_errno("asprintf");
3081 } else {
3082 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3083 de->d_name) == -1)
3084 return got_error_from_errno("asprintf");
3087 err = report_file_status(ie, abspath, dirfd, de->d_name,
3088 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3089 free(abspath);
3090 return err;
3093 static const struct got_error *
3094 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3096 struct diff_dir_cb_arg *a = arg;
3097 struct got_object_id blob_id, commit_id;
3098 unsigned char status;
3100 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3101 return got_error(GOT_ERR_CANCELLED);
3103 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3104 return NULL;
3106 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3107 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3108 if (got_fileindex_entry_has_file_on_disk(ie))
3109 status = GOT_STATUS_MISSING;
3110 else
3111 status = GOT_STATUS_DELETE;
3112 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3113 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3116 void
3117 free_ignorelist(struct got_pathlist_head *ignorelist)
3119 struct got_pathlist_entry *pe;
3121 TAILQ_FOREACH(pe, ignorelist, entry)
3122 free((char *)pe->path);
3123 got_pathlist_free(ignorelist);
3126 void
3127 free_ignores(struct got_pathlist_head *ignores)
3129 struct got_pathlist_entry *pe;
3131 TAILQ_FOREACH(pe, ignores, entry) {
3132 struct got_pathlist_head *ignorelist = pe->data;
3133 free_ignorelist(ignorelist);
3134 free((char *)pe->path);
3136 got_pathlist_free(ignores);
3139 static const struct got_error *
3140 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3142 const struct got_error *err = NULL;
3143 struct got_pathlist_entry *pe = NULL;
3144 struct got_pathlist_head *ignorelist;
3145 char *line = NULL, *pattern, *dirpath = NULL;
3146 size_t linesize = 0;
3147 ssize_t linelen;
3149 ignorelist = calloc(1, sizeof(*ignorelist));
3150 if (ignorelist == NULL)
3151 return got_error_from_errno("calloc");
3152 TAILQ_INIT(ignorelist);
3154 while ((linelen = getline(&line, &linesize, f)) != -1) {
3155 if (linelen > 0 && line[linelen - 1] == '\n')
3156 line[linelen - 1] = '\0';
3158 /* Git's ignores may contain comments. */
3159 if (line[0] == '#')
3160 continue;
3162 /* Git's negated patterns are not (yet?) supported. */
3163 if (line[0] == '!')
3164 continue;
3166 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3167 line) == -1) {
3168 err = got_error_from_errno("asprintf");
3169 goto done;
3171 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3172 if (err)
3173 goto done;
3175 if (ferror(f)) {
3176 err = got_error_from_errno("getline");
3177 goto done;
3180 dirpath = strdup(path);
3181 if (dirpath == NULL) {
3182 err = got_error_from_errno("strdup");
3183 goto done;
3185 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3186 done:
3187 free(line);
3188 if (err || pe == NULL) {
3189 free(dirpath);
3190 free_ignorelist(ignorelist);
3192 return err;
3195 int
3196 match_ignores(struct got_pathlist_head *ignores, const char *path)
3198 struct got_pathlist_entry *pe;
3200 /* Handle patterns which match in all directories. */
3201 TAILQ_FOREACH(pe, ignores, entry) {
3202 struct got_pathlist_head *ignorelist = pe->data;
3203 struct got_pathlist_entry *pi;
3205 TAILQ_FOREACH(pi, ignorelist, entry) {
3206 const char *p, *pattern = pi->path;
3208 if (strncmp(pattern, "**/", 3) != 0)
3209 continue;
3210 pattern += 3;
3211 p = path;
3212 while (*p) {
3213 if (fnmatch(pattern, p,
3214 FNM_PATHNAME | FNM_LEADING_DIR)) {
3215 /* Retry in next directory. */
3216 while (*p && *p != '/')
3217 p++;
3218 while (*p == '/')
3219 p++;
3220 continue;
3222 return 1;
3228 * The ignores pathlist contains ignore lists from children before
3229 * parents, so we can find the most specific ignorelist by walking
3230 * ignores backwards.
3232 pe = TAILQ_LAST(ignores, got_pathlist_head);
3233 while (pe) {
3234 if (got_path_is_child(path, pe->path, pe->path_len)) {
3235 struct got_pathlist_head *ignorelist = pe->data;
3236 struct got_pathlist_entry *pi;
3237 TAILQ_FOREACH(pi, ignorelist, entry) {
3238 const char *pattern = pi->path;
3239 int flags = FNM_LEADING_DIR;
3240 if (strstr(pattern, "/**/") == NULL)
3241 flags |= FNM_PATHNAME;
3242 if (fnmatch(pattern, path, flags))
3243 continue;
3244 return 1;
3247 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3250 return 0;
3253 static const struct got_error *
3254 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3255 const char *path, int dirfd, const char *ignores_filename)
3257 const struct got_error *err = NULL;
3258 char *ignorespath;
3259 int fd = -1;
3260 FILE *ignoresfile = NULL;
3262 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3263 path[0] ? "/" : "", ignores_filename) == -1)
3264 return got_error_from_errno("asprintf");
3266 if (dirfd != -1) {
3267 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3268 if (fd == -1) {
3269 if (errno != ENOENT && errno != EACCES)
3270 err = got_error_from_errno2("openat",
3271 ignorespath);
3272 } else {
3273 ignoresfile = fdopen(fd, "r");
3274 if (ignoresfile == NULL)
3275 err = got_error_from_errno2("fdopen",
3276 ignorespath);
3277 else {
3278 fd = -1;
3279 err = read_ignores(ignores, path, ignoresfile);
3282 } else {
3283 ignoresfile = fopen(ignorespath, "r");
3284 if (ignoresfile == NULL) {
3285 if (errno != ENOENT && errno != EACCES)
3286 err = got_error_from_errno2("fopen",
3287 ignorespath);
3288 } else
3289 err = read_ignores(ignores, path, ignoresfile);
3292 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3293 err = got_error_from_errno2("fclose", path);
3294 if (fd != -1 && close(fd) == -1 && err == NULL)
3295 err = got_error_from_errno2("close", path);
3296 free(ignorespath);
3297 return err;
3300 static const struct got_error *
3301 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3303 const struct got_error *err = NULL;
3304 struct diff_dir_cb_arg *a = arg;
3305 char *path = NULL;
3307 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3308 return got_error(GOT_ERR_CANCELLED);
3310 if (parent_path[0]) {
3311 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3312 return got_error_from_errno("asprintf");
3313 } else {
3314 path = de->d_name;
3317 if (de->d_type != DT_DIR &&
3318 got_path_is_child(path, a->status_path, a->status_path_len)
3319 && !match_ignores(&a->ignores, path))
3320 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3321 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3322 if (parent_path[0])
3323 free(path);
3324 return err;
3327 static const struct got_error *
3328 status_traverse(void *arg, const char *path, int dirfd)
3330 const struct got_error *err = NULL;
3331 struct diff_dir_cb_arg *a = arg;
3333 if (a->no_ignores)
3334 return NULL;
3336 err = add_ignores(&a->ignores, a->worktree->root_path,
3337 path, dirfd, ".cvsignore");
3338 if (err)
3339 return err;
3341 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3342 dirfd, ".gitignore");
3344 return err;
3347 static const struct got_error *
3348 report_single_file_status(const char *path, const char *ondisk_path,
3349 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3350 void *status_arg, struct got_repository *repo, int report_unchanged)
3352 struct got_fileindex_entry *ie;
3353 struct stat sb;
3355 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3356 if (ie)
3357 return report_file_status(ie, ondisk_path, -1, NULL,
3358 status_cb, status_arg, repo, report_unchanged);
3360 if (lstat(ondisk_path, &sb) == -1) {
3361 if (errno != ENOENT)
3362 return got_error_from_errno2("lstat", ondisk_path);
3363 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3364 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3365 return NULL;
3368 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3369 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3370 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3372 return NULL;
3375 static const struct got_error *
3376 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3377 const char *root_path, const char *path)
3379 const struct got_error *err;
3380 char *parent_path, *next_parent_path = NULL;
3382 err = add_ignores(ignores, root_path, "", -1,
3383 ".cvsignore");
3384 if (err)
3385 return err;
3387 err = add_ignores(ignores, root_path, "", -1,
3388 ".gitignore");
3389 if (err)
3390 return err;
3392 err = got_path_dirname(&parent_path, path);
3393 if (err) {
3394 if (err->code == GOT_ERR_BAD_PATH)
3395 return NULL; /* cannot traverse parent */
3396 return err;
3398 for (;;) {
3399 err = add_ignores(ignores, root_path, parent_path, -1,
3400 ".cvsignore");
3401 if (err)
3402 break;
3403 err = add_ignores(ignores, root_path, parent_path, -1,
3404 ".gitignore");
3405 if (err)
3406 break;
3407 err = got_path_dirname(&next_parent_path, parent_path);
3408 if (err) {
3409 if (err->code == GOT_ERR_BAD_PATH)
3410 err = NULL; /* traversed everything */
3411 break;
3413 free(parent_path);
3414 parent_path = next_parent_path;
3415 next_parent_path = NULL;
3418 free(parent_path);
3419 free(next_parent_path);
3420 return err;
3423 static const struct got_error *
3424 worktree_status(struct got_worktree *worktree, const char *path,
3425 struct got_fileindex *fileindex, struct got_repository *repo,
3426 got_worktree_status_cb status_cb, void *status_arg,
3427 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3428 int report_unchanged)
3430 const struct got_error *err = NULL;
3431 int fd = -1;
3432 struct got_fileindex_diff_dir_cb fdiff_cb;
3433 struct diff_dir_cb_arg arg;
3434 char *ondisk_path = NULL;
3436 TAILQ_INIT(&arg.ignores);
3438 if (asprintf(&ondisk_path, "%s%s%s",
3439 worktree->root_path, path[0] ? "/" : "", path) == -1)
3440 return got_error_from_errno("asprintf");
3442 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3443 if (fd == -1) {
3444 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3445 errno != ELOOP)
3446 err = got_error_from_errno2("open", ondisk_path);
3447 else
3448 err = report_single_file_status(path, ondisk_path,
3449 fileindex, status_cb, status_arg, repo,
3450 report_unchanged);
3451 } else {
3452 fdiff_cb.diff_old_new = status_old_new;
3453 fdiff_cb.diff_old = status_old;
3454 fdiff_cb.diff_new = status_new;
3455 fdiff_cb.diff_traverse = status_traverse;
3456 arg.fileindex = fileindex;
3457 arg.worktree = worktree;
3458 arg.status_path = path;
3459 arg.status_path_len = strlen(path);
3460 arg.repo = repo;
3461 arg.status_cb = status_cb;
3462 arg.status_arg = status_arg;
3463 arg.cancel_cb = cancel_cb;
3464 arg.cancel_arg = cancel_arg;
3465 arg.report_unchanged = report_unchanged;
3466 arg.no_ignores = no_ignores;
3467 if (!no_ignores) {
3468 err = add_ignores_from_parent_paths(&arg.ignores,
3469 worktree->root_path, path);
3470 if (err)
3471 goto done;
3473 err = got_fileindex_diff_dir(fileindex, fd,
3474 worktree->root_path, path, repo, &fdiff_cb, &arg);
3476 done:
3477 free_ignores(&arg.ignores);
3478 if (fd != -1 && close(fd) != 0 && err == NULL)
3479 err = got_error_from_errno("close");
3480 free(ondisk_path);
3481 return err;
3484 const struct got_error *
3485 got_worktree_status(struct got_worktree *worktree,
3486 struct got_pathlist_head *paths, struct got_repository *repo,
3487 got_worktree_status_cb status_cb, void *status_arg,
3488 got_cancel_cb cancel_cb, void *cancel_arg)
3490 const struct got_error *err = NULL;
3491 char *fileindex_path = NULL;
3492 struct got_fileindex *fileindex = NULL;
3493 struct got_pathlist_entry *pe;
3495 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3496 if (err)
3497 return err;
3499 TAILQ_FOREACH(pe, paths, entry) {
3500 err = worktree_status(worktree, pe->path, fileindex, repo,
3501 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3502 if (err)
3503 break;
3505 free(fileindex_path);
3506 got_fileindex_free(fileindex);
3507 return err;
3510 const struct got_error *
3511 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3512 const char *arg)
3514 const struct got_error *err = NULL;
3515 char *resolved = NULL, *cwd = NULL, *path = NULL;
3516 size_t len;
3517 struct stat sb;
3519 *wt_path = NULL;
3521 cwd = getcwd(NULL, 0);
3522 if (cwd == NULL)
3523 return got_error_from_errno("getcwd");
3525 if (lstat(arg, &sb) == -1) {
3526 if (errno != ENOENT) {
3527 err = got_error_from_errno2("lstat", arg);
3528 goto done;
3531 if (S_ISLNK(sb.st_mode)) {
3533 * We cannot use realpath(3) with symlinks since we want to
3534 * operate on the symlink itself.
3535 * But we can make the path absolute, assuming it is relative
3536 * to the current working directory, and then canonicalize it.
3538 char *abspath = NULL;
3539 char canonpath[PATH_MAX];
3540 if (!got_path_is_absolute(arg)) {
3541 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3542 err = got_error_from_errno("asprintf");
3543 goto done;
3547 err = got_canonpath(abspath ? abspath : arg, canonpath,
3548 sizeof(canonpath));
3549 if (err)
3550 goto done;
3551 resolved = strdup(canonpath);
3552 if (resolved == NULL) {
3553 err = got_error_from_errno("strdup");
3554 goto done;
3556 } else {
3557 resolved = realpath(arg, NULL);
3558 if (resolved == NULL) {
3559 if (errno != ENOENT) {
3560 err = got_error_from_errno2("realpath", arg);
3561 goto done;
3563 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3564 err = got_error_from_errno("asprintf");
3565 goto done;
3570 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3571 strlen(got_worktree_get_root_path(worktree)))) {
3572 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3573 goto done;
3576 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3577 err = got_path_skip_common_ancestor(&path,
3578 got_worktree_get_root_path(worktree), resolved);
3579 if (err)
3580 goto done;
3581 } else {
3582 path = strdup("");
3583 if (path == NULL) {
3584 err = got_error_from_errno("strdup");
3585 goto done;
3589 /* XXX status walk can't deal with trailing slash! */
3590 len = strlen(path);
3591 while (len > 0 && path[len - 1] == '/') {
3592 path[len - 1] = '\0';
3593 len--;
3595 done:
3596 free(resolved);
3597 free(cwd);
3598 if (err == NULL)
3599 *wt_path = path;
3600 else
3601 free(path);
3602 return err;
3605 struct schedule_addition_args {
3606 struct got_worktree *worktree;
3607 struct got_fileindex *fileindex;
3608 got_worktree_checkout_cb progress_cb;
3609 void *progress_arg;
3610 struct got_repository *repo;
3613 static const struct got_error *
3614 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3615 const char *relpath, struct got_object_id *blob_id,
3616 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3617 int dirfd, const char *de_name)
3619 struct schedule_addition_args *a = arg;
3620 const struct got_error *err = NULL;
3621 struct got_fileindex_entry *ie;
3622 struct stat sb;
3623 char *ondisk_path;
3625 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3626 relpath) == -1)
3627 return got_error_from_errno("asprintf");
3629 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3630 if (ie) {
3631 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3632 de_name, a->repo);
3633 if (err)
3634 goto done;
3635 /* Re-adding an existing entry is a no-op. */
3636 if (status == GOT_STATUS_ADD)
3637 goto done;
3638 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3639 if (err)
3640 goto done;
3643 if (status != GOT_STATUS_UNVERSIONED) {
3644 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3645 goto done;
3648 err = got_fileindex_entry_alloc(&ie, relpath);
3649 if (err)
3650 goto done;
3651 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3652 if (err) {
3653 got_fileindex_entry_free(ie);
3654 goto done;
3656 err = got_fileindex_entry_add(a->fileindex, ie);
3657 if (err) {
3658 got_fileindex_entry_free(ie);
3659 goto done;
3661 done:
3662 free(ondisk_path);
3663 if (err)
3664 return err;
3665 if (status == GOT_STATUS_ADD)
3666 return NULL;
3667 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3670 const struct got_error *
3671 got_worktree_schedule_add(struct got_worktree *worktree,
3672 struct got_pathlist_head *paths,
3673 got_worktree_checkout_cb progress_cb, void *progress_arg,
3674 struct got_repository *repo, int no_ignores)
3676 struct got_fileindex *fileindex = NULL;
3677 char *fileindex_path = NULL;
3678 const struct got_error *err = NULL, *sync_err, *unlockerr;
3679 struct got_pathlist_entry *pe;
3680 struct schedule_addition_args saa;
3682 err = lock_worktree(worktree, LOCK_EX);
3683 if (err)
3684 return err;
3686 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3687 if (err)
3688 goto done;
3690 saa.worktree = worktree;
3691 saa.fileindex = fileindex;
3692 saa.progress_cb = progress_cb;
3693 saa.progress_arg = progress_arg;
3694 saa.repo = repo;
3696 TAILQ_FOREACH(pe, paths, entry) {
3697 err = worktree_status(worktree, pe->path, fileindex, repo,
3698 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3699 if (err)
3700 break;
3702 sync_err = sync_fileindex(fileindex, fileindex_path);
3703 if (sync_err && err == NULL)
3704 err = sync_err;
3705 done:
3706 free(fileindex_path);
3707 if (fileindex)
3708 got_fileindex_free(fileindex);
3709 unlockerr = lock_worktree(worktree, LOCK_SH);
3710 if (unlockerr && err == NULL)
3711 err = unlockerr;
3712 return err;
3715 struct schedule_deletion_args {
3716 struct got_worktree *worktree;
3717 struct got_fileindex *fileindex;
3718 got_worktree_delete_cb progress_cb;
3719 void *progress_arg;
3720 struct got_repository *repo;
3721 int delete_local_mods;
3722 int keep_on_disk;
3725 static const struct got_error *
3726 schedule_for_deletion(void *arg, unsigned char status,
3727 unsigned char staged_status, const char *relpath,
3728 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3729 struct got_object_id *commit_id, int dirfd, const char *de_name)
3731 struct schedule_deletion_args *a = arg;
3732 const struct got_error *err = NULL;
3733 struct got_fileindex_entry *ie = NULL;
3734 struct stat sb;
3735 char *ondisk_path, *parent = NULL;
3737 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3738 if (ie == NULL)
3739 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3741 staged_status = get_staged_status(ie);
3742 if (staged_status != GOT_STATUS_NO_CHANGE) {
3743 if (staged_status == GOT_STATUS_DELETE)
3744 return NULL;
3745 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3748 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3749 relpath) == -1)
3750 return got_error_from_errno("asprintf");
3752 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3753 a->repo);
3754 if (err)
3755 goto done;
3757 if (status != GOT_STATUS_NO_CHANGE) {
3758 if (status == GOT_STATUS_DELETE)
3759 goto done;
3760 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3761 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3762 goto done;
3764 if (status != GOT_STATUS_MODIFY &&
3765 status != GOT_STATUS_MISSING) {
3766 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3767 goto done;
3771 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3772 if (dirfd != -1) {
3773 if (unlinkat(dirfd, de_name, 0) != 0) {
3774 err = got_error_from_errno2("unlinkat",
3775 ondisk_path);
3776 goto done;
3778 } else if (unlink(ondisk_path) != 0) {
3779 err = got_error_from_errno2("unlink", ondisk_path);
3780 goto done;
3783 parent = dirname(ondisk_path);
3785 if (parent == NULL) {
3786 err = got_error_from_errno2("dirname", ondisk_path);
3787 goto done;
3789 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3790 if (rmdir(parent) == -1) {
3791 if (errno != ENOTEMPTY)
3792 err = got_error_from_errno2("rmdir",
3793 parent);
3794 break;
3796 parent = dirname(parent);
3797 if (parent == NULL) {
3798 err = got_error_from_errno2("dirname", parent);
3799 goto done;
3804 got_fileindex_entry_mark_deleted_from_disk(ie);
3805 done:
3806 free(ondisk_path);
3807 if (err)
3808 return err;
3809 if (status == GOT_STATUS_DELETE)
3810 return NULL;
3811 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3812 staged_status, relpath);
3815 const struct got_error *
3816 got_worktree_schedule_delete(struct got_worktree *worktree,
3817 struct got_pathlist_head *paths, int delete_local_mods,
3818 got_worktree_delete_cb progress_cb, void *progress_arg,
3819 struct got_repository *repo, int keep_on_disk)
3821 struct got_fileindex *fileindex = NULL;
3822 char *fileindex_path = NULL;
3823 const struct got_error *err = NULL, *sync_err, *unlockerr;
3824 struct got_pathlist_entry *pe;
3825 struct schedule_deletion_args sda;
3827 err = lock_worktree(worktree, LOCK_EX);
3828 if (err)
3829 return err;
3831 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3832 if (err)
3833 goto done;
3835 sda.worktree = worktree;
3836 sda.fileindex = fileindex;
3837 sda.progress_cb = progress_cb;
3838 sda.progress_arg = progress_arg;
3839 sda.repo = repo;
3840 sda.delete_local_mods = delete_local_mods;
3841 sda.keep_on_disk = keep_on_disk;
3843 TAILQ_FOREACH(pe, paths, entry) {
3844 err = worktree_status(worktree, pe->path, fileindex, repo,
3845 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3846 if (err)
3847 break;
3849 sync_err = sync_fileindex(fileindex, fileindex_path);
3850 if (sync_err && err == NULL)
3851 err = sync_err;
3852 done:
3853 free(fileindex_path);
3854 if (fileindex)
3855 got_fileindex_free(fileindex);
3856 unlockerr = lock_worktree(worktree, LOCK_SH);
3857 if (unlockerr && err == NULL)
3858 err = unlockerr;
3859 return err;
3862 static const struct got_error *
3863 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3865 const struct got_error *err = NULL;
3866 char *line = NULL;
3867 size_t linesize = 0, n;
3868 ssize_t linelen;
3870 linelen = getline(&line, &linesize, infile);
3871 if (linelen == -1) {
3872 if (ferror(infile)) {
3873 err = got_error_from_errno("getline");
3874 goto done;
3876 return NULL;
3878 if (outfile) {
3879 n = fwrite(line, 1, linelen, outfile);
3880 if (n != linelen) {
3881 err = got_ferror(outfile, GOT_ERR_IO);
3882 goto done;
3885 if (rejectfile) {
3886 n = fwrite(line, 1, linelen, rejectfile);
3887 if (n != linelen)
3888 err = got_ferror(outfile, GOT_ERR_IO);
3890 done:
3891 free(line);
3892 return err;
3895 static const struct got_error *
3896 skip_one_line(FILE *f)
3898 char *line = NULL;
3899 size_t linesize = 0;
3900 ssize_t linelen;
3902 linelen = getline(&line, &linesize, f);
3903 if (linelen == -1) {
3904 if (ferror(f))
3905 return got_error_from_errno("getline");
3906 return NULL;
3908 free(line);
3909 return NULL;
3912 static const struct got_error *
3913 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3914 int start_old, int end_old, int start_new, int end_new,
3915 FILE *outfile, FILE *rejectfile)
3917 const struct got_error *err;
3919 /* Copy old file's lines leading up to patch. */
3920 while (!feof(f1) && *line_cur1 < start_old) {
3921 err = copy_one_line(f1, outfile, NULL);
3922 if (err)
3923 return err;
3924 (*line_cur1)++;
3926 /* Skip new file's lines leading up to patch. */
3927 while (!feof(f2) && *line_cur2 < start_new) {
3928 if (rejectfile)
3929 err = copy_one_line(f2, NULL, rejectfile);
3930 else
3931 err = skip_one_line(f2);
3932 if (err)
3933 return err;
3934 (*line_cur2)++;
3936 /* Copy patched lines. */
3937 while (!feof(f2) && *line_cur2 <= end_new) {
3938 err = copy_one_line(f2, outfile, NULL);
3939 if (err)
3940 return err;
3941 (*line_cur2)++;
3943 /* Skip over old file's replaced lines. */
3944 while (!feof(f1) && *line_cur1 <= end_old) {
3945 if (rejectfile)
3946 err = copy_one_line(f1, NULL, rejectfile);
3947 else
3948 err = skip_one_line(f1);
3949 if (err)
3950 return err;
3951 (*line_cur1)++;
3954 return NULL;
3957 static const struct got_error *
3958 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3959 FILE *outfile, FILE *rejectfile)
3961 const struct got_error *err;
3963 if (outfile) {
3964 /* Copy old file's lines until EOF. */
3965 while (!feof(f1)) {
3966 err = copy_one_line(f1, outfile, NULL);
3967 if (err)
3968 return err;
3969 (*line_cur1)++;
3972 if (rejectfile) {
3973 /* Copy new file's lines until EOF. */
3974 while (!feof(f2)) {
3975 err = copy_one_line(f2, NULL, rejectfile);
3976 if (err)
3977 return err;
3978 (*line_cur2)++;
3982 return NULL;
3985 static const struct got_error *
3986 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3987 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3988 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3989 int *line_cur2, FILE *outfile, FILE *rejectfile,
3990 got_worktree_patch_cb patch_cb, void *patch_arg)
3992 const struct got_error *err = NULL;
3993 int start_old = change->cv.a;
3994 int end_old = change->cv.b;
3995 int start_new = change->cv.c;
3996 int end_new = change->cv.d;
3997 long pos1, pos2;
3998 FILE *hunkfile;
4000 *choice = GOT_PATCH_CHOICE_NONE;
4002 hunkfile = got_opentemp();
4003 if (hunkfile == NULL)
4004 return got_error_from_errno("got_opentemp");
4006 pos1 = ftell(f1);
4007 pos2 = ftell(f2);
4009 /* XXX TODO needs error checking */
4010 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4012 if (fseek(f1, pos1, SEEK_SET) == -1) {
4013 err = got_ferror(f1, GOT_ERR_IO);
4014 goto done;
4016 if (fseek(f2, pos2, SEEK_SET) == -1) {
4017 err = got_ferror(f1, GOT_ERR_IO);
4018 goto done;
4020 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4021 err = got_ferror(hunkfile, GOT_ERR_IO);
4022 goto done;
4025 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4026 hunkfile, n, nchanges);
4027 if (err)
4028 goto done;
4030 switch (*choice) {
4031 case GOT_PATCH_CHOICE_YES:
4032 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4033 end_old, start_new, end_new, outfile, rejectfile);
4034 break;
4035 case GOT_PATCH_CHOICE_NO:
4036 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4037 end_old, start_new, end_new, rejectfile, outfile);
4038 break;
4039 case GOT_PATCH_CHOICE_QUIT:
4040 break;
4041 default:
4042 err = got_error(GOT_ERR_PATCH_CHOICE);
4043 break;
4045 done:
4046 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4047 err = got_error_from_errno("fclose");
4048 return err;
4051 struct revert_file_args {
4052 struct got_worktree *worktree;
4053 struct got_fileindex *fileindex;
4054 got_worktree_checkout_cb progress_cb;
4055 void *progress_arg;
4056 got_worktree_patch_cb patch_cb;
4057 void *patch_arg;
4058 struct got_repository *repo;
4061 static const struct got_error *
4062 create_patched_content(char **path_outfile, int reverse_patch,
4063 struct got_object_id *blob_id, const char *path2,
4064 int dirfd2, const char *de_name2,
4065 const char *relpath, struct got_repository *repo,
4066 got_worktree_patch_cb patch_cb, void *patch_arg)
4068 const struct got_error *err;
4069 struct got_blob_object *blob = NULL;
4070 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4071 int fd2 = -1;
4072 char link_target[PATH_MAX];
4073 ssize_t link_len = 0;
4074 char *path1 = NULL, *id_str = NULL;
4075 struct stat sb1, sb2;
4076 struct got_diff_changes *changes = NULL;
4077 struct got_diff_state *ds = NULL;
4078 struct got_diff_args *args = NULL;
4079 struct got_diff_change *change;
4080 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4081 int n = 0;
4083 *path_outfile = NULL;
4085 err = got_object_id_str(&id_str, blob_id);
4086 if (err)
4087 return err;
4089 if (dirfd2 != -1) {
4090 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4091 if (fd2 == -1) {
4092 if (errno != ELOOP) {
4093 err = got_error_from_errno2("openat", path2);
4094 goto done;
4096 link_len = readlinkat(dirfd2, de_name2,
4097 link_target, sizeof(link_target));
4098 if (link_len == -1)
4099 return got_error_from_errno2("readlinkat", path2);
4100 sb2.st_mode = S_IFLNK;
4101 sb2.st_size = link_len;
4103 } else {
4104 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4105 if (fd2 == -1) {
4106 if (errno != ELOOP) {
4107 err = got_error_from_errno2("open", path2);
4108 goto done;
4110 link_len = readlink(path2, link_target,
4111 sizeof(link_target));
4112 if (link_len == -1)
4113 return got_error_from_errno2("readlink", path2);
4114 sb2.st_mode = S_IFLNK;
4115 sb2.st_size = link_len;
4118 if (fd2 != -1) {
4119 if (fstat(fd2, &sb2) == -1) {
4120 err = got_error_from_errno2("fstat", path2);
4121 goto done;
4124 f2 = fdopen(fd2, "r");
4125 if (f2 == NULL) {
4126 err = got_error_from_errno2("fdopen", path2);
4127 goto done;
4129 fd2 = -1;
4130 } else {
4131 size_t n;
4132 f2 = got_opentemp();
4133 if (f2 == NULL) {
4134 err = got_error_from_errno2("got_opentemp", path2);
4135 goto done;
4137 n = fwrite(link_target, 1, link_len, f2);
4138 if (n != link_len) {
4139 err = got_ferror(f2, GOT_ERR_IO);
4140 goto done;
4142 if (fflush(f2) == EOF) {
4143 err = got_error_from_errno("fflush");
4144 goto done;
4146 rewind(f2);
4149 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4150 if (err)
4151 goto done;
4153 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4154 if (err)
4155 goto done;
4157 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4158 if (err)
4159 goto done;
4161 if (stat(path1, &sb1) == -1) {
4162 err = got_error_from_errno2("stat", path1);
4163 goto done;
4166 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4167 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4168 if (err)
4169 goto done;
4171 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4172 if (err)
4173 goto done;
4175 if (fseek(f1, 0L, SEEK_SET) == -1)
4176 return got_ferror(f1, GOT_ERR_IO);
4177 if (fseek(f2, 0L, SEEK_SET) == -1)
4178 return got_ferror(f2, GOT_ERR_IO);
4179 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4180 int choice;
4181 err = apply_or_reject_change(&choice, change, ++n,
4182 changes->nchanges, ds, args, diff_flags, relpath,
4183 f1, f2, &line_cur1, &line_cur2,
4184 reverse_patch ? NULL : outfile,
4185 reverse_patch ? outfile : NULL,
4186 patch_cb, patch_arg);
4187 if (err)
4188 goto done;
4189 if (choice == GOT_PATCH_CHOICE_YES)
4190 have_content = 1;
4191 else if (choice == GOT_PATCH_CHOICE_QUIT)
4192 break;
4194 if (have_content) {
4195 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4196 reverse_patch ? NULL : outfile,
4197 reverse_patch ? outfile : NULL);
4198 if (err)
4199 goto done;
4201 if (!S_ISLNK(sb2.st_mode)) {
4202 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4203 err = got_error_from_errno2("chmod", path2);
4204 goto done;
4208 done:
4209 free(id_str);
4210 if (blob)
4211 got_object_blob_close(blob);
4212 if (f1 && fclose(f1) == EOF && err == NULL)
4213 err = got_error_from_errno2("fclose", path1);
4214 if (f2 && fclose(f2) == EOF && err == NULL)
4215 err = got_error_from_errno2("fclose", path2);
4216 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4217 err = got_error_from_errno2("close", path2);
4218 if (outfile && fclose(outfile) == EOF && err == NULL)
4219 err = got_error_from_errno2("fclose", *path_outfile);
4220 if (path1 && unlink(path1) == -1 && err == NULL)
4221 err = got_error_from_errno2("unlink", path1);
4222 if (err || !have_content) {
4223 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4224 err = got_error_from_errno2("unlink", *path_outfile);
4225 free(*path_outfile);
4226 *path_outfile = NULL;
4228 free(args);
4229 if (ds) {
4230 got_diff_state_free(ds);
4231 free(ds);
4233 if (changes)
4234 got_diff_free_changes(changes);
4235 free(path1);
4236 return err;
4239 static const struct got_error *
4240 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4241 const char *relpath, struct got_object_id *blob_id,
4242 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4243 int dirfd, const char *de_name)
4245 struct revert_file_args *a = arg;
4246 const struct got_error *err = NULL;
4247 char *parent_path = NULL;
4248 struct got_fileindex_entry *ie;
4249 struct got_tree_object *tree = NULL;
4250 struct got_object_id *tree_id = NULL;
4251 const struct got_tree_entry *te = NULL;
4252 char *tree_path = NULL, *te_name;
4253 char *ondisk_path = NULL, *path_content = NULL;
4254 struct got_blob_object *blob = NULL;
4256 /* Reverting a staged deletion is a no-op. */
4257 if (status == GOT_STATUS_DELETE &&
4258 staged_status != GOT_STATUS_NO_CHANGE)
4259 return NULL;
4261 if (status == GOT_STATUS_UNVERSIONED)
4262 return (*a->progress_cb)(a->progress_arg,
4263 GOT_STATUS_UNVERSIONED, relpath);
4265 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4266 if (ie == NULL)
4267 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4269 /* Construct in-repository path of tree which contains this blob. */
4270 err = got_path_dirname(&parent_path, ie->path);
4271 if (err) {
4272 if (err->code != GOT_ERR_BAD_PATH)
4273 goto done;
4274 parent_path = strdup("/");
4275 if (parent_path == NULL) {
4276 err = got_error_from_errno("strdup");
4277 goto done;
4280 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4281 tree_path = strdup(parent_path);
4282 if (tree_path == NULL) {
4283 err = got_error_from_errno("strdup");
4284 goto done;
4286 } else {
4287 if (got_path_is_root_dir(parent_path)) {
4288 tree_path = strdup(a->worktree->path_prefix);
4289 if (tree_path == NULL) {
4290 err = got_error_from_errno("strdup");
4291 goto done;
4293 } else {
4294 if (asprintf(&tree_path, "%s/%s",
4295 a->worktree->path_prefix, parent_path) == -1) {
4296 err = got_error_from_errno("asprintf");
4297 goto done;
4302 err = got_object_id_by_path(&tree_id, a->repo,
4303 a->worktree->base_commit_id, tree_path);
4304 if (err) {
4305 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4306 (status == GOT_STATUS_ADD ||
4307 staged_status == GOT_STATUS_ADD)))
4308 goto done;
4309 } else {
4310 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4311 if (err)
4312 goto done;
4314 te_name = basename(ie->path);
4315 if (te_name == NULL) {
4316 err = got_error_from_errno2("basename", ie->path);
4317 goto done;
4320 te = got_object_tree_find_entry(tree, te_name);
4321 if (te == NULL && status != GOT_STATUS_ADD &&
4322 staged_status != GOT_STATUS_ADD) {
4323 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4324 goto done;
4328 switch (status) {
4329 case GOT_STATUS_ADD:
4330 if (a->patch_cb) {
4331 int choice = GOT_PATCH_CHOICE_NONE;
4332 err = (*a->patch_cb)(&choice, a->patch_arg,
4333 status, ie->path, NULL, 1, 1);
4334 if (err)
4335 goto done;
4336 if (choice != GOT_PATCH_CHOICE_YES)
4337 break;
4339 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4340 ie->path);
4341 if (err)
4342 goto done;
4343 got_fileindex_entry_remove(a->fileindex, ie);
4344 break;
4345 case GOT_STATUS_DELETE:
4346 if (a->patch_cb) {
4347 int choice = GOT_PATCH_CHOICE_NONE;
4348 err = (*a->patch_cb)(&choice, a->patch_arg,
4349 status, ie->path, NULL, 1, 1);
4350 if (err)
4351 goto done;
4352 if (choice != GOT_PATCH_CHOICE_YES)
4353 break;
4355 /* fall through */
4356 case GOT_STATUS_MODIFY:
4357 case GOT_STATUS_MODE_CHANGE:
4358 case GOT_STATUS_CONFLICT:
4359 case GOT_STATUS_MISSING: {
4360 struct got_object_id id;
4361 if (staged_status == GOT_STATUS_ADD ||
4362 staged_status == GOT_STATUS_MODIFY) {
4363 memcpy(id.sha1, ie->staged_blob_sha1,
4364 SHA1_DIGEST_LENGTH);
4365 } else
4366 memcpy(id.sha1, ie->blob_sha1,
4367 SHA1_DIGEST_LENGTH);
4368 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4369 if (err)
4370 goto done;
4372 if (asprintf(&ondisk_path, "%s/%s",
4373 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4374 err = got_error_from_errno("asprintf");
4375 goto done;
4378 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4379 status == GOT_STATUS_CONFLICT)) {
4380 int is_bad_symlink = 0;
4381 err = create_patched_content(&path_content, 1, &id,
4382 ondisk_path, dirfd, de_name, ie->path, a->repo,
4383 a->patch_cb, a->patch_arg);
4384 if (err || path_content == NULL)
4385 break;
4386 if (te && S_ISLNK(te->mode)) {
4387 if (unlink(path_content) == -1) {
4388 err = got_error_from_errno2("unlink",
4389 path_content);
4390 break;
4392 err = install_symlink(&is_bad_symlink,
4393 a->worktree, ondisk_path, ie->path,
4394 blob, 0, 1, 0, a->repo,
4395 a->progress_cb, a->progress_arg);
4396 } else {
4397 if (rename(path_content, ondisk_path) == -1) {
4398 err = got_error_from_errno3("rename",
4399 path_content, ondisk_path);
4400 goto done;
4403 } else {
4404 int is_bad_symlink = 0;
4405 if (te && S_ISLNK(te->mode)) {
4406 err = install_symlink(&is_bad_symlink,
4407 a->worktree, ondisk_path, ie->path,
4408 blob, 0, 1, 0, a->repo,
4409 a->progress_cb, a->progress_arg);
4410 } else {
4411 err = install_blob(a->worktree, ondisk_path,
4412 ie->path,
4413 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4414 got_fileindex_perms_to_st(ie), blob,
4415 0, 1, 0, 0, a->repo,
4416 a->progress_cb, a->progress_arg);
4418 if (err)
4419 goto done;
4420 if (status == GOT_STATUS_DELETE ||
4421 status == GOT_STATUS_MODE_CHANGE) {
4422 err = got_fileindex_entry_update(ie,
4423 ondisk_path, blob->id.sha1,
4424 a->worktree->base_commit_id->sha1, 1);
4425 if (err)
4426 goto done;
4428 if (is_bad_symlink) {
4429 got_fileindex_entry_filetype_set(ie,
4430 GOT_FILEIDX_MODE_BAD_SYMLINK);
4433 break;
4435 default:
4436 break;
4438 done:
4439 free(ondisk_path);
4440 free(path_content);
4441 free(parent_path);
4442 free(tree_path);
4443 if (blob)
4444 got_object_blob_close(blob);
4445 if (tree)
4446 got_object_tree_close(tree);
4447 free(tree_id);
4448 return err;
4451 const struct got_error *
4452 got_worktree_revert(struct got_worktree *worktree,
4453 struct got_pathlist_head *paths,
4454 got_worktree_checkout_cb progress_cb, void *progress_arg,
4455 got_worktree_patch_cb patch_cb, void *patch_arg,
4456 struct got_repository *repo)
4458 struct got_fileindex *fileindex = NULL;
4459 char *fileindex_path = NULL;
4460 const struct got_error *err = NULL, *unlockerr = NULL;
4461 const struct got_error *sync_err = NULL;
4462 struct got_pathlist_entry *pe;
4463 struct revert_file_args rfa;
4465 err = lock_worktree(worktree, LOCK_EX);
4466 if (err)
4467 return err;
4469 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4470 if (err)
4471 goto done;
4473 rfa.worktree = worktree;
4474 rfa.fileindex = fileindex;
4475 rfa.progress_cb = progress_cb;
4476 rfa.progress_arg = progress_arg;
4477 rfa.patch_cb = patch_cb;
4478 rfa.patch_arg = patch_arg;
4479 rfa.repo = repo;
4480 TAILQ_FOREACH(pe, paths, entry) {
4481 err = worktree_status(worktree, pe->path, fileindex, repo,
4482 revert_file, &rfa, NULL, NULL, 0, 0);
4483 if (err)
4484 break;
4486 sync_err = sync_fileindex(fileindex, fileindex_path);
4487 if (sync_err && err == NULL)
4488 err = sync_err;
4489 done:
4490 free(fileindex_path);
4491 if (fileindex)
4492 got_fileindex_free(fileindex);
4493 unlockerr = lock_worktree(worktree, LOCK_SH);
4494 if (unlockerr && err == NULL)
4495 err = unlockerr;
4496 return err;
4499 static void
4500 free_commitable(struct got_commitable *ct)
4502 free(ct->path);
4503 free(ct->in_repo_path);
4504 free(ct->ondisk_path);
4505 free(ct->blob_id);
4506 free(ct->base_blob_id);
4507 free(ct->staged_blob_id);
4508 free(ct->base_commit_id);
4509 free(ct);
4512 struct collect_commitables_arg {
4513 struct got_pathlist_head *commitable_paths;
4514 struct got_repository *repo;
4515 struct got_worktree *worktree;
4516 struct got_fileindex *fileindex;
4517 int have_staged_files;
4518 int allow_bad_symlinks;
4521 static const struct got_error *
4522 collect_commitables(void *arg, unsigned char status,
4523 unsigned char staged_status, const char *relpath,
4524 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4525 struct got_object_id *commit_id, int dirfd, const char *de_name)
4527 struct collect_commitables_arg *a = arg;
4528 const struct got_error *err = NULL;
4529 struct got_commitable *ct = NULL;
4530 struct got_pathlist_entry *new = NULL;
4531 char *parent_path = NULL, *path = NULL;
4532 struct stat sb;
4534 if (a->have_staged_files) {
4535 if (staged_status != GOT_STATUS_MODIFY &&
4536 staged_status != GOT_STATUS_ADD &&
4537 staged_status != GOT_STATUS_DELETE)
4538 return NULL;
4539 } else {
4540 if (status == GOT_STATUS_CONFLICT)
4541 return got_error(GOT_ERR_COMMIT_CONFLICT);
4543 if (status != GOT_STATUS_MODIFY &&
4544 status != GOT_STATUS_MODE_CHANGE &&
4545 status != GOT_STATUS_ADD &&
4546 status != GOT_STATUS_DELETE)
4547 return NULL;
4550 if (asprintf(&path, "/%s", relpath) == -1) {
4551 err = got_error_from_errno("asprintf");
4552 goto done;
4554 if (strcmp(path, "/") == 0) {
4555 parent_path = strdup("");
4556 if (parent_path == NULL)
4557 return got_error_from_errno("strdup");
4558 } else {
4559 err = got_path_dirname(&parent_path, path);
4560 if (err)
4561 return err;
4564 ct = calloc(1, sizeof(*ct));
4565 if (ct == NULL) {
4566 err = got_error_from_errno("calloc");
4567 goto done;
4570 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4571 relpath) == -1) {
4572 err = got_error_from_errno("asprintf");
4573 goto done;
4576 if (staged_status == GOT_STATUS_ADD ||
4577 staged_status == GOT_STATUS_MODIFY) {
4578 struct got_fileindex_entry *ie;
4579 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4580 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4581 case GOT_FILEIDX_MODE_REGULAR_FILE:
4582 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4583 ct->mode = S_IFREG;
4584 break;
4585 case GOT_FILEIDX_MODE_SYMLINK:
4586 ct->mode = S_IFLNK;
4587 break;
4588 default:
4589 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4590 goto done;
4592 ct->mode |= got_fileindex_entry_perms_get(ie);
4593 } else if (status != GOT_STATUS_DELETE &&
4594 staged_status != GOT_STATUS_DELETE) {
4595 if (dirfd != -1) {
4596 if (fstatat(dirfd, de_name, &sb,
4597 AT_SYMLINK_NOFOLLOW) == -1) {
4598 err = got_error_from_errno2("fstatat",
4599 ct->ondisk_path);
4600 goto done;
4602 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4603 err = got_error_from_errno2("lstat", ct->ondisk_path);
4604 goto done;
4606 ct->mode = sb.st_mode;
4609 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4610 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4611 relpath) == -1) {
4612 err = got_error_from_errno("asprintf");
4613 goto done;
4616 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4617 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4618 int is_bad_symlink;
4619 char target_path[PATH_MAX];
4620 ssize_t target_len;
4621 target_len = readlink(ct->ondisk_path, target_path,
4622 sizeof(target_path));
4623 if (target_len == -1) {
4624 err = got_error_from_errno2("readlink",
4625 ct->ondisk_path);
4626 goto done;
4628 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4629 target_len, ct->ondisk_path, a->worktree->root_path);
4630 if (err)
4631 goto done;
4632 if (is_bad_symlink) {
4633 err = got_error_path(ct->ondisk_path,
4634 GOT_ERR_BAD_SYMLINK);
4635 goto done;
4640 ct->status = status;
4641 ct->staged_status = staged_status;
4642 ct->blob_id = NULL; /* will be filled in when blob gets created */
4643 if (ct->status != GOT_STATUS_ADD &&
4644 ct->staged_status != GOT_STATUS_ADD) {
4645 ct->base_blob_id = got_object_id_dup(blob_id);
4646 if (ct->base_blob_id == NULL) {
4647 err = got_error_from_errno("got_object_id_dup");
4648 goto done;
4650 ct->base_commit_id = got_object_id_dup(commit_id);
4651 if (ct->base_commit_id == NULL) {
4652 err = got_error_from_errno("got_object_id_dup");
4653 goto done;
4656 if (ct->staged_status == GOT_STATUS_ADD ||
4657 ct->staged_status == GOT_STATUS_MODIFY) {
4658 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4659 if (ct->staged_blob_id == NULL) {
4660 err = got_error_from_errno("got_object_id_dup");
4661 goto done;
4664 ct->path = strdup(path);
4665 if (ct->path == NULL) {
4666 err = got_error_from_errno("strdup");
4667 goto done;
4669 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4670 done:
4671 if (ct && (err || new == NULL))
4672 free_commitable(ct);
4673 free(parent_path);
4674 free(path);
4675 return err;
4678 static const struct got_error *write_tree(struct got_object_id **, int *,
4679 struct got_tree_object *, const char *, struct got_pathlist_head *,
4680 got_worktree_status_cb status_cb, void *status_arg,
4681 struct got_repository *);
4683 static const struct got_error *
4684 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4685 struct got_tree_entry *te, const char *parent_path,
4686 struct got_pathlist_head *commitable_paths,
4687 got_worktree_status_cb status_cb, void *status_arg,
4688 struct got_repository *repo)
4690 const struct got_error *err = NULL;
4691 struct got_tree_object *subtree;
4692 char *subpath;
4694 if (asprintf(&subpath, "%s%s%s", parent_path,
4695 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4696 return got_error_from_errno("asprintf");
4698 err = got_object_open_as_tree(&subtree, repo, &te->id);
4699 if (err)
4700 return err;
4702 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4703 commitable_paths, status_cb, status_arg, repo);
4704 got_object_tree_close(subtree);
4705 free(subpath);
4706 return err;
4709 static const struct got_error *
4710 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4712 const struct got_error *err = NULL;
4713 char *ct_parent_path = NULL;
4715 *match = 0;
4717 if (strchr(ct->in_repo_path, '/') == NULL) {
4718 *match = got_path_is_root_dir(path);
4719 return NULL;
4722 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4723 if (err)
4724 return err;
4725 *match = (strcmp(path, ct_parent_path) == 0);
4726 free(ct_parent_path);
4727 return err;
4730 static mode_t
4731 get_ct_file_mode(struct got_commitable *ct)
4733 if (S_ISLNK(ct->mode))
4734 return S_IFLNK;
4736 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4739 static const struct got_error *
4740 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4741 struct got_tree_entry *te, struct got_commitable *ct)
4743 const struct got_error *err = NULL;
4745 *new_te = NULL;
4747 err = got_object_tree_entry_dup(new_te, te);
4748 if (err)
4749 goto done;
4751 (*new_te)->mode = get_ct_file_mode(ct);
4753 if (ct->staged_status == GOT_STATUS_MODIFY)
4754 memcpy(&(*new_te)->id, ct->staged_blob_id,
4755 sizeof((*new_te)->id));
4756 else
4757 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4758 done:
4759 if (err && *new_te) {
4760 free(*new_te);
4761 *new_te = NULL;
4763 return err;
4766 static const struct got_error *
4767 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4768 struct got_commitable *ct)
4770 const struct got_error *err = NULL;
4771 char *ct_name;
4773 *new_te = NULL;
4775 *new_te = calloc(1, sizeof(**new_te));
4776 if (*new_te == NULL)
4777 return got_error_from_errno("calloc");
4779 ct_name = basename(ct->path);
4780 if (ct_name == NULL) {
4781 err = got_error_from_errno2("basename", ct->path);
4782 goto done;
4784 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4785 sizeof((*new_te)->name)) {
4786 err = got_error(GOT_ERR_NO_SPACE);
4787 goto done;
4790 (*new_te)->mode = get_ct_file_mode(ct);
4792 if (ct->staged_status == GOT_STATUS_ADD)
4793 memcpy(&(*new_te)->id, ct->staged_blob_id,
4794 sizeof((*new_te)->id));
4795 else
4796 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4797 done:
4798 if (err && *new_te) {
4799 free(*new_te);
4800 *new_te = NULL;
4802 return err;
4805 static const struct got_error *
4806 insert_tree_entry(struct got_tree_entry *new_te,
4807 struct got_pathlist_head *paths)
4809 const struct got_error *err = NULL;
4810 struct got_pathlist_entry *new_pe;
4812 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4813 if (err)
4814 return err;
4815 if (new_pe == NULL)
4816 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4817 return NULL;
4820 static const struct got_error *
4821 report_ct_status(struct got_commitable *ct,
4822 got_worktree_status_cb status_cb, void *status_arg)
4824 const char *ct_path = ct->path;
4825 unsigned char status;
4827 while (ct_path[0] == '/')
4828 ct_path++;
4830 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4831 status = ct->staged_status;
4832 else
4833 status = ct->status;
4835 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4836 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4839 static const struct got_error *
4840 match_modified_subtree(int *modified, struct got_tree_entry *te,
4841 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4843 const struct got_error *err = NULL;
4844 struct got_pathlist_entry *pe;
4845 char *te_path;
4847 *modified = 0;
4849 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4850 got_path_is_root_dir(base_tree_path) ? "" : "/",
4851 te->name) == -1)
4852 return got_error_from_errno("asprintf");
4854 TAILQ_FOREACH(pe, commitable_paths, entry) {
4855 struct got_commitable *ct = pe->data;
4856 *modified = got_path_is_child(ct->in_repo_path, te_path,
4857 strlen(te_path));
4858 if (*modified)
4859 break;
4862 free(te_path);
4863 return err;
4866 static const struct got_error *
4867 match_deleted_or_modified_ct(struct got_commitable **ctp,
4868 struct got_tree_entry *te, const char *base_tree_path,
4869 struct got_pathlist_head *commitable_paths)
4871 const struct got_error *err = NULL;
4872 struct got_pathlist_entry *pe;
4874 *ctp = NULL;
4876 TAILQ_FOREACH(pe, commitable_paths, entry) {
4877 struct got_commitable *ct = pe->data;
4878 char *ct_name = NULL;
4879 int path_matches;
4881 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4882 if (ct->status != GOT_STATUS_MODIFY &&
4883 ct->status != GOT_STATUS_MODE_CHANGE &&
4884 ct->status != GOT_STATUS_DELETE)
4885 continue;
4886 } else {
4887 if (ct->staged_status != GOT_STATUS_MODIFY &&
4888 ct->staged_status != GOT_STATUS_DELETE)
4889 continue;
4892 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4893 continue;
4895 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4896 if (err)
4897 return err;
4898 if (!path_matches)
4899 continue;
4901 ct_name = basename(pe->path);
4902 if (ct_name == NULL)
4903 return got_error_from_errno2("basename", pe->path);
4905 if (strcmp(te->name, ct_name) != 0)
4906 continue;
4908 *ctp = ct;
4909 break;
4912 return err;
4915 static const struct got_error *
4916 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4917 const char *child_path, const char *path_base_tree,
4918 struct got_pathlist_head *commitable_paths,
4919 got_worktree_status_cb status_cb, void *status_arg,
4920 struct got_repository *repo)
4922 const struct got_error *err = NULL;
4923 struct got_tree_entry *new_te;
4924 char *subtree_path;
4925 struct got_object_id *id = NULL;
4926 int nentries;
4928 *new_tep = NULL;
4930 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4931 got_path_is_root_dir(path_base_tree) ? "" : "/",
4932 child_path) == -1)
4933 return got_error_from_errno("asprintf");
4935 new_te = calloc(1, sizeof(*new_te));
4936 if (new_te == NULL)
4937 return got_error_from_errno("calloc");
4938 new_te->mode = S_IFDIR;
4940 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4941 sizeof(new_te->name)) {
4942 err = got_error(GOT_ERR_NO_SPACE);
4943 goto done;
4945 err = write_tree(&id, &nentries, NULL, subtree_path,
4946 commitable_paths, status_cb, status_arg, repo);
4947 if (err) {
4948 free(new_te);
4949 goto done;
4951 memcpy(&new_te->id, id, sizeof(new_te->id));
4952 done:
4953 free(id);
4954 free(subtree_path);
4955 if (err == NULL)
4956 *new_tep = new_te;
4957 return err;
4960 static const struct got_error *
4961 write_tree(struct got_object_id **new_tree_id, int *nentries,
4962 struct got_tree_object *base_tree, const char *path_base_tree,
4963 struct got_pathlist_head *commitable_paths,
4964 got_worktree_status_cb status_cb, void *status_arg,
4965 struct got_repository *repo)
4967 const struct got_error *err = NULL;
4968 struct got_pathlist_head paths;
4969 struct got_tree_entry *te, *new_te = NULL;
4970 struct got_pathlist_entry *pe;
4972 TAILQ_INIT(&paths);
4973 *nentries = 0;
4975 /* Insert, and recurse into, newly added entries first. */
4976 TAILQ_FOREACH(pe, commitable_paths, entry) {
4977 struct got_commitable *ct = pe->data;
4978 char *child_path = NULL, *slash;
4980 if ((ct->status != GOT_STATUS_ADD &&
4981 ct->staged_status != GOT_STATUS_ADD) ||
4982 (ct->flags & GOT_COMMITABLE_ADDED))
4983 continue;
4985 if (!got_path_is_child(pe->path, path_base_tree,
4986 strlen(path_base_tree)))
4987 continue;
4989 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4990 pe->path);
4991 if (err)
4992 goto done;
4994 slash = strchr(child_path, '/');
4995 if (slash == NULL) {
4996 err = alloc_added_blob_tree_entry(&new_te, ct);
4997 if (err)
4998 goto done;
4999 err = report_ct_status(ct, status_cb, status_arg);
5000 if (err)
5001 goto done;
5002 ct->flags |= GOT_COMMITABLE_ADDED;
5003 err = insert_tree_entry(new_te, &paths);
5004 if (err)
5005 goto done;
5006 (*nentries)++;
5007 } else {
5008 *slash = '\0'; /* trim trailing path components */
5009 if (base_tree == NULL ||
5010 got_object_tree_find_entry(base_tree, child_path)
5011 == NULL) {
5012 err = make_subtree_for_added_blob(&new_te,
5013 child_path, path_base_tree,
5014 commitable_paths, status_cb, status_arg,
5015 repo);
5016 if (err)
5017 goto done;
5018 err = insert_tree_entry(new_te, &paths);
5019 if (err)
5020 goto done;
5021 (*nentries)++;
5026 if (base_tree) {
5027 int i, nbase_entries;
5028 /* Handle modified and deleted entries. */
5029 nbase_entries = got_object_tree_get_nentries(base_tree);
5030 for (i = 0; i < nbase_entries; i++) {
5031 struct got_commitable *ct = NULL;
5033 te = got_object_tree_get_entry(base_tree, i);
5034 if (got_object_tree_entry_is_submodule(te)) {
5035 /* Entry is a submodule; just copy it. */
5036 err = got_object_tree_entry_dup(&new_te, te);
5037 if (err)
5038 goto done;
5039 err = insert_tree_entry(new_te, &paths);
5040 if (err)
5041 goto done;
5042 (*nentries)++;
5043 continue;
5046 if (S_ISDIR(te->mode)) {
5047 int modified;
5048 err = got_object_tree_entry_dup(&new_te, te);
5049 if (err)
5050 goto done;
5051 err = match_modified_subtree(&modified, te,
5052 path_base_tree, commitable_paths);
5053 if (err)
5054 goto done;
5055 /* Avoid recursion into unmodified subtrees. */
5056 if (modified) {
5057 struct got_object_id *new_id;
5058 int nsubentries;
5059 err = write_subtree(&new_id,
5060 &nsubentries, te,
5061 path_base_tree, commitable_paths,
5062 status_cb, status_arg, repo);
5063 if (err)
5064 goto done;
5065 if (nsubentries == 0) {
5066 /* All entries were deleted. */
5067 free(new_id);
5068 continue;
5070 memcpy(&new_te->id, new_id,
5071 sizeof(new_te->id));
5072 free(new_id);
5074 err = insert_tree_entry(new_te, &paths);
5075 if (err)
5076 goto done;
5077 (*nentries)++;
5078 continue;
5081 err = match_deleted_or_modified_ct(&ct, te,
5082 path_base_tree, commitable_paths);
5083 if (err)
5084 goto done;
5085 if (ct) {
5086 /* NB: Deleted entries get dropped here. */
5087 if (ct->status == GOT_STATUS_MODIFY ||
5088 ct->status == GOT_STATUS_MODE_CHANGE ||
5089 ct->staged_status == GOT_STATUS_MODIFY) {
5090 err = alloc_modified_blob_tree_entry(
5091 &new_te, te, ct);
5092 if (err)
5093 goto done;
5094 err = insert_tree_entry(new_te, &paths);
5095 if (err)
5096 goto done;
5097 (*nentries)++;
5099 err = report_ct_status(ct, status_cb,
5100 status_arg);
5101 if (err)
5102 goto done;
5103 } else {
5104 /* Entry is unchanged; just copy it. */
5105 err = got_object_tree_entry_dup(&new_te, te);
5106 if (err)
5107 goto done;
5108 err = insert_tree_entry(new_te, &paths);
5109 if (err)
5110 goto done;
5111 (*nentries)++;
5116 /* Write new list of entries; deleted entries have been dropped. */
5117 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5118 done:
5119 got_pathlist_free(&paths);
5120 return err;
5123 static const struct got_error *
5124 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5125 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5126 int have_staged_files)
5128 const struct got_error *err = NULL;
5129 struct got_pathlist_entry *pe;
5131 TAILQ_FOREACH(pe, commitable_paths, entry) {
5132 struct got_fileindex_entry *ie;
5133 struct got_commitable *ct = pe->data;
5135 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5136 if (ie) {
5137 if (ct->status == GOT_STATUS_DELETE ||
5138 ct->staged_status == GOT_STATUS_DELETE) {
5139 got_fileindex_entry_remove(fileindex, ie);
5140 } else if (ct->staged_status == GOT_STATUS_ADD ||
5141 ct->staged_status == GOT_STATUS_MODIFY) {
5142 got_fileindex_entry_stage_set(ie,
5143 GOT_FILEIDX_STAGE_NONE);
5144 err = got_fileindex_entry_update(ie,
5145 ct->ondisk_path, ct->staged_blob_id->sha1,
5146 new_base_commit_id->sha1,
5147 !have_staged_files);
5148 } else
5149 err = got_fileindex_entry_update(ie,
5150 ct->ondisk_path, ct->blob_id->sha1,
5151 new_base_commit_id->sha1,
5152 !have_staged_files);
5153 } else {
5154 err = got_fileindex_entry_alloc(&ie, pe->path);
5155 if (err)
5156 break;
5157 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5158 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5159 if (err) {
5160 got_fileindex_entry_free(ie);
5161 break;
5163 err = got_fileindex_entry_add(fileindex, ie);
5164 if (err) {
5165 got_fileindex_entry_free(ie);
5166 break;
5170 return err;
5174 static const struct got_error *
5175 check_out_of_date(const char *in_repo_path, unsigned char status,
5176 unsigned char staged_status, struct got_object_id *base_blob_id,
5177 struct got_object_id *base_commit_id,
5178 struct got_object_id *head_commit_id, struct got_repository *repo,
5179 int ood_errcode)
5181 const struct got_error *err = NULL;
5182 struct got_object_id *id = NULL;
5184 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5185 /* Trivial case: base commit == head commit */
5186 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5187 return NULL;
5189 * Ensure file content which local changes were based
5190 * on matches file content in the branch head.
5192 err = got_object_id_by_path(&id, repo, head_commit_id,
5193 in_repo_path);
5194 if (err) {
5195 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5196 err = got_error(ood_errcode);
5197 goto done;
5198 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5199 err = got_error(ood_errcode);
5200 } else {
5201 /* Require that added files don't exist in the branch head. */
5202 err = got_object_id_by_path(&id, repo, head_commit_id,
5203 in_repo_path);
5204 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5205 goto done;
5206 err = id ? got_error(ood_errcode) : NULL;
5208 done:
5209 free(id);
5210 return err;
5213 const struct got_error *
5214 commit_worktree(struct got_object_id **new_commit_id,
5215 struct got_pathlist_head *commitable_paths,
5216 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5217 const char *author, const char *committer,
5218 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5219 got_worktree_status_cb status_cb, void *status_arg,
5220 struct got_repository *repo)
5222 const struct got_error *err = NULL, *unlockerr = NULL;
5223 struct got_pathlist_entry *pe;
5224 const char *head_ref_name = NULL;
5225 struct got_commit_object *head_commit = NULL;
5226 struct got_reference *head_ref2 = NULL;
5227 struct got_object_id *head_commit_id2 = NULL;
5228 struct got_tree_object *head_tree = NULL;
5229 struct got_object_id *new_tree_id = NULL;
5230 int nentries;
5231 struct got_object_id_queue parent_ids;
5232 struct got_object_qid *pid = NULL;
5233 char *logmsg = NULL;
5235 *new_commit_id = NULL;
5237 SIMPLEQ_INIT(&parent_ids);
5239 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5240 if (err)
5241 goto done;
5243 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5244 if (err)
5245 goto done;
5247 if (commit_msg_cb != NULL) {
5248 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5249 if (err)
5250 goto done;
5253 if (logmsg == NULL || strlen(logmsg) == 0) {
5254 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5255 goto done;
5258 /* Create blobs from added and modified files and record their IDs. */
5259 TAILQ_FOREACH(pe, commitable_paths, entry) {
5260 struct got_commitable *ct = pe->data;
5261 char *ondisk_path;
5263 /* Blobs for staged files already exist. */
5264 if (ct->staged_status == GOT_STATUS_ADD ||
5265 ct->staged_status == GOT_STATUS_MODIFY)
5266 continue;
5268 if (ct->status != GOT_STATUS_ADD &&
5269 ct->status != GOT_STATUS_MODIFY &&
5270 ct->status != GOT_STATUS_MODE_CHANGE)
5271 continue;
5273 if (asprintf(&ondisk_path, "%s/%s",
5274 worktree->root_path, pe->path) == -1) {
5275 err = got_error_from_errno("asprintf");
5276 goto done;
5278 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5279 free(ondisk_path);
5280 if (err)
5281 goto done;
5284 /* Recursively write new tree objects. */
5285 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5286 commitable_paths, status_cb, status_arg, repo);
5287 if (err)
5288 goto done;
5290 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5291 if (err)
5292 goto done;
5293 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5294 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5295 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5296 got_object_qid_free(pid);
5297 if (logmsg != NULL)
5298 free(logmsg);
5299 if (err)
5300 goto done;
5302 /* Check if a concurrent commit to our branch has occurred. */
5303 head_ref_name = got_worktree_get_head_ref_name(worktree);
5304 if (head_ref_name == NULL) {
5305 err = got_error_from_errno("got_worktree_get_head_ref_name");
5306 goto done;
5308 /* Lock the reference here to prevent concurrent modification. */
5309 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5310 if (err)
5311 goto done;
5312 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5313 if (err)
5314 goto done;
5315 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5316 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5317 goto done;
5319 /* Update branch head in repository. */
5320 err = got_ref_change_ref(head_ref2, *new_commit_id);
5321 if (err)
5322 goto done;
5323 err = got_ref_write(head_ref2, repo);
5324 if (err)
5325 goto done;
5327 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5328 if (err)
5329 goto done;
5331 err = ref_base_commit(worktree, repo);
5332 if (err)
5333 goto done;
5334 done:
5335 if (head_tree)
5336 got_object_tree_close(head_tree);
5337 if (head_commit)
5338 got_object_commit_close(head_commit);
5339 free(head_commit_id2);
5340 if (head_ref2) {
5341 unlockerr = got_ref_unlock(head_ref2);
5342 if (unlockerr && err == NULL)
5343 err = unlockerr;
5344 got_ref_close(head_ref2);
5346 return err;
5349 static const struct got_error *
5350 check_path_is_commitable(const char *path,
5351 struct got_pathlist_head *commitable_paths)
5353 struct got_pathlist_entry *cpe = NULL;
5354 size_t path_len = strlen(path);
5356 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5357 struct got_commitable *ct = cpe->data;
5358 const char *ct_path = ct->path;
5360 while (ct_path[0] == '/')
5361 ct_path++;
5363 if (strcmp(path, ct_path) == 0 ||
5364 got_path_is_child(ct_path, path, path_len))
5365 break;
5368 if (cpe == NULL)
5369 return got_error_path(path, GOT_ERR_BAD_PATH);
5371 return NULL;
5374 static const struct got_error *
5375 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5377 int *have_staged_files = arg;
5379 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5380 *have_staged_files = 1;
5381 return got_error(GOT_ERR_CANCELLED);
5384 return NULL;
5387 static const struct got_error *
5388 check_non_staged_files(struct got_fileindex *fileindex,
5389 struct got_pathlist_head *paths)
5391 struct got_pathlist_entry *pe;
5392 struct got_fileindex_entry *ie;
5394 TAILQ_FOREACH(pe, paths, entry) {
5395 if (pe->path[0] == '\0')
5396 continue;
5397 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5398 if (ie == NULL)
5399 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5400 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5401 return got_error_path(pe->path,
5402 GOT_ERR_FILE_NOT_STAGED);
5405 return NULL;
5408 const struct got_error *
5409 got_worktree_commit(struct got_object_id **new_commit_id,
5410 struct got_worktree *worktree, struct got_pathlist_head *paths,
5411 const char *author, const char *committer, int allow_bad_symlinks,
5412 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5413 got_worktree_status_cb status_cb, void *status_arg,
5414 struct got_repository *repo)
5416 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5417 struct got_fileindex *fileindex = NULL;
5418 char *fileindex_path = NULL;
5419 struct got_pathlist_head commitable_paths;
5420 struct collect_commitables_arg cc_arg;
5421 struct got_pathlist_entry *pe;
5422 struct got_reference *head_ref = NULL;
5423 struct got_object_id *head_commit_id = NULL;
5424 int have_staged_files = 0;
5426 *new_commit_id = NULL;
5428 TAILQ_INIT(&commitable_paths);
5430 err = lock_worktree(worktree, LOCK_EX);
5431 if (err)
5432 goto done;
5434 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5435 if (err)
5436 goto done;
5438 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5439 if (err)
5440 goto done;
5442 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5443 if (err)
5444 goto done;
5446 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5447 &have_staged_files);
5448 if (err && err->code != GOT_ERR_CANCELLED)
5449 goto done;
5450 if (have_staged_files) {
5451 err = check_non_staged_files(fileindex, paths);
5452 if (err)
5453 goto done;
5456 cc_arg.commitable_paths = &commitable_paths;
5457 cc_arg.worktree = worktree;
5458 cc_arg.fileindex = fileindex;
5459 cc_arg.repo = repo;
5460 cc_arg.have_staged_files = have_staged_files;
5461 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5462 TAILQ_FOREACH(pe, paths, entry) {
5463 err = worktree_status(worktree, pe->path, fileindex, repo,
5464 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5465 if (err)
5466 goto done;
5469 if (TAILQ_EMPTY(&commitable_paths)) {
5470 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5471 goto done;
5474 TAILQ_FOREACH(pe, paths, entry) {
5475 err = check_path_is_commitable(pe->path, &commitable_paths);
5476 if (err)
5477 goto done;
5480 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5481 struct got_commitable *ct = pe->data;
5482 const char *ct_path = ct->in_repo_path;
5484 while (ct_path[0] == '/')
5485 ct_path++;
5486 err = check_out_of_date(ct_path, ct->status,
5487 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5488 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5489 if (err)
5490 goto done;
5494 err = commit_worktree(new_commit_id, &commitable_paths,
5495 head_commit_id, worktree, author, committer,
5496 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5497 if (err)
5498 goto done;
5500 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5501 fileindex, have_staged_files);
5502 sync_err = sync_fileindex(fileindex, fileindex_path);
5503 if (sync_err && err == NULL)
5504 err = sync_err;
5505 done:
5506 if (fileindex)
5507 got_fileindex_free(fileindex);
5508 free(fileindex_path);
5509 unlockerr = lock_worktree(worktree, LOCK_SH);
5510 if (unlockerr && err == NULL)
5511 err = unlockerr;
5512 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5513 struct got_commitable *ct = pe->data;
5514 free_commitable(ct);
5516 got_pathlist_free(&commitable_paths);
5517 return err;
5520 const char *
5521 got_commitable_get_path(struct got_commitable *ct)
5523 return ct->path;
5526 unsigned int
5527 got_commitable_get_status(struct got_commitable *ct)
5529 return ct->status;
5532 struct check_rebase_ok_arg {
5533 struct got_worktree *worktree;
5534 struct got_repository *repo;
5537 static const struct got_error *
5538 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5540 const struct got_error *err = NULL;
5541 struct check_rebase_ok_arg *a = arg;
5542 unsigned char status;
5543 struct stat sb;
5544 char *ondisk_path;
5546 /* Reject rebase of a work tree with mixed base commits. */
5547 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5548 SHA1_DIGEST_LENGTH))
5549 return got_error(GOT_ERR_MIXED_COMMITS);
5551 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5552 == -1)
5553 return got_error_from_errno("asprintf");
5555 /* Reject rebase of a work tree with modified or staged files. */
5556 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5557 free(ondisk_path);
5558 if (err)
5559 return err;
5561 if (status != GOT_STATUS_NO_CHANGE)
5562 return got_error(GOT_ERR_MODIFIED);
5563 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5564 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5566 return NULL;
5569 const struct got_error *
5570 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5571 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5572 struct got_worktree *worktree, struct got_reference *branch,
5573 struct got_repository *repo)
5575 const struct got_error *err = NULL;
5576 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5577 char *branch_ref_name = NULL;
5578 char *fileindex_path = NULL;
5579 struct check_rebase_ok_arg ok_arg;
5580 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5581 struct got_object_id *wt_branch_tip = NULL;
5583 *new_base_branch_ref = NULL;
5584 *tmp_branch = NULL;
5585 *fileindex = NULL;
5587 err = lock_worktree(worktree, LOCK_EX);
5588 if (err)
5589 return err;
5591 err = open_fileindex(fileindex, &fileindex_path, worktree);
5592 if (err)
5593 goto done;
5595 ok_arg.worktree = worktree;
5596 ok_arg.repo = repo;
5597 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5598 &ok_arg);
5599 if (err)
5600 goto done;
5602 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5603 if (err)
5604 goto done;
5606 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5607 if (err)
5608 goto done;
5610 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5611 if (err)
5612 goto done;
5614 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5615 0);
5616 if (err)
5617 goto done;
5619 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5620 if (err)
5621 goto done;
5622 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5623 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5624 goto done;
5627 err = got_ref_alloc_symref(new_base_branch_ref,
5628 new_base_branch_ref_name, wt_branch);
5629 if (err)
5630 goto done;
5631 err = got_ref_write(*new_base_branch_ref, repo);
5632 if (err)
5633 goto done;
5635 /* TODO Lock original branch's ref while rebasing? */
5637 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5638 if (err)
5639 goto done;
5641 err = got_ref_write(branch_ref, repo);
5642 if (err)
5643 goto done;
5645 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5646 worktree->base_commit_id);
5647 if (err)
5648 goto done;
5649 err = got_ref_write(*tmp_branch, repo);
5650 if (err)
5651 goto done;
5653 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5654 if (err)
5655 goto done;
5656 done:
5657 free(fileindex_path);
5658 free(tmp_branch_name);
5659 free(new_base_branch_ref_name);
5660 free(branch_ref_name);
5661 if (branch_ref)
5662 got_ref_close(branch_ref);
5663 if (wt_branch)
5664 got_ref_close(wt_branch);
5665 free(wt_branch_tip);
5666 if (err) {
5667 if (*new_base_branch_ref) {
5668 got_ref_close(*new_base_branch_ref);
5669 *new_base_branch_ref = NULL;
5671 if (*tmp_branch) {
5672 got_ref_close(*tmp_branch);
5673 *tmp_branch = NULL;
5675 if (*fileindex) {
5676 got_fileindex_free(*fileindex);
5677 *fileindex = NULL;
5679 lock_worktree(worktree, LOCK_SH);
5681 return err;
5684 const struct got_error *
5685 got_worktree_rebase_continue(struct got_object_id **commit_id,
5686 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5687 struct got_reference **branch, struct got_fileindex **fileindex,
5688 struct got_worktree *worktree, struct got_repository *repo)
5690 const struct got_error *err;
5691 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5692 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5693 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5694 char *fileindex_path = NULL;
5695 int have_staged_files = 0;
5697 *commit_id = NULL;
5698 *new_base_branch = NULL;
5699 *tmp_branch = NULL;
5700 *branch = NULL;
5701 *fileindex = NULL;
5703 err = lock_worktree(worktree, LOCK_EX);
5704 if (err)
5705 return err;
5707 err = open_fileindex(fileindex, &fileindex_path, worktree);
5708 if (err)
5709 goto done;
5711 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5712 &have_staged_files);
5713 if (err && err->code != GOT_ERR_CANCELLED)
5714 goto done;
5715 if (have_staged_files) {
5716 err = got_error(GOT_ERR_STAGED_PATHS);
5717 goto done;
5720 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5721 if (err)
5722 goto done;
5724 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5725 if (err)
5726 goto done;
5728 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5729 if (err)
5730 goto done;
5732 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5733 if (err)
5734 goto done;
5736 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5737 if (err)
5738 goto done;
5740 err = got_ref_open(branch, repo,
5741 got_ref_get_symref_target(branch_ref), 0);
5742 if (err)
5743 goto done;
5745 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5746 if (err)
5747 goto done;
5749 err = got_ref_resolve(commit_id, repo, commit_ref);
5750 if (err)
5751 goto done;
5753 err = got_ref_open(new_base_branch, repo,
5754 new_base_branch_ref_name, 0);
5755 if (err)
5756 goto done;
5758 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5759 if (err)
5760 goto done;
5761 done:
5762 free(commit_ref_name);
5763 free(branch_ref_name);
5764 free(fileindex_path);
5765 if (commit_ref)
5766 got_ref_close(commit_ref);
5767 if (branch_ref)
5768 got_ref_close(branch_ref);
5769 if (err) {
5770 free(*commit_id);
5771 *commit_id = NULL;
5772 if (*tmp_branch) {
5773 got_ref_close(*tmp_branch);
5774 *tmp_branch = NULL;
5776 if (*new_base_branch) {
5777 got_ref_close(*new_base_branch);
5778 *new_base_branch = NULL;
5780 if (*branch) {
5781 got_ref_close(*branch);
5782 *branch = NULL;
5784 if (*fileindex) {
5785 got_fileindex_free(*fileindex);
5786 *fileindex = NULL;
5788 lock_worktree(worktree, LOCK_SH);
5790 return err;
5793 const struct got_error *
5794 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5796 const struct got_error *err;
5797 char *tmp_branch_name = NULL;
5799 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5800 if (err)
5801 return err;
5803 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5804 free(tmp_branch_name);
5805 return NULL;
5808 static const struct got_error *
5809 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5810 char **logmsg, void *arg)
5812 *logmsg = arg;
5813 return NULL;
5816 static const struct got_error *
5817 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5818 const char *path, struct got_object_id *blob_id,
5819 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5820 int dirfd, const char *de_name)
5822 return NULL;
5825 struct collect_merged_paths_arg {
5826 got_worktree_checkout_cb progress_cb;
5827 void *progress_arg;
5828 struct got_pathlist_head *merged_paths;
5831 static const struct got_error *
5832 collect_merged_paths(void *arg, unsigned char status, const char *path)
5834 const struct got_error *err;
5835 struct collect_merged_paths_arg *a = arg;
5836 char *p;
5837 struct got_pathlist_entry *new;
5839 err = (*a->progress_cb)(a->progress_arg, status, path);
5840 if (err)
5841 return err;
5843 if (status != GOT_STATUS_MERGE &&
5844 status != GOT_STATUS_ADD &&
5845 status != GOT_STATUS_DELETE &&
5846 status != GOT_STATUS_CONFLICT)
5847 return NULL;
5849 p = strdup(path);
5850 if (p == NULL)
5851 return got_error_from_errno("strdup");
5853 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5854 if (err || new == NULL)
5855 free(p);
5856 return err;
5859 void
5860 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5862 struct got_pathlist_entry *pe;
5864 TAILQ_FOREACH(pe, merged_paths, entry)
5865 free((char *)pe->path);
5867 got_pathlist_free(merged_paths);
5870 static const struct got_error *
5871 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5872 int is_rebase, struct got_repository *repo)
5874 const struct got_error *err;
5875 struct got_reference *commit_ref = NULL;
5877 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5878 if (err) {
5879 if (err->code != GOT_ERR_NOT_REF)
5880 goto done;
5881 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5882 if (err)
5883 goto done;
5884 err = got_ref_write(commit_ref, repo);
5885 if (err)
5886 goto done;
5887 } else if (is_rebase) {
5888 struct got_object_id *stored_id;
5889 int cmp;
5891 err = got_ref_resolve(&stored_id, repo, commit_ref);
5892 if (err)
5893 goto done;
5894 cmp = got_object_id_cmp(commit_id, stored_id);
5895 free(stored_id);
5896 if (cmp != 0) {
5897 err = got_error(GOT_ERR_REBASE_COMMITID);
5898 goto done;
5901 done:
5902 if (commit_ref)
5903 got_ref_close(commit_ref);
5904 return err;
5907 static const struct got_error *
5908 rebase_merge_files(struct got_pathlist_head *merged_paths,
5909 const char *commit_ref_name, struct got_worktree *worktree,
5910 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5911 struct got_object_id *commit_id, struct got_repository *repo,
5912 got_worktree_checkout_cb progress_cb, void *progress_arg,
5913 got_cancel_cb cancel_cb, void *cancel_arg)
5915 const struct got_error *err;
5916 struct got_reference *commit_ref = NULL;
5917 struct collect_merged_paths_arg cmp_arg;
5918 char *fileindex_path;
5920 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5922 err = get_fileindex_path(&fileindex_path, worktree);
5923 if (err)
5924 return err;
5926 cmp_arg.progress_cb = progress_cb;
5927 cmp_arg.progress_arg = progress_arg;
5928 cmp_arg.merged_paths = merged_paths;
5929 err = merge_files(worktree, fileindex, fileindex_path,
5930 parent_commit_id, commit_id, repo, collect_merged_paths,
5931 &cmp_arg, cancel_cb, cancel_arg);
5932 if (commit_ref)
5933 got_ref_close(commit_ref);
5934 return err;
5937 const struct got_error *
5938 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5939 struct got_worktree *worktree, struct got_fileindex *fileindex,
5940 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5941 struct got_repository *repo,
5942 got_worktree_checkout_cb progress_cb, void *progress_arg,
5943 got_cancel_cb cancel_cb, void *cancel_arg)
5945 const struct got_error *err;
5946 char *commit_ref_name;
5948 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5949 if (err)
5950 return err;
5952 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5953 if (err)
5954 goto done;
5956 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5957 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5958 progress_arg, cancel_cb, cancel_arg);
5959 done:
5960 free(commit_ref_name);
5961 return err;
5964 const struct got_error *
5965 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5966 struct got_worktree *worktree, struct got_fileindex *fileindex,
5967 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5968 struct got_repository *repo,
5969 got_worktree_checkout_cb progress_cb, void *progress_arg,
5970 got_cancel_cb cancel_cb, void *cancel_arg)
5972 const struct got_error *err;
5973 char *commit_ref_name;
5975 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5976 if (err)
5977 return err;
5979 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5980 if (err)
5981 goto done;
5983 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5984 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5985 progress_arg, cancel_cb, cancel_arg);
5986 done:
5987 free(commit_ref_name);
5988 return err;
5991 static const struct got_error *
5992 rebase_commit(struct got_object_id **new_commit_id,
5993 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5994 struct got_worktree *worktree, struct got_fileindex *fileindex,
5995 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5996 const char *new_logmsg, struct got_repository *repo)
5998 const struct got_error *err, *sync_err;
5999 struct got_pathlist_head commitable_paths;
6000 struct collect_commitables_arg cc_arg;
6001 char *fileindex_path = NULL;
6002 struct got_reference *head_ref = NULL;
6003 struct got_object_id *head_commit_id = NULL;
6004 char *logmsg = NULL;
6006 TAILQ_INIT(&commitable_paths);
6007 *new_commit_id = NULL;
6009 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6011 err = get_fileindex_path(&fileindex_path, worktree);
6012 if (err)
6013 return err;
6015 cc_arg.commitable_paths = &commitable_paths;
6016 cc_arg.worktree = worktree;
6017 cc_arg.repo = repo;
6018 cc_arg.have_staged_files = 0;
6020 * If possible get the status of individual files directly to
6021 * avoid crawling the entire work tree once per rebased commit.
6022 * TODO: Ideally, merged_paths would contain a list of commitables
6023 * we could use so we could skip worktree_status() entirely.
6025 if (merged_paths) {
6026 struct got_pathlist_entry *pe;
6027 TAILQ_FOREACH(pe, merged_paths, entry) {
6028 err = worktree_status(worktree, pe->path, fileindex,
6029 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6030 0);
6031 if (err)
6032 goto done;
6034 } else {
6035 err = worktree_status(worktree, "", fileindex, repo,
6036 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6037 if (err)
6038 goto done;
6041 if (TAILQ_EMPTY(&commitable_paths)) {
6042 /* No-op change; commit will be elided. */
6043 err = got_ref_delete(commit_ref, repo);
6044 if (err)
6045 goto done;
6046 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6047 goto done;
6050 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6051 if (err)
6052 goto done;
6054 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6055 if (err)
6056 goto done;
6058 if (new_logmsg) {
6059 logmsg = strdup(new_logmsg);
6060 if (logmsg == NULL) {
6061 err = got_error_from_errno("strdup");
6062 goto done;
6064 } else {
6065 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6066 if (err)
6067 goto done;
6070 /* NB: commit_worktree will call free(logmsg) */
6071 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6072 worktree, got_object_commit_get_author(orig_commit),
6073 got_object_commit_get_committer(orig_commit),
6074 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6075 if (err)
6076 goto done;
6078 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6079 if (err)
6080 goto done;
6082 err = got_ref_delete(commit_ref, repo);
6083 if (err)
6084 goto done;
6086 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6087 fileindex, 0);
6088 sync_err = sync_fileindex(fileindex, fileindex_path);
6089 if (sync_err && err == NULL)
6090 err = sync_err;
6091 done:
6092 free(fileindex_path);
6093 free(head_commit_id);
6094 if (head_ref)
6095 got_ref_close(head_ref);
6096 if (err) {
6097 free(*new_commit_id);
6098 *new_commit_id = NULL;
6100 return err;
6103 const struct got_error *
6104 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6105 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6106 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6107 struct got_commit_object *orig_commit,
6108 struct got_object_id *orig_commit_id, struct got_repository *repo)
6110 const struct got_error *err;
6111 char *commit_ref_name;
6112 struct got_reference *commit_ref = NULL;
6113 struct got_object_id *commit_id = NULL;
6115 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6116 if (err)
6117 return err;
6119 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6120 if (err)
6121 goto done;
6122 err = got_ref_resolve(&commit_id, repo, commit_ref);
6123 if (err)
6124 goto done;
6125 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6126 err = got_error(GOT_ERR_REBASE_COMMITID);
6127 goto done;
6130 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6131 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6132 done:
6133 if (commit_ref)
6134 got_ref_close(commit_ref);
6135 free(commit_ref_name);
6136 free(commit_id);
6137 return err;
6140 const struct got_error *
6141 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6142 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6143 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6144 struct got_commit_object *orig_commit,
6145 struct got_object_id *orig_commit_id, const char *new_logmsg,
6146 struct got_repository *repo)
6148 const struct got_error *err;
6149 char *commit_ref_name;
6150 struct got_reference *commit_ref = NULL;
6152 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6153 if (err)
6154 return err;
6156 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6157 if (err)
6158 goto done;
6160 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6161 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6162 done:
6163 if (commit_ref)
6164 got_ref_close(commit_ref);
6165 free(commit_ref_name);
6166 return err;
6169 const struct got_error *
6170 got_worktree_rebase_postpone(struct got_worktree *worktree,
6171 struct got_fileindex *fileindex)
6173 if (fileindex)
6174 got_fileindex_free(fileindex);
6175 return lock_worktree(worktree, LOCK_SH);
6178 static const struct got_error *
6179 delete_ref(const char *name, struct got_repository *repo)
6181 const struct got_error *err;
6182 struct got_reference *ref;
6184 err = got_ref_open(&ref, repo, name, 0);
6185 if (err) {
6186 if (err->code == GOT_ERR_NOT_REF)
6187 return NULL;
6188 return err;
6191 err = got_ref_delete(ref, repo);
6192 got_ref_close(ref);
6193 return err;
6196 static const struct got_error *
6197 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6199 const struct got_error *err;
6200 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6201 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6203 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6204 if (err)
6205 goto done;
6206 err = delete_ref(tmp_branch_name, repo);
6207 if (err)
6208 goto done;
6210 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6211 if (err)
6212 goto done;
6213 err = delete_ref(new_base_branch_ref_name, repo);
6214 if (err)
6215 goto done;
6217 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6218 if (err)
6219 goto done;
6220 err = delete_ref(branch_ref_name, repo);
6221 if (err)
6222 goto done;
6224 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6225 if (err)
6226 goto done;
6227 err = delete_ref(commit_ref_name, repo);
6228 if (err)
6229 goto done;
6231 done:
6232 free(tmp_branch_name);
6233 free(new_base_branch_ref_name);
6234 free(branch_ref_name);
6235 free(commit_ref_name);
6236 return err;
6239 const struct got_error *
6240 got_worktree_rebase_complete(struct got_worktree *worktree,
6241 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6242 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6243 struct got_repository *repo)
6245 const struct got_error *err, *unlockerr;
6246 struct got_object_id *new_head_commit_id = NULL;
6248 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6249 if (err)
6250 return err;
6252 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6253 if (err)
6254 goto done;
6256 err = got_ref_write(rebased_branch, repo);
6257 if (err)
6258 goto done;
6260 err = got_worktree_set_head_ref(worktree, rebased_branch);
6261 if (err)
6262 goto done;
6264 err = delete_rebase_refs(worktree, repo);
6265 done:
6266 if (fileindex)
6267 got_fileindex_free(fileindex);
6268 free(new_head_commit_id);
6269 unlockerr = lock_worktree(worktree, LOCK_SH);
6270 if (unlockerr && err == NULL)
6271 err = unlockerr;
6272 return err;
6275 const struct got_error *
6276 got_worktree_rebase_abort(struct got_worktree *worktree,
6277 struct got_fileindex *fileindex, struct got_repository *repo,
6278 struct got_reference *new_base_branch,
6279 got_worktree_checkout_cb progress_cb, void *progress_arg)
6281 const struct got_error *err, *unlockerr, *sync_err;
6282 struct got_reference *resolved = NULL;
6283 struct got_object_id *commit_id = NULL;
6284 char *fileindex_path = NULL;
6285 struct revert_file_args rfa;
6286 struct got_object_id *tree_id = NULL;
6288 err = lock_worktree(worktree, LOCK_EX);
6289 if (err)
6290 return err;
6292 err = got_ref_open(&resolved, repo,
6293 got_ref_get_symref_target(new_base_branch), 0);
6294 if (err)
6295 goto done;
6297 err = got_worktree_set_head_ref(worktree, resolved);
6298 if (err)
6299 goto done;
6302 * XXX commits to the base branch could have happened while
6303 * we were busy rebasing; should we store the original commit ID
6304 * when rebase begins and read it back here?
6306 err = got_ref_resolve(&commit_id, repo, resolved);
6307 if (err)
6308 goto done;
6310 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6311 if (err)
6312 goto done;
6314 err = got_object_id_by_path(&tree_id, repo,
6315 worktree->base_commit_id, worktree->path_prefix);
6316 if (err)
6317 goto done;
6319 err = delete_rebase_refs(worktree, repo);
6320 if (err)
6321 goto done;
6323 err = get_fileindex_path(&fileindex_path, worktree);
6324 if (err)
6325 goto done;
6327 rfa.worktree = worktree;
6328 rfa.fileindex = fileindex;
6329 rfa.progress_cb = progress_cb;
6330 rfa.progress_arg = progress_arg;
6331 rfa.patch_cb = NULL;
6332 rfa.patch_arg = NULL;
6333 rfa.repo = repo;
6334 err = worktree_status(worktree, "", fileindex, repo,
6335 revert_file, &rfa, NULL, NULL, 0, 0);
6336 if (err)
6337 goto sync;
6339 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6340 repo, progress_cb, progress_arg, NULL, NULL);
6341 sync:
6342 sync_err = sync_fileindex(fileindex, fileindex_path);
6343 if (sync_err && err == NULL)
6344 err = sync_err;
6345 done:
6346 got_ref_close(resolved);
6347 free(tree_id);
6348 free(commit_id);
6349 if (fileindex)
6350 got_fileindex_free(fileindex);
6351 free(fileindex_path);
6353 unlockerr = lock_worktree(worktree, LOCK_SH);
6354 if (unlockerr && err == NULL)
6355 err = unlockerr;
6356 return err;
6359 const struct got_error *
6360 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6361 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6362 struct got_fileindex **fileindex, struct got_worktree *worktree,
6363 struct got_repository *repo)
6365 const struct got_error *err = NULL;
6366 char *tmp_branch_name = NULL;
6367 char *branch_ref_name = NULL;
6368 char *base_commit_ref_name = NULL;
6369 char *fileindex_path = NULL;
6370 struct check_rebase_ok_arg ok_arg;
6371 struct got_reference *wt_branch = NULL;
6372 struct got_reference *base_commit_ref = NULL;
6374 *tmp_branch = NULL;
6375 *branch_ref = NULL;
6376 *base_commit_id = NULL;
6377 *fileindex = NULL;
6379 err = lock_worktree(worktree, LOCK_EX);
6380 if (err)
6381 return err;
6383 err = open_fileindex(fileindex, &fileindex_path, worktree);
6384 if (err)
6385 goto done;
6387 ok_arg.worktree = worktree;
6388 ok_arg.repo = repo;
6389 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6390 &ok_arg);
6391 if (err)
6392 goto done;
6394 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6395 if (err)
6396 goto done;
6398 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6399 if (err)
6400 goto done;
6402 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6403 worktree);
6404 if (err)
6405 goto done;
6407 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6408 0);
6409 if (err)
6410 goto done;
6412 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6413 if (err)
6414 goto done;
6416 err = got_ref_write(*branch_ref, repo);
6417 if (err)
6418 goto done;
6420 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6421 worktree->base_commit_id);
6422 if (err)
6423 goto done;
6424 err = got_ref_write(base_commit_ref, repo);
6425 if (err)
6426 goto done;
6427 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6428 if (*base_commit_id == NULL) {
6429 err = got_error_from_errno("got_object_id_dup");
6430 goto done;
6433 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6434 worktree->base_commit_id);
6435 if (err)
6436 goto done;
6437 err = got_ref_write(*tmp_branch, repo);
6438 if (err)
6439 goto done;
6441 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6442 if (err)
6443 goto done;
6444 done:
6445 free(fileindex_path);
6446 free(tmp_branch_name);
6447 free(branch_ref_name);
6448 free(base_commit_ref_name);
6449 if (wt_branch)
6450 got_ref_close(wt_branch);
6451 if (err) {
6452 if (*branch_ref) {
6453 got_ref_close(*branch_ref);
6454 *branch_ref = NULL;
6456 if (*tmp_branch) {
6457 got_ref_close(*tmp_branch);
6458 *tmp_branch = NULL;
6460 free(*base_commit_id);
6461 if (*fileindex) {
6462 got_fileindex_free(*fileindex);
6463 *fileindex = NULL;
6465 lock_worktree(worktree, LOCK_SH);
6467 return err;
6470 const struct got_error *
6471 got_worktree_histedit_postpone(struct got_worktree *worktree,
6472 struct got_fileindex *fileindex)
6474 if (fileindex)
6475 got_fileindex_free(fileindex);
6476 return lock_worktree(worktree, LOCK_SH);
6479 const struct got_error *
6480 got_worktree_histedit_in_progress(int *in_progress,
6481 struct got_worktree *worktree)
6483 const struct got_error *err;
6484 char *tmp_branch_name = NULL;
6486 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6487 if (err)
6488 return err;
6490 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6491 free(tmp_branch_name);
6492 return NULL;
6495 const struct got_error *
6496 got_worktree_histedit_continue(struct got_object_id **commit_id,
6497 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6498 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6499 struct got_worktree *worktree, struct got_repository *repo)
6501 const struct got_error *err;
6502 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6503 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6504 struct got_reference *commit_ref = NULL;
6505 struct got_reference *base_commit_ref = NULL;
6506 char *fileindex_path = NULL;
6507 int have_staged_files = 0;
6509 *commit_id = NULL;
6510 *tmp_branch = NULL;
6511 *base_commit_id = NULL;
6512 *fileindex = NULL;
6514 err = lock_worktree(worktree, LOCK_EX);
6515 if (err)
6516 return err;
6518 err = open_fileindex(fileindex, &fileindex_path, worktree);
6519 if (err)
6520 goto done;
6522 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6523 &have_staged_files);
6524 if (err && err->code != GOT_ERR_CANCELLED)
6525 goto done;
6526 if (have_staged_files) {
6527 err = got_error(GOT_ERR_STAGED_PATHS);
6528 goto done;
6531 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6532 if (err)
6533 goto done;
6535 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6536 if (err)
6537 goto done;
6539 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6540 if (err)
6541 goto done;
6543 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6544 worktree);
6545 if (err)
6546 goto done;
6548 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6549 if (err)
6550 goto done;
6552 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6553 if (err)
6554 goto done;
6555 err = got_ref_resolve(commit_id, repo, commit_ref);
6556 if (err)
6557 goto done;
6559 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6560 if (err)
6561 goto done;
6562 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6563 if (err)
6564 goto done;
6566 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6567 if (err)
6568 goto done;
6569 done:
6570 free(commit_ref_name);
6571 free(branch_ref_name);
6572 free(fileindex_path);
6573 if (commit_ref)
6574 got_ref_close(commit_ref);
6575 if (base_commit_ref)
6576 got_ref_close(base_commit_ref);
6577 if (err) {
6578 free(*commit_id);
6579 *commit_id = NULL;
6580 free(*base_commit_id);
6581 *base_commit_id = NULL;
6582 if (*tmp_branch) {
6583 got_ref_close(*tmp_branch);
6584 *tmp_branch = NULL;
6586 if (*fileindex) {
6587 got_fileindex_free(*fileindex);
6588 *fileindex = NULL;
6590 lock_worktree(worktree, LOCK_EX);
6592 return err;
6595 static const struct got_error *
6596 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6598 const struct got_error *err;
6599 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6600 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6602 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6603 if (err)
6604 goto done;
6605 err = delete_ref(tmp_branch_name, repo);
6606 if (err)
6607 goto done;
6609 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6610 worktree);
6611 if (err)
6612 goto done;
6613 err = delete_ref(base_commit_ref_name, repo);
6614 if (err)
6615 goto done;
6617 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6618 if (err)
6619 goto done;
6620 err = delete_ref(branch_ref_name, repo);
6621 if (err)
6622 goto done;
6624 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6625 if (err)
6626 goto done;
6627 err = delete_ref(commit_ref_name, repo);
6628 if (err)
6629 goto done;
6630 done:
6631 free(tmp_branch_name);
6632 free(base_commit_ref_name);
6633 free(branch_ref_name);
6634 free(commit_ref_name);
6635 return err;
6638 const struct got_error *
6639 got_worktree_histedit_abort(struct got_worktree *worktree,
6640 struct got_fileindex *fileindex, struct got_repository *repo,
6641 struct got_reference *branch, struct got_object_id *base_commit_id,
6642 got_worktree_checkout_cb progress_cb, void *progress_arg)
6644 const struct got_error *err, *unlockerr, *sync_err;
6645 struct got_reference *resolved = NULL;
6646 char *fileindex_path = NULL;
6647 struct got_object_id *tree_id = NULL;
6648 struct revert_file_args rfa;
6650 err = lock_worktree(worktree, LOCK_EX);
6651 if (err)
6652 return err;
6654 err = got_ref_open(&resolved, repo,
6655 got_ref_get_symref_target(branch), 0);
6656 if (err)
6657 goto done;
6659 err = got_worktree_set_head_ref(worktree, resolved);
6660 if (err)
6661 goto done;
6663 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6664 if (err)
6665 goto done;
6667 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6668 worktree->path_prefix);
6669 if (err)
6670 goto done;
6672 err = delete_histedit_refs(worktree, repo);
6673 if (err)
6674 goto done;
6676 err = get_fileindex_path(&fileindex_path, worktree);
6677 if (err)
6678 goto done;
6680 rfa.worktree = worktree;
6681 rfa.fileindex = fileindex;
6682 rfa.progress_cb = progress_cb;
6683 rfa.progress_arg = progress_arg;
6684 rfa.patch_cb = NULL;
6685 rfa.patch_arg = NULL;
6686 rfa.repo = repo;
6687 err = worktree_status(worktree, "", fileindex, repo,
6688 revert_file, &rfa, NULL, NULL, 0, 0);
6689 if (err)
6690 goto sync;
6692 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6693 repo, progress_cb, progress_arg, NULL, NULL);
6694 sync:
6695 sync_err = sync_fileindex(fileindex, fileindex_path);
6696 if (sync_err && err == NULL)
6697 err = sync_err;
6698 done:
6699 got_ref_close(resolved);
6700 free(tree_id);
6701 free(fileindex_path);
6703 unlockerr = lock_worktree(worktree, LOCK_SH);
6704 if (unlockerr && err == NULL)
6705 err = unlockerr;
6706 return err;
6709 const struct got_error *
6710 got_worktree_histedit_complete(struct got_worktree *worktree,
6711 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6712 struct got_reference *edited_branch, struct got_repository *repo)
6714 const struct got_error *err, *unlockerr;
6715 struct got_object_id *new_head_commit_id = NULL;
6716 struct got_reference *resolved = NULL;
6718 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6719 if (err)
6720 return err;
6722 err = got_ref_open(&resolved, repo,
6723 got_ref_get_symref_target(edited_branch), 0);
6724 if (err)
6725 goto done;
6727 err = got_ref_change_ref(resolved, new_head_commit_id);
6728 if (err)
6729 goto done;
6731 err = got_ref_write(resolved, repo);
6732 if (err)
6733 goto done;
6735 err = got_worktree_set_head_ref(worktree, resolved);
6736 if (err)
6737 goto done;
6739 err = delete_histedit_refs(worktree, repo);
6740 done:
6741 if (fileindex)
6742 got_fileindex_free(fileindex);
6743 free(new_head_commit_id);
6744 unlockerr = lock_worktree(worktree, LOCK_SH);
6745 if (unlockerr && err == NULL)
6746 err = unlockerr;
6747 return err;
6750 const struct got_error *
6751 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6752 struct got_object_id *commit_id, struct got_repository *repo)
6754 const struct got_error *err;
6755 char *commit_ref_name;
6757 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6758 if (err)
6759 return err;
6761 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6762 if (err)
6763 goto done;
6765 err = delete_ref(commit_ref_name, repo);
6766 done:
6767 free(commit_ref_name);
6768 return err;
6771 const struct got_error *
6772 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6773 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6774 struct got_worktree *worktree, const char *refname,
6775 struct got_repository *repo)
6777 const struct got_error *err = NULL;
6778 char *fileindex_path = NULL;
6779 struct check_rebase_ok_arg ok_arg;
6781 *fileindex = NULL;
6782 *branch_ref = NULL;
6783 *base_branch_ref = NULL;
6785 err = lock_worktree(worktree, LOCK_EX);
6786 if (err)
6787 return err;
6789 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6790 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6791 "cannot integrate a branch into itself; "
6792 "update -b or different branch name required");
6793 goto done;
6796 err = open_fileindex(fileindex, &fileindex_path, worktree);
6797 if (err)
6798 goto done;
6800 /* Preconditions are the same as for rebase. */
6801 ok_arg.worktree = worktree;
6802 ok_arg.repo = repo;
6803 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6804 &ok_arg);
6805 if (err)
6806 goto done;
6808 err = got_ref_open(branch_ref, repo, refname, 1);
6809 if (err)
6810 goto done;
6812 err = got_ref_open(base_branch_ref, repo,
6813 got_worktree_get_head_ref_name(worktree), 1);
6814 done:
6815 if (err) {
6816 if (*branch_ref) {
6817 got_ref_close(*branch_ref);
6818 *branch_ref = NULL;
6820 if (*base_branch_ref) {
6821 got_ref_close(*base_branch_ref);
6822 *base_branch_ref = NULL;
6824 if (*fileindex) {
6825 got_fileindex_free(*fileindex);
6826 *fileindex = NULL;
6828 lock_worktree(worktree, LOCK_SH);
6830 return err;
6833 const struct got_error *
6834 got_worktree_integrate_continue(struct got_worktree *worktree,
6835 struct got_fileindex *fileindex, struct got_repository *repo,
6836 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6837 got_worktree_checkout_cb progress_cb, void *progress_arg,
6838 got_cancel_cb cancel_cb, void *cancel_arg)
6840 const struct got_error *err = NULL, *sync_err, *unlockerr;
6841 char *fileindex_path = NULL;
6842 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6844 err = get_fileindex_path(&fileindex_path, worktree);
6845 if (err)
6846 goto done;
6848 err = got_ref_resolve(&commit_id, repo, branch_ref);
6849 if (err)
6850 goto done;
6852 err = got_object_id_by_path(&tree_id, repo, commit_id,
6853 worktree->path_prefix);
6854 if (err)
6855 goto done;
6857 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6858 if (err)
6859 goto done;
6861 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6862 progress_cb, progress_arg, cancel_cb, cancel_arg);
6863 if (err)
6864 goto sync;
6866 err = got_ref_change_ref(base_branch_ref, commit_id);
6867 if (err)
6868 goto sync;
6870 err = got_ref_write(base_branch_ref, repo);
6871 sync:
6872 sync_err = sync_fileindex(fileindex, fileindex_path);
6873 if (sync_err && err == NULL)
6874 err = sync_err;
6876 done:
6877 unlockerr = got_ref_unlock(branch_ref);
6878 if (unlockerr && err == NULL)
6879 err = unlockerr;
6880 got_ref_close(branch_ref);
6882 unlockerr = got_ref_unlock(base_branch_ref);
6883 if (unlockerr && err == NULL)
6884 err = unlockerr;
6885 got_ref_close(base_branch_ref);
6887 got_fileindex_free(fileindex);
6888 free(fileindex_path);
6889 free(tree_id);
6891 unlockerr = lock_worktree(worktree, LOCK_SH);
6892 if (unlockerr && err == NULL)
6893 err = unlockerr;
6894 return err;
6897 const struct got_error *
6898 got_worktree_integrate_abort(struct got_worktree *worktree,
6899 struct got_fileindex *fileindex, struct got_repository *repo,
6900 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6902 const struct got_error *err = NULL, *unlockerr = NULL;
6904 got_fileindex_free(fileindex);
6906 err = lock_worktree(worktree, LOCK_SH);
6908 unlockerr = got_ref_unlock(branch_ref);
6909 if (unlockerr && err == NULL)
6910 err = unlockerr;
6911 got_ref_close(branch_ref);
6913 unlockerr = got_ref_unlock(base_branch_ref);
6914 if (unlockerr && err == NULL)
6915 err = unlockerr;
6916 got_ref_close(base_branch_ref);
6918 return err;
6921 struct check_stage_ok_arg {
6922 struct got_object_id *head_commit_id;
6923 struct got_worktree *worktree;
6924 struct got_fileindex *fileindex;
6925 struct got_repository *repo;
6926 int have_changes;
6929 const struct got_error *
6930 check_stage_ok(void *arg, unsigned char status,
6931 unsigned char staged_status, const char *relpath,
6932 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6933 struct got_object_id *commit_id, int dirfd, const char *de_name)
6935 struct check_stage_ok_arg *a = arg;
6936 const struct got_error *err = NULL;
6937 struct got_fileindex_entry *ie;
6938 struct got_object_id base_commit_id;
6939 struct got_object_id *base_commit_idp = NULL;
6940 char *in_repo_path = NULL, *p;
6942 if (status == GOT_STATUS_UNVERSIONED ||
6943 status == GOT_STATUS_NO_CHANGE)
6944 return NULL;
6945 if (status == GOT_STATUS_NONEXISTENT)
6946 return got_error_set_errno(ENOENT, relpath);
6948 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6949 if (ie == NULL)
6950 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6952 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6953 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6954 relpath) == -1)
6955 return got_error_from_errno("asprintf");
6957 if (got_fileindex_entry_has_commit(ie)) {
6958 memcpy(base_commit_id.sha1, ie->commit_sha1,
6959 SHA1_DIGEST_LENGTH);
6960 base_commit_idp = &base_commit_id;
6963 if (status == GOT_STATUS_CONFLICT) {
6964 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6965 goto done;
6966 } else if (status != GOT_STATUS_ADD &&
6967 status != GOT_STATUS_MODIFY &&
6968 status != GOT_STATUS_DELETE) {
6969 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6970 goto done;
6973 a->have_changes = 1;
6975 p = in_repo_path;
6976 while (p[0] == '/')
6977 p++;
6978 err = check_out_of_date(p, status, staged_status,
6979 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6980 GOT_ERR_STAGE_OUT_OF_DATE);
6981 done:
6982 free(in_repo_path);
6983 return err;
6986 struct stage_path_arg {
6987 struct got_worktree *worktree;
6988 struct got_fileindex *fileindex;
6989 struct got_repository *repo;
6990 got_worktree_status_cb status_cb;
6991 void *status_arg;
6992 got_worktree_patch_cb patch_cb;
6993 void *patch_arg;
6994 int staged_something;
6995 int allow_bad_symlinks;
6998 static const struct got_error *
6999 stage_path(void *arg, unsigned char status,
7000 unsigned char staged_status, const char *relpath,
7001 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7002 struct got_object_id *commit_id, int dirfd, const char *de_name)
7004 struct stage_path_arg *a = arg;
7005 const struct got_error *err = NULL;
7006 struct got_fileindex_entry *ie;
7007 char *ondisk_path = NULL, *path_content = NULL;
7008 uint32_t stage;
7009 struct got_object_id *new_staged_blob_id = NULL;
7010 struct stat sb;
7012 if (status == GOT_STATUS_UNVERSIONED)
7013 return NULL;
7015 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7016 if (ie == NULL)
7017 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7019 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7020 relpath)== -1)
7021 return got_error_from_errno("asprintf");
7023 switch (status) {
7024 case GOT_STATUS_ADD:
7025 case GOT_STATUS_MODIFY:
7026 /* XXX could sb.st_mode be passed in by our caller? */
7027 if (lstat(ondisk_path, &sb) == -1) {
7028 err = got_error_from_errno2("lstat", ondisk_path);
7029 break;
7031 if (a->patch_cb) {
7032 if (status == GOT_STATUS_ADD) {
7033 int choice = GOT_PATCH_CHOICE_NONE;
7034 err = (*a->patch_cb)(&choice, a->patch_arg,
7035 status, ie->path, NULL, 1, 1);
7036 if (err)
7037 break;
7038 if (choice != GOT_PATCH_CHOICE_YES)
7039 break;
7040 } else {
7041 err = create_patched_content(&path_content, 0,
7042 staged_blob_id ? staged_blob_id : blob_id,
7043 ondisk_path, dirfd, de_name, ie->path,
7044 a->repo, a->patch_cb, a->patch_arg);
7045 if (err || path_content == NULL)
7046 break;
7049 err = got_object_blob_create(&new_staged_blob_id,
7050 path_content ? path_content : ondisk_path, a->repo);
7051 if (err)
7052 break;
7053 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7054 SHA1_DIGEST_LENGTH);
7055 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7056 stage = GOT_FILEIDX_STAGE_ADD;
7057 else
7058 stage = GOT_FILEIDX_STAGE_MODIFY;
7059 got_fileindex_entry_stage_set(ie, stage);
7060 if (S_ISLNK(sb.st_mode)) {
7061 int is_bad_symlink = 0;
7062 if (!a->allow_bad_symlinks) {
7063 char target_path[PATH_MAX];
7064 ssize_t target_len;
7065 target_len = readlink(ondisk_path, target_path,
7066 sizeof(target_path));
7067 if (target_len == -1) {
7068 err = got_error_from_errno2("readlink",
7069 ondisk_path);
7070 break;
7072 err = is_bad_symlink_target(&is_bad_symlink,
7073 target_path, target_len, ondisk_path,
7074 a->worktree->root_path);
7075 if (err)
7076 break;
7077 if (is_bad_symlink) {
7078 err = got_error_path(ondisk_path,
7079 GOT_ERR_BAD_SYMLINK);
7080 break;
7083 if (is_bad_symlink)
7084 got_fileindex_entry_staged_filetype_set(ie,
7085 GOT_FILEIDX_MODE_BAD_SYMLINK);
7086 else
7087 got_fileindex_entry_staged_filetype_set(ie,
7088 GOT_FILEIDX_MODE_SYMLINK);
7089 } else {
7090 got_fileindex_entry_staged_filetype_set(ie,
7091 GOT_FILEIDX_MODE_REGULAR_FILE);
7093 a->staged_something = 1;
7094 if (a->status_cb == NULL)
7095 break;
7096 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7097 get_staged_status(ie), relpath, blob_id,
7098 new_staged_blob_id, NULL, dirfd, de_name);
7099 break;
7100 case GOT_STATUS_DELETE:
7101 if (staged_status == GOT_STATUS_DELETE)
7102 break;
7103 if (a->patch_cb) {
7104 int choice = GOT_PATCH_CHOICE_NONE;
7105 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7106 ie->path, NULL, 1, 1);
7107 if (err)
7108 break;
7109 if (choice == GOT_PATCH_CHOICE_NO)
7110 break;
7111 if (choice != GOT_PATCH_CHOICE_YES) {
7112 err = got_error(GOT_ERR_PATCH_CHOICE);
7113 break;
7116 stage = GOT_FILEIDX_STAGE_DELETE;
7117 got_fileindex_entry_stage_set(ie, stage);
7118 a->staged_something = 1;
7119 if (a->status_cb == NULL)
7120 break;
7121 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7122 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7123 de_name);
7124 break;
7125 case GOT_STATUS_NO_CHANGE:
7126 break;
7127 case GOT_STATUS_CONFLICT:
7128 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7129 break;
7130 case GOT_STATUS_NONEXISTENT:
7131 err = got_error_set_errno(ENOENT, relpath);
7132 break;
7133 default:
7134 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7135 break;
7138 if (path_content && unlink(path_content) == -1 && err == NULL)
7139 err = got_error_from_errno2("unlink", path_content);
7140 free(path_content);
7141 free(ondisk_path);
7142 free(new_staged_blob_id);
7143 return err;
7146 const struct got_error *
7147 got_worktree_stage(struct got_worktree *worktree,
7148 struct got_pathlist_head *paths,
7149 got_worktree_status_cb status_cb, void *status_arg,
7150 got_worktree_patch_cb patch_cb, void *patch_arg,
7151 int allow_bad_symlinks, struct got_repository *repo)
7153 const struct got_error *err = NULL, *sync_err, *unlockerr;
7154 struct got_pathlist_entry *pe;
7155 struct got_fileindex *fileindex = NULL;
7156 char *fileindex_path = NULL;
7157 struct got_reference *head_ref = NULL;
7158 struct got_object_id *head_commit_id = NULL;
7159 struct check_stage_ok_arg oka;
7160 struct stage_path_arg spa;
7162 err = lock_worktree(worktree, LOCK_EX);
7163 if (err)
7164 return err;
7166 err = got_ref_open(&head_ref, repo,
7167 got_worktree_get_head_ref_name(worktree), 0);
7168 if (err)
7169 goto done;
7170 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7171 if (err)
7172 goto done;
7173 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7174 if (err)
7175 goto done;
7177 /* Check pre-conditions before staging anything. */
7178 oka.head_commit_id = head_commit_id;
7179 oka.worktree = worktree;
7180 oka.fileindex = fileindex;
7181 oka.repo = repo;
7182 oka.have_changes = 0;
7183 TAILQ_FOREACH(pe, paths, entry) {
7184 err = worktree_status(worktree, pe->path, fileindex, repo,
7185 check_stage_ok, &oka, NULL, NULL, 0, 0);
7186 if (err)
7187 goto done;
7189 if (!oka.have_changes) {
7190 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7191 goto done;
7194 spa.worktree = worktree;
7195 spa.fileindex = fileindex;
7196 spa.repo = repo;
7197 spa.patch_cb = patch_cb;
7198 spa.patch_arg = patch_arg;
7199 spa.status_cb = status_cb;
7200 spa.status_arg = status_arg;
7201 spa.staged_something = 0;
7202 spa.allow_bad_symlinks = allow_bad_symlinks;
7203 TAILQ_FOREACH(pe, paths, entry) {
7204 err = worktree_status(worktree, pe->path, fileindex, repo,
7205 stage_path, &spa, NULL, NULL, 0, 0);
7206 if (err)
7207 goto done;
7209 if (!spa.staged_something) {
7210 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7211 goto done;
7214 sync_err = sync_fileindex(fileindex, fileindex_path);
7215 if (sync_err && err == NULL)
7216 err = sync_err;
7217 done:
7218 if (head_ref)
7219 got_ref_close(head_ref);
7220 free(head_commit_id);
7221 free(fileindex_path);
7222 if (fileindex)
7223 got_fileindex_free(fileindex);
7224 unlockerr = lock_worktree(worktree, LOCK_SH);
7225 if (unlockerr && err == NULL)
7226 err = unlockerr;
7227 return err;
7230 struct unstage_path_arg {
7231 struct got_worktree *worktree;
7232 struct got_fileindex *fileindex;
7233 struct got_repository *repo;
7234 got_worktree_checkout_cb progress_cb;
7235 void *progress_arg;
7236 got_worktree_patch_cb patch_cb;
7237 void *patch_arg;
7240 static const struct got_error *
7241 create_unstaged_content(char **path_unstaged_content,
7242 char **path_new_staged_content, struct got_object_id *blob_id,
7243 struct got_object_id *staged_blob_id, const char *relpath,
7244 struct got_repository *repo,
7245 got_worktree_patch_cb patch_cb, void *patch_arg)
7247 const struct got_error *err;
7248 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7249 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7250 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7251 struct stat sb1, sb2;
7252 struct got_diff_changes *changes = NULL;
7253 struct got_diff_state *ds = NULL;
7254 struct got_diff_args *args = NULL;
7255 struct got_diff_change *change;
7256 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7257 int have_content = 0, have_rejected_content = 0;
7259 *path_unstaged_content = NULL;
7260 *path_new_staged_content = NULL;
7262 err = got_object_id_str(&label1, blob_id);
7263 if (err)
7264 return err;
7265 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7266 if (err)
7267 goto done;
7269 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7270 if (err)
7271 goto done;
7273 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7274 if (err)
7275 goto done;
7277 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7278 if (err)
7279 goto done;
7281 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7282 if (err)
7283 goto done;
7285 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7286 if (err)
7287 goto done;
7289 if (stat(path1, &sb1) == -1) {
7290 err = got_error_from_errno2("stat", path1);
7291 goto done;
7294 if (stat(path2, &sb2) == -1) {
7295 err = got_error_from_errno2("stat", path2);
7296 goto done;
7299 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7300 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7301 if (err)
7302 goto done;
7304 err = got_opentemp_named(path_unstaged_content, &outfile,
7305 "got-unstaged-content");
7306 if (err)
7307 goto done;
7308 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7309 "got-new-staged-content");
7310 if (err)
7311 goto done;
7313 if (fseek(f1, 0L, SEEK_SET) == -1) {
7314 err = got_ferror(f1, GOT_ERR_IO);
7315 goto done;
7317 if (fseek(f2, 0L, SEEK_SET) == -1) {
7318 err = got_ferror(f2, GOT_ERR_IO);
7319 goto done;
7321 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7322 int choice;
7323 err = apply_or_reject_change(&choice, change, ++n,
7324 changes->nchanges, ds, args, diff_flags, relpath,
7325 f1, f2, &line_cur1, &line_cur2,
7326 outfile, rejectfile, patch_cb, patch_arg);
7327 if (err)
7328 goto done;
7329 if (choice == GOT_PATCH_CHOICE_YES)
7330 have_content = 1;
7331 else
7332 have_rejected_content = 1;
7333 if (choice == GOT_PATCH_CHOICE_QUIT)
7334 break;
7336 if (have_content || have_rejected_content)
7337 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7338 outfile, rejectfile);
7339 done:
7340 free(label1);
7341 if (blob)
7342 got_object_blob_close(blob);
7343 if (staged_blob)
7344 got_object_blob_close(staged_blob);
7345 if (f1 && fclose(f1) == EOF && err == NULL)
7346 err = got_error_from_errno2("fclose", path1);
7347 if (f2 && fclose(f2) == EOF && err == NULL)
7348 err = got_error_from_errno2("fclose", path2);
7349 if (outfile && fclose(outfile) == EOF && err == NULL)
7350 err = got_error_from_errno2("fclose", *path_unstaged_content);
7351 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7352 err = got_error_from_errno2("fclose", *path_new_staged_content);
7353 if (path1 && unlink(path1) == -1 && err == NULL)
7354 err = got_error_from_errno2("unlink", path1);
7355 if (path2 && unlink(path2) == -1 && err == NULL)
7356 err = got_error_from_errno2("unlink", path2);
7357 if (err || !have_content) {
7358 if (*path_unstaged_content &&
7359 unlink(*path_unstaged_content) == -1 && err == NULL)
7360 err = got_error_from_errno2("unlink",
7361 *path_unstaged_content);
7362 free(*path_unstaged_content);
7363 *path_unstaged_content = NULL;
7365 if (err || !have_rejected_content) {
7366 if (*path_new_staged_content &&
7367 unlink(*path_new_staged_content) == -1 && err == NULL)
7368 err = got_error_from_errno2("unlink",
7369 *path_new_staged_content);
7370 free(*path_new_staged_content);
7371 *path_new_staged_content = NULL;
7373 free(args);
7374 if (ds) {
7375 got_diff_state_free(ds);
7376 free(ds);
7378 if (changes)
7379 got_diff_free_changes(changes);
7380 free(path1);
7381 free(path2);
7382 return err;
7385 static const struct got_error *
7386 unstage_path(void *arg, unsigned char status,
7387 unsigned char staged_status, const char *relpath,
7388 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7389 struct got_object_id *commit_id, int dirfd, const char *de_name)
7391 const struct got_error *err = NULL;
7392 struct unstage_path_arg *a = arg;
7393 struct got_fileindex_entry *ie;
7394 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7395 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7396 char *path_new_staged_content = NULL;
7397 char *id_str = NULL, *label_orig = NULL;
7398 int local_changes_subsumed;
7399 struct stat sb;
7401 if (staged_status != GOT_STATUS_ADD &&
7402 staged_status != GOT_STATUS_MODIFY &&
7403 staged_status != GOT_STATUS_DELETE)
7404 return NULL;
7406 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7407 if (ie == NULL)
7408 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7410 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7411 == -1)
7412 return got_error_from_errno("asprintf");
7414 err = got_object_id_str(&id_str,
7415 commit_id ? commit_id : a->worktree->base_commit_id);
7416 if (err)
7417 goto done;
7418 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7419 id_str) == -1) {
7420 err = got_error_from_errno("asprintf");
7421 goto done;
7424 switch (staged_status) {
7425 case GOT_STATUS_MODIFY:
7426 err = got_object_open_as_blob(&blob_base, a->repo,
7427 blob_id, 8192);
7428 if (err)
7429 break;
7430 /* fall through */
7431 case GOT_STATUS_ADD:
7432 if (a->patch_cb) {
7433 if (staged_status == GOT_STATUS_ADD) {
7434 int choice = GOT_PATCH_CHOICE_NONE;
7435 err = (*a->patch_cb)(&choice, a->patch_arg,
7436 staged_status, ie->path, NULL, 1, 1);
7437 if (err)
7438 break;
7439 if (choice != GOT_PATCH_CHOICE_YES)
7440 break;
7441 } else {
7442 err = create_unstaged_content(
7443 &path_unstaged_content,
7444 &path_new_staged_content, blob_id,
7445 staged_blob_id, ie->path, a->repo,
7446 a->patch_cb, a->patch_arg);
7447 if (err || path_unstaged_content == NULL)
7448 break;
7449 if (path_new_staged_content) {
7450 err = got_object_blob_create(
7451 &staged_blob_id,
7452 path_new_staged_content,
7453 a->repo);
7454 if (err)
7455 break;
7456 memcpy(ie->staged_blob_sha1,
7457 staged_blob_id->sha1,
7458 SHA1_DIGEST_LENGTH);
7460 err = merge_file(&local_changes_subsumed,
7461 a->worktree, blob_base, ondisk_path,
7462 relpath, got_fileindex_perms_to_st(ie),
7463 path_unstaged_content, label_orig,
7464 "unstaged", a->repo, a->progress_cb,
7465 a->progress_arg);
7466 if (err == NULL &&
7467 path_new_staged_content == NULL)
7468 got_fileindex_entry_stage_set(ie,
7469 GOT_FILEIDX_STAGE_NONE);
7470 break; /* Done with this file. */
7473 err = got_object_open_as_blob(&blob_staged, a->repo,
7474 staged_blob_id, 8192);
7475 if (err)
7476 break;
7477 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7478 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7479 case GOT_FILEIDX_MODE_REGULAR_FILE:
7480 err = merge_blob(&local_changes_subsumed, a->worktree,
7481 blob_base, ondisk_path, relpath,
7482 got_fileindex_perms_to_st(ie), label_orig,
7483 blob_staged, commit_id ? commit_id :
7484 a->worktree->base_commit_id, a->repo,
7485 a->progress_cb, a->progress_arg);
7486 break;
7487 case GOT_FILEIDX_MODE_SYMLINK:
7488 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7489 err = merge_symlink(a->worktree, blob_base,
7490 ondisk_path, relpath, label_orig,
7491 blob_staged, commit_id ? commit_id :
7492 a->worktree->base_commit_id,
7493 a->repo, a->progress_cb, a->progress_arg);
7494 } else {
7495 err = merge_blob(&local_changes_subsumed,
7496 a->worktree, blob_base, ondisk_path,
7497 relpath, got_fileindex_perms_to_st(ie),
7498 label_orig, blob_staged,
7499 commit_id ? commit_id :
7500 a->worktree->base_commit_id, a->repo,
7501 a->progress_cb, a->progress_arg);
7503 break;
7504 default:
7505 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7506 break;
7508 if (err == NULL)
7509 got_fileindex_entry_stage_set(ie,
7510 GOT_FILEIDX_STAGE_NONE);
7511 break;
7512 case GOT_STATUS_DELETE:
7513 if (a->patch_cb) {
7514 int choice = GOT_PATCH_CHOICE_NONE;
7515 err = (*a->patch_cb)(&choice, a->patch_arg,
7516 staged_status, ie->path, NULL, 1, 1);
7517 if (err)
7518 break;
7519 if (choice == GOT_PATCH_CHOICE_NO)
7520 break;
7521 if (choice != GOT_PATCH_CHOICE_YES) {
7522 err = got_error(GOT_ERR_PATCH_CHOICE);
7523 break;
7526 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7527 err = get_file_status(&status, &sb, ie, ondisk_path,
7528 dirfd, de_name, a->repo);
7529 if (err)
7530 break;
7531 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7532 break;
7534 done:
7535 free(ondisk_path);
7536 if (path_unstaged_content &&
7537 unlink(path_unstaged_content) == -1 && err == NULL)
7538 err = got_error_from_errno2("unlink", path_unstaged_content);
7539 if (path_new_staged_content &&
7540 unlink(path_new_staged_content) == -1 && err == NULL)
7541 err = got_error_from_errno2("unlink", path_new_staged_content);
7542 free(path_unstaged_content);
7543 free(path_new_staged_content);
7544 if (blob_base)
7545 got_object_blob_close(blob_base);
7546 if (blob_staged)
7547 got_object_blob_close(blob_staged);
7548 free(id_str);
7549 free(label_orig);
7550 return err;
7553 const struct got_error *
7554 got_worktree_unstage(struct got_worktree *worktree,
7555 struct got_pathlist_head *paths,
7556 got_worktree_checkout_cb progress_cb, void *progress_arg,
7557 got_worktree_patch_cb patch_cb, void *patch_arg,
7558 struct got_repository *repo)
7560 const struct got_error *err = NULL, *sync_err, *unlockerr;
7561 struct got_pathlist_entry *pe;
7562 struct got_fileindex *fileindex = NULL;
7563 char *fileindex_path = NULL;
7564 struct unstage_path_arg upa;
7566 err = lock_worktree(worktree, LOCK_EX);
7567 if (err)
7568 return err;
7570 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7571 if (err)
7572 goto done;
7574 upa.worktree = worktree;
7575 upa.fileindex = fileindex;
7576 upa.repo = repo;
7577 upa.progress_cb = progress_cb;
7578 upa.progress_arg = progress_arg;
7579 upa.patch_cb = patch_cb;
7580 upa.patch_arg = patch_arg;
7581 TAILQ_FOREACH(pe, paths, entry) {
7582 err = worktree_status(worktree, pe->path, fileindex, repo,
7583 unstage_path, &upa, NULL, NULL, 0, 0);
7584 if (err)
7585 goto done;
7588 sync_err = sync_fileindex(fileindex, fileindex_path);
7589 if (sync_err && err == NULL)
7590 err = sync_err;
7591 done:
7592 free(fileindex_path);
7593 if (fileindex)
7594 got_fileindex_free(fileindex);
7595 unlockerr = lock_worktree(worktree, LOCK_SH);
7596 if (unlockerr && err == NULL)
7597 err = unlockerr;
7598 return err;