Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
745 char *symlink_path = NULL;
746 FILE *symlinkf = NULL;
748 *local_changes_subsumed = 0;
750 parent = dirname(ondisk_path);
751 if (parent == NULL)
752 return got_error_from_errno2("dirname", ondisk_path);
754 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 return got_error_from_errno("asprintf");
757 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
758 if (err)
759 goto done;
761 free(base_path);
762 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 err = got_error_from_errno("asprintf");
764 base_path = NULL;
765 goto done;
768 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
769 if (err)
770 goto done;
771 if (blob_orig) {
772 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
773 blob_orig);
774 if (err)
775 goto done;
776 } else {
777 /*
778 * If the file has no blob, this is an "add vs add" conflict,
779 * and we simply use an empty ancestor file to make both files
780 * appear in the merged result in their entirety.
781 */
784 /*
785 * In order the run a 3-way merge with a symlink we copy the symlink's
786 * target path into a temporary file and use that file with diff3.
787 */
788 if (S_ISLNK(st_mode)) {
789 char target_path[PATH_MAX];
790 ssize_t target_len;
791 size_t n;
793 free(base_path);
794 if (asprintf(&base_path, "%s/got-symlink-merge",
795 parent) == -1) {
796 err = got_error_from_errno("asprintf");
797 base_path = NULL;
798 goto done;
800 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
801 if (err)
802 goto done;
803 target_len = readlink(ondisk_path, target_path,
804 sizeof(target_path));
805 if (target_len == -1) {
806 err = got_error_from_errno2("readlink", ondisk_path);
807 goto done;
809 n = fwrite(target_path, 1, target_len, symlinkf);
810 if (n != target_len) {
811 err = got_ferror(symlinkf, GOT_ERR_IO);
812 goto done;
814 if (fflush(symlinkf) == EOF) {
815 err = got_error_from_errno2("fflush", symlink_path);
816 goto done;
820 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 label_deriv, label_orig, NULL);
823 if (err)
824 goto done;
826 err = (*progress_cb)(progress_arg,
827 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
828 if (err)
829 goto done;
831 if (fsync(merged_fd) != 0) {
832 err = got_error_from_errno("fsync");
833 goto done;
836 /* Check if a clean merge has subsumed all local changes. */
837 if (overlapcnt == 0) {
838 err = check_files_equal(local_changes_subsumed, deriv_path,
839 merged_path);
840 if (err)
841 goto done;
844 if (fchmod(merged_fd, st_mode) != 0) {
845 err = got_error_from_errno2("fchmod", merged_path);
846 goto done;
849 if (rename(merged_path, ondisk_path) != 0) {
850 err = got_error_from_errno3("rename", merged_path,
851 ondisk_path);
852 goto done;
854 done:
855 if (err) {
856 if (merged_path)
857 unlink(merged_path);
859 if (symlink_path) {
860 if (unlink(symlink_path) == -1 && err == NULL)
861 err = got_error_from_errno2("unlink", symlink_path);
863 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 err = got_error_from_errno2("fclose", symlink_path);
865 free(symlink_path);
866 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 err = got_error_from_errno("close");
868 if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 err = got_error_from_errno("fclose");
870 free(merged_path);
871 free(base_path);
872 if (blob_orig_path) {
873 unlink(blob_orig_path);
874 free(blob_orig_path);
876 return err;
879 static const struct got_error *
880 update_symlink(const char *ondisk_path, const char *target_path,
881 size_t target_len)
883 /* This is not atomic but matches what 'ln -sf' does. */
884 if (unlink(ondisk_path) == -1)
885 return got_error_from_errno2("unlink", ondisk_path);
886 if (symlink(target_path, ondisk_path) == -1)
887 return got_error_from_errno3("symlink", target_path,
888 ondisk_path);
889 return NULL;
892 /*
893 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 * in the work tree with a file that contains conflict markers and the
895 * conflicting target paths of the original version, a "derived version"
896 * of a symlink from an incoming change, and a local version of the symlink.
898 * The original versions's target path can be NULL if it is not available,
899 * such as if both derived versions added a new symlink at the same path.
901 * The incoming derived symlink target is NULL in case the incoming change
902 * has deleted this symlink.
903 */
904 static const struct got_error *
905 install_symlink_conflict(const char *deriv_target,
906 struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 const char *label_orig, const char *local_target, const char *ondisk_path)
909 const struct got_error *err;
910 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
911 FILE *f = NULL;
913 err = got_object_id_str(&id_str, deriv_base_commit_id);
914 if (err)
915 return got_error_from_errno("asprintf");
917 if (asprintf(&label_deriv, "%s: commit %s",
918 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 err = got_error_from_errno("asprintf");
920 goto done;
923 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
924 if (err)
925 goto done;
927 if (fprintf(f, "%s: Could not install symbolic link because of merge "
928 "conflict.\nln(1) may be used to fix the situation. If this is "
929 "intended to be a\nregular file instead then its expected "
930 "contents may be filled in.\nThe following conflicting symlink "
931 "target paths were found:\n"
932 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
933 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
934 deriv_target ? deriv_target : "(symlink was deleted)",
935 orig_target ? label_orig : "",
936 orig_target ? "\n" : "",
937 orig_target ? orig_target : "",
938 orig_target ? "\n" : "",
939 GOT_DIFF_CONFLICT_MARKER_SEP,
940 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
941 err = got_error_from_errno2("fprintf", path);
942 goto done;
945 if (unlink(ondisk_path) == -1) {
946 err = got_error_from_errno2("unlink", ondisk_path);
947 goto done;
949 if (rename(path, ondisk_path) == -1) {
950 err = got_error_from_errno3("rename", path, ondisk_path);
951 goto done;
953 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
954 err = got_error_from_errno2("chmod", ondisk_path);
955 goto done;
957 done:
958 if (f != NULL && fclose(f) == EOF && err == NULL)
959 err = got_error_from_errno2("fclose", path);
960 free(path);
961 free(id_str);
962 free(label_deriv);
963 return err;
966 /* forward declaration */
967 static const struct got_error *
968 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
969 const char *, const char *, uint16_t, const char *,
970 struct got_blob_object *, struct got_object_id *,
971 struct got_repository *, got_worktree_checkout_cb, void *);
973 /*
974 * Merge a symlink into the work tree, where blob_orig acts as the common
975 * ancestor, blob_deriv acts as the first derived version, and the symlink
976 * on disk acts as the second derived version.
977 * Assume that contents of both blobs represent symlinks.
978 */
979 static const struct got_error *
980 merge_symlink(struct got_worktree *worktree,
981 struct got_blob_object *blob_orig, const char *ondisk_path,
982 const char *path, uint16_t st_mode, const char *label_orig,
983 struct got_blob_object *blob_deriv,
984 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
985 got_worktree_checkout_cb progress_cb, void *progress_arg)
987 const struct got_error *err = NULL;
988 char *ancestor_target = NULL, *deriv_target = NULL;
989 struct stat sb;
990 ssize_t ondisk_len, deriv_len;
991 char ondisk_target[PATH_MAX];
992 int have_local_change = 0;
993 int have_incoming_change = 0;
995 if (lstat(ondisk_path, &sb) == -1)
996 return got_error_from_errno2("lstat", ondisk_path);
998 if (!S_ISLNK(sb.st_mode)) {
999 /*
1000 * If there is a regular file on disk, merge the symlink
1001 * target path into this file, which will usually cause
1002 * a merge conflict.
1004 if (S_ISREG(sb.st_mode)) {
1005 int local_changes_subsumed;
1006 return merge_blob(&local_changes_subsumed, worktree,
1007 NULL, ondisk_path, path, sb.st_mode, label_orig,
1008 blob_deriv, deriv_base_commit_id,
1009 repo, progress_cb, progress_arg);
1012 /* TODO symlink is obstructed; do something */
1013 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
1016 ondisk_len = readlink(ondisk_path, ondisk_target,
1017 sizeof(ondisk_target));
1018 if (ondisk_len == -1) {
1019 err = got_error_from_errno2("readlink",
1020 ondisk_path);
1021 goto done;
1023 ondisk_target[ondisk_len] = '\0';
1025 if (blob_orig) {
1026 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1027 if (err)
1028 goto done;
1031 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
1032 if (err)
1033 goto done;
1035 if (ancestor_target == NULL ||
1036 (ondisk_len != strlen(ancestor_target) ||
1037 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1038 have_local_change = 1;
1040 deriv_len = strlen(deriv_target);
1041 if (ancestor_target == NULL ||
1042 (deriv_len != strlen(ancestor_target) ||
1043 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1044 have_incoming_change = 1;
1046 if (!have_local_change && !have_incoming_change) {
1047 if (ancestor_target) {
1048 /* Both sides made the same change. */
1049 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1050 path);
1051 } else if (deriv_len == ondisk_len &&
1052 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1053 /* Both sides added the same symlink. */
1054 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1055 path);
1056 } else {
1057 /* Both sides added symlinks which don't match. */
1058 err = install_symlink_conflict(deriv_target,
1059 deriv_base_commit_id, ancestor_target,
1060 label_orig, ondisk_target, ondisk_path);
1061 if (err)
1062 goto done;
1063 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1064 path);
1066 } else if (!have_local_change && have_incoming_change) {
1067 /* Apply the incoming change. */
1068 err = update_symlink(ondisk_path, deriv_target,
1069 strlen(deriv_target));
1070 if (err)
1071 goto done;
1072 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1073 } else if (have_local_change && have_incoming_change) {
1074 err = install_symlink_conflict(deriv_target,
1075 deriv_base_commit_id, ancestor_target, label_orig,
1076 ondisk_target, ondisk_path);
1077 if (err)
1078 goto done;
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1080 path);
1083 done:
1084 free(ancestor_target);
1085 free(deriv_target);
1086 return err;
1090 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1091 * blob_deriv acts as the first derived version, and the file on disk
1092 * acts as the second derived version.
1094 static const struct got_error *
1095 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1096 struct got_blob_object *blob_orig, const char *ondisk_path,
1097 const char *path, uint16_t st_mode, const char *label_orig,
1098 struct got_blob_object *blob_deriv,
1099 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1100 got_worktree_checkout_cb progress_cb, void *progress_arg)
1102 const struct got_error *err = NULL;
1103 FILE *f_deriv = NULL;
1104 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1105 char *label_deriv = NULL, *parent;
1107 *local_changes_subsumed = 0;
1109 parent = dirname(ondisk_path);
1110 if (parent == NULL)
1111 return got_error_from_errno2("dirname", ondisk_path);
1113 free(base_path);
1114 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1115 err = got_error_from_errno("asprintf");
1116 base_path = NULL;
1117 goto done;
1120 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1121 if (err)
1122 goto done;
1123 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1124 blob_deriv);
1125 if (err)
1126 goto done;
1128 err = got_object_id_str(&id_str, deriv_base_commit_id);
1129 if (err)
1130 goto done;
1131 if (asprintf(&label_deriv, "%s: commit %s",
1132 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1133 err = got_error_from_errno("asprintf");
1134 goto done;
1137 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1138 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1139 label_deriv, repo, progress_cb, progress_arg);
1140 done:
1141 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1142 err = got_error_from_errno("fclose");
1143 free(base_path);
1144 if (blob_deriv_path) {
1145 unlink(blob_deriv_path);
1146 free(blob_deriv_path);
1148 free(id_str);
1149 free(label_deriv);
1150 return err;
1153 static const struct got_error *
1154 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1155 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1156 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1158 const struct got_error *err = NULL;
1159 struct got_fileindex_entry *new_ie;
1161 *new_iep = NULL;
1163 err = got_fileindex_entry_alloc(&new_ie, path);
1164 if (err)
1165 return err;
1167 err = got_fileindex_entry_update(new_ie, ondisk_path,
1168 blob_id->sha1, base_commit_id->sha1, 1);
1169 if (err)
1170 goto done;
1172 err = got_fileindex_entry_add(fileindex, new_ie);
1173 done:
1174 if (err)
1175 got_fileindex_entry_free(new_ie);
1176 else
1177 *new_iep = new_ie;
1178 return err;
1181 static mode_t
1182 get_ondisk_perms(int executable, mode_t st_mode)
1184 mode_t xbits = S_IXUSR;
1186 if (executable) {
1187 /* Map read bits to execute bits. */
1188 if (st_mode & S_IRGRP)
1189 xbits |= S_IXGRP;
1190 if (st_mode & S_IROTH)
1191 xbits |= S_IXOTH;
1192 return st_mode | xbits;
1195 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1198 /* forward declaration */
1199 static const struct got_error *
1200 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1201 const char *path, mode_t te_mode, mode_t st_mode,
1202 struct got_blob_object *blob, int restoring_missing_file,
1203 int reverting_versioned_file, int installing_bad_symlink,
1204 int path_is_unversioned, struct got_repository *repo,
1205 got_worktree_checkout_cb progress_cb, void *progress_arg);
1208 * This function assumes that the provided symlink target points at a
1209 * safe location in the work tree!
1211 static const struct got_error *
1212 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1213 size_t target_len)
1215 const struct got_error *err = NULL;
1216 ssize_t elen;
1217 char etarget[PATH_MAX];
1218 int fd;
1221 * "Bad" symlinks (those pointing outside the work tree or into the
1222 * .got directory) are installed in the work tree as a regular file
1223 * which contains the bad symlink target path.
1224 * The new symlink target has already been checked for safety by our
1225 * caller. If we can successfully open a regular file then we simply
1226 * replace this file with a symlink below.
1228 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1229 if (fd == -1) {
1230 if (errno != ELOOP)
1231 return got_error_from_errno2("open", ondisk_path);
1233 /* We are updating an existing on-disk symlink. */
1234 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1235 if (elen == -1)
1236 return got_error_from_errno2("readlink", ondisk_path);
1238 if (elen == target_len &&
1239 memcmp(etarget, target_path, target_len) == 0)
1240 return NULL; /* nothing to do */
1243 err = update_symlink(ondisk_path, target_path, target_len);
1244 if (fd != -1 && close(fd) == -1 && err == NULL)
1245 err = got_error_from_errno2("close", ondisk_path);
1246 return err;
1249 static const struct got_error *
1250 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1251 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1252 int restoring_missing_file, int reverting_versioned_file,
1253 int path_is_unversioned, struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *resolved_path = NULL, *abspath = NULL;
1260 char *path_got = NULL;
1261 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1262 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1264 *is_bad_symlink = 0;
1267 * Blob object content specifies the target path of the link.
1268 * If a symbolic link cannot be installed we instead create
1269 * a regular file which contains the link target path stored
1270 * in the blob object.
1272 do {
1273 err = got_object_blob_read_block(&len, blob);
1274 if (len + target_len >= sizeof(target_path)) {
1275 /* Path too long; install as a regular file. */
1276 *is_bad_symlink = 1;
1277 got_object_blob_rewind(blob);
1278 return install_blob(worktree, ondisk_path, path,
1279 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1280 restoring_missing_file, reverting_versioned_file,
1281 1, path_is_unversioned, repo, progress_cb,
1282 progress_arg);
1284 if (len > 0) {
1285 /* Skip blob object header first time around. */
1286 memcpy(target_path + target_len, buf + hdrlen,
1287 len - hdrlen);
1288 target_len += len - hdrlen;
1289 hdrlen = 0;
1291 } while (len != 0);
1292 target_path[target_len] = '\0';
1295 * Relative symlink target lookup should begin at the directory
1296 * in which the blob object is being installed.
1298 if (!got_path_is_absolute(target_path)) {
1299 char *parent = dirname(ondisk_path);
1300 if (parent == NULL) {
1301 err = got_error_from_errno2("dirname", ondisk_path);
1302 goto done;
1304 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1305 err = got_error_from_errno("asprintf");
1306 goto done;
1311 * unveil(2) restricts our view of paths in the filesystem.
1312 * ENOENT will occur if a link target path does not exist or
1313 * if it points outside our unveiled path space.
1315 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1316 if (resolved_path == NULL) {
1317 if (errno != ENOENT) {
1318 err = got_error_from_errno2("realpath", target_path);
1319 goto done;
1323 /* Only allow symlinks pointing at paths within the work tree. */
1324 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1325 abspath : target_path), worktree->root_path,
1326 strlen(worktree->root_path))) {
1327 /* install as a regular file */
1328 *is_bad_symlink = 1;
1329 got_object_blob_rewind(blob);
1330 err = install_blob(worktree, ondisk_path, path,
1331 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1332 restoring_missing_file, reverting_versioned_file, 1,
1333 path_is_unversioned, repo, progress_cb, progress_arg);
1334 goto done;
1337 /* Do not allow symlinks pointing into the .got directory. */
1338 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1339 GOT_WORKTREE_GOT_DIR) == -1) {
1340 err = got_error_from_errno("asprintf");
1341 goto done;
1343 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1344 abspath : target_path), path_got, strlen(path_got))) {
1345 /* install as a regular file */
1346 *is_bad_symlink = 1;
1347 got_object_blob_rewind(blob);
1348 err = install_blob(worktree, ondisk_path, path,
1349 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1350 restoring_missing_file, reverting_versioned_file, 1,
1351 path_is_unversioned, repo, progress_cb, progress_arg);
1352 goto done;
1355 if (symlink(target_path, ondisk_path) == -1) {
1356 if (errno == EEXIST) {
1357 if (path_is_unversioned) {
1358 err = (*progress_cb)(progress_arg,
1359 GOT_STATUS_UNVERSIONED, path);
1360 goto done;
1362 err = replace_existing_symlink(ondisk_path,
1363 target_path, target_len);
1364 if (err)
1365 goto done;
1366 if (progress_cb) {
1367 err = (*progress_cb)(progress_arg,
1368 reverting_versioned_file ?
1369 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1370 path);
1372 goto done; /* Nothing else to do. */
1375 if (errno == ENOENT) {
1376 char *parent = dirname(ondisk_path);
1377 if (parent == NULL) {
1378 err = got_error_from_errno2("dirname",
1379 ondisk_path);
1380 goto done;
1382 err = add_dir_on_disk(worktree, parent);
1383 if (err)
1384 goto done;
1386 * Retry, and fall through to error handling
1387 * below if this second attempt fails.
1389 if (symlink(target_path, ondisk_path) != -1) {
1390 err = NULL; /* success */
1391 goto done;
1395 /* Handle errors from first or second creation attempt. */
1396 if (errno == ENAMETOOLONG) {
1397 /* bad target path; install as a regular file */
1398 *is_bad_symlink = 1;
1399 got_object_blob_rewind(blob);
1400 err = install_blob(worktree, ondisk_path, path,
1401 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1402 restoring_missing_file, reverting_versioned_file, 1,
1403 path_is_unversioned, repo,
1404 progress_cb, progress_arg);
1405 } else if (errno == ENOTDIR) {
1406 err = got_error_path(ondisk_path,
1407 GOT_ERR_FILE_OBSTRUCTED);
1408 } else {
1409 err = got_error_from_errno3("symlink",
1410 target_path, ondisk_path);
1412 } else if (progress_cb)
1413 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1414 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1415 done:
1416 free(resolved_path);
1417 free(abspath);
1418 free(path_got);
1419 return err;
1422 static const struct got_error *
1423 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1424 const char *path, mode_t te_mode, mode_t st_mode,
1425 struct got_blob_object *blob, int restoring_missing_file,
1426 int reverting_versioned_file, int installing_bad_symlink,
1427 int path_is_unversioned, struct got_repository *repo,
1428 got_worktree_checkout_cb progress_cb, void *progress_arg)
1430 const struct got_error *err = NULL;
1431 int fd = -1;
1432 size_t len, hdrlen;
1433 int update = 0;
1434 char *tmppath = NULL;
1436 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1437 GOT_DEFAULT_FILE_MODE);
1438 if (fd == -1) {
1439 if (errno == ENOENT) {
1440 char *parent = dirname(path);
1441 if (parent == NULL)
1442 return got_error_from_errno2("dirname", path);
1443 err = add_dir_on_disk(worktree, parent);
1444 if (err)
1445 return err;
1446 fd = open(ondisk_path,
1447 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1448 GOT_DEFAULT_FILE_MODE);
1449 if (fd == -1)
1450 return got_error_from_errno2("open",
1451 ondisk_path);
1452 } else if (errno == EEXIST) {
1453 if (path_is_unversioned) {
1454 err = (*progress_cb)(progress_arg,
1455 GOT_STATUS_UNVERSIONED, path);
1456 goto done;
1458 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1459 /* TODO file is obstructed; do something */
1460 err = got_error_path(ondisk_path,
1461 GOT_ERR_FILE_OBSTRUCTED);
1462 goto done;
1463 } else {
1464 err = got_opentemp_named_fd(&tmppath, &fd,
1465 ondisk_path);
1466 if (err)
1467 goto done;
1468 update = 1;
1470 } else
1471 return got_error_from_errno2("open", ondisk_path);
1474 if (progress_cb) {
1475 if (restoring_missing_file)
1476 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1477 path);
1478 else if (reverting_versioned_file)
1479 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1480 path);
1481 else
1482 err = (*progress_cb)(progress_arg,
1483 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1484 if (err)
1485 goto done;
1488 hdrlen = got_object_blob_get_hdrlen(blob);
1489 do {
1490 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1491 err = got_object_blob_read_block(&len, blob);
1492 if (err)
1493 break;
1494 if (len > 0) {
1495 /* Skip blob object header first time around. */
1496 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1497 if (outlen == -1) {
1498 err = got_error_from_errno("write");
1499 goto done;
1500 } else if (outlen != len - hdrlen) {
1501 err = got_error(GOT_ERR_IO);
1502 goto done;
1504 hdrlen = 0;
1506 } while (len != 0);
1508 if (fsync(fd) != 0) {
1509 err = got_error_from_errno("fsync");
1510 goto done;
1513 if (update) {
1514 if (rename(tmppath, ondisk_path) != 0) {
1515 err = got_error_from_errno3("rename", tmppath,
1516 ondisk_path);
1517 unlink(tmppath);
1518 goto done;
1522 if (chmod(ondisk_path,
1523 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1524 err = got_error_from_errno2("chmod", ondisk_path);
1525 goto done;
1528 done:
1529 if (fd != -1 && close(fd) != 0 && err == NULL)
1530 err = got_error_from_errno("close");
1531 free(tmppath);
1532 return err;
1535 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1536 static const struct got_error *
1537 get_modified_file_content_status(unsigned char *status, FILE *f)
1539 const struct got_error *err = NULL;
1540 const char *markers[3] = {
1541 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1542 GOT_DIFF_CONFLICT_MARKER_SEP,
1543 GOT_DIFF_CONFLICT_MARKER_END
1545 int i = 0;
1546 char *line;
1547 size_t len;
1548 const char delim[3] = {'\0', '\0', '\0'};
1550 while (*status == GOT_STATUS_MODIFY) {
1551 line = fparseln(f, &len, NULL, delim, 0);
1552 if (line == NULL) {
1553 if (feof(f))
1554 break;
1555 err = got_ferror(f, GOT_ERR_IO);
1556 break;
1559 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1560 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1561 == 0)
1562 *status = GOT_STATUS_CONFLICT;
1563 else
1564 i++;
1568 return err;
1571 static int
1572 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1574 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1575 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1578 static int
1579 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1581 return !(ie->ctime_sec == sb->st_ctime &&
1582 ie->ctime_nsec == sb->st_ctimensec &&
1583 ie->mtime_sec == sb->st_mtime &&
1584 ie->mtime_nsec == sb->st_mtimensec &&
1585 ie->size == (sb->st_size & 0xffffffff) &&
1586 !xbit_differs(ie, sb->st_mode));
1589 static unsigned char
1590 get_staged_status(struct got_fileindex_entry *ie)
1592 switch (got_fileindex_entry_stage_get(ie)) {
1593 case GOT_FILEIDX_STAGE_ADD:
1594 return GOT_STATUS_ADD;
1595 case GOT_FILEIDX_STAGE_DELETE:
1596 return GOT_STATUS_DELETE;
1597 case GOT_FILEIDX_STAGE_MODIFY:
1598 return GOT_STATUS_MODIFY;
1599 default:
1600 return GOT_STATUS_NO_CHANGE;
1604 static const struct got_error *
1605 get_symlink_status(unsigned char *status, struct stat *sb,
1606 struct got_fileindex_entry *ie, const char *abspath,
1607 int dirfd, const char *de_name, struct got_blob_object *blob)
1609 const struct got_error *err = NULL;
1610 char target_path[PATH_MAX];
1611 char etarget[PATH_MAX];
1612 ssize_t elen;
1613 size_t len, target_len = 0;
1614 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1615 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1617 *status = GOT_STATUS_NO_CHANGE;
1619 /* Blob object content specifies the target path of the link. */
1620 do {
1621 err = got_object_blob_read_block(&len, blob);
1622 if (err)
1623 return err;
1624 if (len + target_len >= sizeof(target_path)) {
1626 * Should not happen. The blob contents were OK
1627 * when this symlink was installed.
1629 return got_error(GOT_ERR_NO_SPACE);
1631 if (len > 0) {
1632 /* Skip blob object header first time around. */
1633 memcpy(target_path + target_len, buf + hdrlen,
1634 len - hdrlen);
1635 target_len += len - hdrlen;
1636 hdrlen = 0;
1638 } while (len != 0);
1639 target_path[target_len] = '\0';
1641 if (dirfd != -1) {
1642 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1643 if (elen == -1)
1644 return got_error_from_errno2("readlinkat", abspath);
1645 } else {
1646 elen = readlink(abspath, etarget, sizeof(etarget));
1647 if (elen == -1)
1648 return got_error_from_errno2("readlinkat", abspath);
1651 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1652 *status = GOT_STATUS_MODIFY;
1654 return NULL;
1657 static const struct got_error *
1658 get_file_status(unsigned char *status, struct stat *sb,
1659 struct got_fileindex_entry *ie, const char *abspath,
1660 int dirfd, const char *de_name, struct got_repository *repo)
1662 const struct got_error *err = NULL;
1663 struct got_object_id id;
1664 size_t hdrlen;
1665 int fd = -1;
1666 FILE *f = NULL;
1667 uint8_t fbuf[8192];
1668 struct got_blob_object *blob = NULL;
1669 size_t flen, blen;
1670 unsigned char staged_status = get_staged_status(ie);
1672 *status = GOT_STATUS_NO_CHANGE;
1675 * Whenever the caller provides a directory descriptor and a
1676 * directory entry name for the file, use them! This prevents
1677 * race conditions if filesystem paths change beneath our feet.
1679 if (dirfd != -1) {
1680 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1681 if (errno == ENOENT) {
1682 if (got_fileindex_entry_has_file_on_disk(ie))
1683 *status = GOT_STATUS_MISSING;
1684 else
1685 *status = GOT_STATUS_DELETE;
1686 goto done;
1688 err = got_error_from_errno2("fstatat", abspath);
1689 goto done;
1691 } else {
1692 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1693 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1694 return got_error_from_errno2("open", abspath);
1695 else if (fd == -1 && errno == ELOOP) {
1696 if (lstat(abspath, sb) == -1)
1697 return got_error_from_errno2("lstat", abspath);
1698 } else if (fd == -1 || fstat(fd, sb) == -1) {
1699 if (errno == ENOENT) {
1700 if (got_fileindex_entry_has_file_on_disk(ie))
1701 *status = GOT_STATUS_MISSING;
1702 else
1703 *status = GOT_STATUS_DELETE;
1704 goto done;
1706 err = got_error_from_errno2("fstat", abspath);
1707 goto done;
1711 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1712 *status = GOT_STATUS_OBSTRUCTED;
1713 goto done;
1716 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1717 *status = GOT_STATUS_DELETE;
1718 goto done;
1719 } else if (!got_fileindex_entry_has_blob(ie) &&
1720 staged_status != GOT_STATUS_ADD) {
1721 *status = GOT_STATUS_ADD;
1722 goto done;
1725 if (!stat_info_differs(ie, sb))
1726 goto done;
1728 if (staged_status == GOT_STATUS_MODIFY ||
1729 staged_status == GOT_STATUS_ADD)
1730 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1731 else
1732 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1734 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1735 if (err)
1736 goto done;
1738 if (S_ISLNK(sb->st_mode)) {
1739 /* Staging changes to symlinks is not yet(?) supported. */
1740 if (staged_status != GOT_STATUS_NO_CHANGE) {
1741 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1742 goto done;
1744 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1745 de_name, blob);
1746 goto done;
1750 if (dirfd != -1) {
1751 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1752 if (fd == -1) {
1753 err = got_error_from_errno2("openat", abspath);
1754 goto done;
1758 f = fdopen(fd, "r");
1759 if (f == NULL) {
1760 err = got_error_from_errno2("fdopen", abspath);
1761 goto done;
1763 fd = -1;
1764 hdrlen = got_object_blob_get_hdrlen(blob);
1765 for (;;) {
1766 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1767 err = got_object_blob_read_block(&blen, blob);
1768 if (err)
1769 goto done;
1770 /* Skip length of blob object header first time around. */
1771 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1772 if (flen == 0 && ferror(f)) {
1773 err = got_error_from_errno("fread");
1774 goto done;
1776 if (blen == 0) {
1777 if (flen != 0)
1778 *status = GOT_STATUS_MODIFY;
1779 break;
1780 } else if (flen == 0) {
1781 if (blen != 0)
1782 *status = GOT_STATUS_MODIFY;
1783 break;
1784 } else if (blen - hdrlen == flen) {
1785 /* Skip blob object header first time around. */
1786 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1787 *status = GOT_STATUS_MODIFY;
1788 break;
1790 } else {
1791 *status = GOT_STATUS_MODIFY;
1792 break;
1794 hdrlen = 0;
1797 if (*status == GOT_STATUS_MODIFY) {
1798 rewind(f);
1799 err = get_modified_file_content_status(status, f);
1800 } else if (xbit_differs(ie, sb->st_mode))
1801 *status = GOT_STATUS_MODE_CHANGE;
1802 done:
1803 if (blob)
1804 got_object_blob_close(blob);
1805 if (f != NULL && fclose(f) == EOF && err == NULL)
1806 err = got_error_from_errno2("fclose", abspath);
1807 if (fd != -1 && close(fd) == -1 && err == NULL)
1808 err = got_error_from_errno2("close", abspath);
1809 return err;
1813 * Update timestamps in the file index if a file is unmodified and
1814 * we had to run a full content comparison to find out.
1816 static const struct got_error *
1817 sync_timestamps(char *ondisk_path, unsigned char status,
1818 struct got_fileindex_entry *ie, struct stat *sb)
1820 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1821 return got_fileindex_entry_update(ie, ondisk_path,
1822 ie->blob_sha1, ie->commit_sha1, 1);
1824 return NULL;
1827 static const struct got_error *
1828 update_blob(struct got_worktree *worktree,
1829 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1830 struct got_tree_entry *te, const char *path,
1831 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1832 void *progress_arg)
1834 const struct got_error *err = NULL;
1835 struct got_blob_object *blob = NULL;
1836 char *ondisk_path;
1837 unsigned char status = GOT_STATUS_NO_CHANGE;
1838 struct stat sb;
1840 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1841 return got_error_from_errno("asprintf");
1843 if (ie) {
1844 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1845 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1846 goto done;
1848 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1849 repo);
1850 if (err)
1851 goto done;
1852 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1853 sb.st_mode = got_fileindex_perms_to_st(ie);
1854 } else {
1855 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1856 status = GOT_STATUS_UNVERSIONED;
1859 if (status == GOT_STATUS_OBSTRUCTED) {
1860 err = (*progress_cb)(progress_arg, status, path);
1861 goto done;
1863 if (status == GOT_STATUS_CONFLICT) {
1864 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1865 path);
1866 goto done;
1869 if (ie && status != GOT_STATUS_MISSING &&
1870 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1871 if (got_fileindex_entry_has_commit(ie) &&
1872 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1873 SHA1_DIGEST_LENGTH) == 0) {
1874 err = sync_timestamps(ondisk_path, status, ie, &sb);
1875 if (err)
1876 goto done;
1877 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1878 path);
1879 goto done;
1881 if (got_fileindex_entry_has_blob(ie) &&
1882 memcmp(ie->blob_sha1, te->id.sha1,
1883 SHA1_DIGEST_LENGTH) == 0) {
1884 err = sync_timestamps(ondisk_path, status, ie, &sb);
1885 goto done;
1889 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 if (err)
1891 goto done;
1893 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 int update_timestamps;
1895 struct got_blob_object *blob2 = NULL;
1896 char *label_orig = NULL;
1897 if (got_fileindex_entry_has_blob(ie)) {
1898 struct got_object_id id2;
1899 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 if (err)
1902 goto done;
1904 if (got_fileindex_entry_has_commit(ie)) {
1905 char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 sizeof(id_str)) == NULL) {
1908 err = got_error_path(id_str,
1909 GOT_ERR_BAD_OBJ_ID_STR);
1910 goto done;
1912 if (asprintf(&label_orig, "%s: commit %s",
1913 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 err = got_error_from_errno("asprintf");
1915 goto done;
1918 if (S_ISLNK(te->mode)) {
1919 err = merge_symlink(worktree, blob2,
1920 ondisk_path, path, sb.st_mode, label_orig,
1921 blob, worktree->base_commit_id, repo,
1922 progress_cb, progress_arg);
1923 } else {
1924 err = merge_blob(&update_timestamps, worktree, blob2,
1925 ondisk_path, path, sb.st_mode, label_orig, blob,
1926 worktree->base_commit_id, repo,
1927 progress_cb, progress_arg);
1929 free(label_orig);
1930 if (blob2)
1931 got_object_blob_close(blob2);
1932 if (err)
1933 goto done;
1935 * Do not update timestamps of files with local changes.
1936 * Otherwise, a future status walk would treat them as
1937 * unmodified files again.
1939 err = got_fileindex_entry_update(ie, ondisk_path,
1940 blob->id.sha1, worktree->base_commit_id->sha1,
1941 update_timestamps);
1942 } else if (status == GOT_STATUS_MODE_CHANGE) {
1943 err = got_fileindex_entry_update(ie, ondisk_path,
1944 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1945 } else if (status == GOT_STATUS_DELETE) {
1946 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1947 if (err)
1948 goto done;
1949 err = got_fileindex_entry_update(ie, ondisk_path,
1950 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1951 if (err)
1952 goto done;
1953 } else {
1954 int is_bad_symlink = 0;
1955 if (S_ISLNK(te->mode)) {
1956 err = install_symlink(&is_bad_symlink, worktree,
1957 ondisk_path, path, blob,
1958 status == GOT_STATUS_MISSING, 0,
1959 status == GOT_STATUS_UNVERSIONED, repo,
1960 progress_cb, progress_arg);
1961 } else {
1962 err = install_blob(worktree, ondisk_path, path,
1963 te->mode, sb.st_mode, blob,
1964 status == GOT_STATUS_MISSING, 0, 0,
1965 status == GOT_STATUS_UNVERSIONED, repo,
1966 progress_cb, progress_arg);
1968 if (err)
1969 goto done;
1971 if (ie) {
1972 err = got_fileindex_entry_update(ie, ondisk_path,
1973 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1974 } else {
1975 err = create_fileindex_entry(&ie, fileindex,
1976 worktree->base_commit_id, ondisk_path, path,
1977 &blob->id);
1979 if (err)
1980 goto done;
1982 if (is_bad_symlink) {
1983 err = got_fileindex_entry_filetype_set(ie,
1984 GOT_FILEIDX_MODE_BAD_SYMLINK);
1985 if (err)
1986 goto done;
1989 got_object_blob_close(blob);
1990 done:
1991 free(ondisk_path);
1992 return err;
1995 static const struct got_error *
1996 remove_ondisk_file(const char *root_path, const char *path)
1998 const struct got_error *err = NULL;
1999 char *ondisk_path = NULL;
2001 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2002 return got_error_from_errno("asprintf");
2004 if (unlink(ondisk_path) == -1) {
2005 if (errno != ENOENT)
2006 err = got_error_from_errno2("unlink", ondisk_path);
2007 } else {
2008 char *parent = dirname(ondisk_path);
2009 while (parent && strcmp(parent, root_path) != 0) {
2010 if (rmdir(parent) == -1) {
2011 if (errno != ENOTEMPTY)
2012 err = got_error_from_errno2("rmdir",
2013 parent);
2014 break;
2016 parent = dirname(parent);
2019 free(ondisk_path);
2020 return err;
2023 static const struct got_error *
2024 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2025 struct got_fileindex_entry *ie, struct got_repository *repo,
2026 got_worktree_checkout_cb progress_cb, void *progress_arg)
2028 const struct got_error *err = NULL;
2029 unsigned char status;
2030 struct stat sb;
2031 char *ondisk_path;
2033 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2034 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2036 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2037 == -1)
2038 return got_error_from_errno("asprintf");
2040 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2041 if (err)
2042 goto done;
2044 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2045 char ondisk_target[PATH_MAX];
2046 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2047 sizeof(ondisk_target));
2048 if (ondisk_len == -1) {
2049 err = got_error_from_errno2("readlink", ondisk_path);
2050 goto done;
2052 ondisk_target[ondisk_len] = '\0';
2053 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2054 NULL, NULL, /* XXX pass common ancestor info? */
2055 ondisk_target, ondisk_path);
2056 if (err)
2057 goto done;
2058 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2059 ie->path);
2060 goto done;
2063 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2064 status == GOT_STATUS_ADD) {
2065 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2066 if (err)
2067 goto done;
2069 * Preserve the working file and change the deleted blob's
2070 * entry into a schedule-add entry.
2072 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2073 0);
2074 } else {
2075 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2076 if (err)
2077 goto done;
2078 if (status == GOT_STATUS_NO_CHANGE) {
2079 err = remove_ondisk_file(worktree->root_path, ie->path);
2080 if (err)
2081 goto done;
2083 got_fileindex_entry_remove(fileindex, ie);
2085 done:
2086 free(ondisk_path);
2087 return err;
2090 struct diff_cb_arg {
2091 struct got_fileindex *fileindex;
2092 struct got_worktree *worktree;
2093 struct got_repository *repo;
2094 got_worktree_checkout_cb progress_cb;
2095 void *progress_arg;
2096 got_cancel_cb cancel_cb;
2097 void *cancel_arg;
2100 static const struct got_error *
2101 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2102 struct got_tree_entry *te, const char *parent_path)
2104 struct diff_cb_arg *a = arg;
2106 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2107 return got_error(GOT_ERR_CANCELLED);
2109 return update_blob(a->worktree, a->fileindex, ie, te,
2110 ie->path, a->repo, a->progress_cb, a->progress_arg);
2113 static const struct got_error *
2114 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2116 struct diff_cb_arg *a = arg;
2118 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2119 return got_error(GOT_ERR_CANCELLED);
2121 return delete_blob(a->worktree, a->fileindex, ie,
2122 a->repo, a->progress_cb, a->progress_arg);
2125 static const struct got_error *
2126 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2128 struct diff_cb_arg *a = arg;
2129 const struct got_error *err;
2130 char *path;
2132 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2133 return got_error(GOT_ERR_CANCELLED);
2135 if (got_object_tree_entry_is_submodule(te))
2136 return NULL;
2138 if (asprintf(&path, "%s%s%s", parent_path,
2139 parent_path[0] ? "/" : "", te->name)
2140 == -1)
2141 return got_error_from_errno("asprintf");
2143 if (S_ISDIR(te->mode))
2144 err = add_dir_on_disk(a->worktree, path);
2145 else
2146 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2147 a->repo, a->progress_cb, a->progress_arg);
2149 free(path);
2150 return err;
2153 static const struct got_error *
2154 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2156 const struct got_error *err = NULL;
2157 char *uuidstr = NULL;
2158 uint32_t uuid_status;
2160 *refname = NULL;
2162 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2163 if (uuid_status != uuid_s_ok)
2164 return got_error_uuid(uuid_status, "uuid_to_string");
2166 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2167 == -1) {
2168 err = got_error_from_errno("asprintf");
2169 *refname = NULL;
2171 free(uuidstr);
2172 return err;
2175 const struct got_error *
2176 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2178 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2181 static const struct got_error *
2182 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2184 return get_ref_name(refname, worktree,
2185 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2188 static const struct got_error *
2189 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2191 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2194 static const struct got_error *
2195 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2197 return get_ref_name(refname, worktree,
2198 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2201 static const struct got_error *
2202 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2204 return get_ref_name(refname, worktree,
2205 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2208 static const struct got_error *
2209 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2211 return get_ref_name(refname, worktree,
2212 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2215 static const struct got_error *
2216 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2218 return get_ref_name(refname, worktree,
2219 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2222 static const struct got_error *
2223 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2225 return get_ref_name(refname, worktree,
2226 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2229 static const struct got_error *
2230 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2232 return get_ref_name(refname, worktree,
2233 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2236 const struct got_error *
2237 got_worktree_get_histedit_script_path(char **path,
2238 struct got_worktree *worktree)
2240 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2241 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2242 *path = NULL;
2243 return got_error_from_errno("asprintf");
2245 return NULL;
2249 * Prevent Git's garbage collector from deleting our base commit by
2250 * setting a reference to our base commit's ID.
2252 static const struct got_error *
2253 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2255 const struct got_error *err = NULL;
2256 struct got_reference *ref = NULL;
2257 char *refname;
2259 err = got_worktree_get_base_ref_name(&refname, worktree);
2260 if (err)
2261 return err;
2263 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2264 if (err)
2265 goto done;
2267 err = got_ref_write(ref, repo);
2268 done:
2269 free(refname);
2270 if (ref)
2271 got_ref_close(ref);
2272 return err;
2275 static const struct got_error *
2276 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2278 const struct got_error *err = NULL;
2280 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2281 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2282 err = got_error_from_errno("asprintf");
2283 *fileindex_path = NULL;
2285 return err;
2289 static const struct got_error *
2290 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2291 struct got_worktree *worktree)
2293 const struct got_error *err = NULL;
2294 FILE *index = NULL;
2296 *fileindex_path = NULL;
2297 *fileindex = got_fileindex_alloc();
2298 if (*fileindex == NULL)
2299 return got_error_from_errno("got_fileindex_alloc");
2301 err = get_fileindex_path(fileindex_path, worktree);
2302 if (err)
2303 goto done;
2305 index = fopen(*fileindex_path, "rb");
2306 if (index == NULL) {
2307 if (errno != ENOENT)
2308 err = got_error_from_errno2("fopen", *fileindex_path);
2309 } else {
2310 err = got_fileindex_read(*fileindex, index);
2311 if (fclose(index) != 0 && err == NULL)
2312 err = got_error_from_errno("fclose");
2314 done:
2315 if (err) {
2316 free(*fileindex_path);
2317 *fileindex_path = NULL;
2318 got_fileindex_free(*fileindex);
2319 *fileindex = NULL;
2321 return err;
2324 struct bump_base_commit_id_arg {
2325 struct got_object_id *base_commit_id;
2326 const char *path;
2327 size_t path_len;
2328 const char *entry_name;
2329 got_worktree_checkout_cb progress_cb;
2330 void *progress_arg;
2333 /* Bump base commit ID of all files within an updated part of the work tree. */
2334 static const struct got_error *
2335 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2337 const struct got_error *err;
2338 struct bump_base_commit_id_arg *a = arg;
2340 if (a->entry_name) {
2341 if (strcmp(ie->path, a->path) != 0)
2342 return NULL;
2343 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2344 return NULL;
2346 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2347 SHA1_DIGEST_LENGTH) == 0)
2348 return NULL;
2350 if (a->progress_cb) {
2351 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2352 ie->path);
2353 if (err)
2354 return err;
2356 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2357 return NULL;
2360 static const struct got_error *
2361 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2363 const struct got_error *err = NULL;
2364 char *new_fileindex_path = NULL;
2365 FILE *new_index = NULL;
2366 struct timespec timeout;
2368 err = got_opentemp_named(&new_fileindex_path, &new_index,
2369 fileindex_path);
2370 if (err)
2371 goto done;
2373 err = got_fileindex_write(fileindex, new_index);
2374 if (err)
2375 goto done;
2377 if (rename(new_fileindex_path, fileindex_path) != 0) {
2378 err = got_error_from_errno3("rename", new_fileindex_path,
2379 fileindex_path);
2380 unlink(new_fileindex_path);
2384 * Sleep for a short amount of time to ensure that files modified after
2385 * this program exits have a different time stamp from the one which
2386 * was recorded in the file index.
2388 timeout.tv_sec = 0;
2389 timeout.tv_nsec = 1;
2390 nanosleep(&timeout, NULL);
2391 done:
2392 if (new_index)
2393 fclose(new_index);
2394 free(new_fileindex_path);
2395 return err;
2398 static const struct got_error *
2399 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2400 struct got_object_id **tree_id, const char *wt_relpath,
2401 struct got_worktree *worktree, struct got_repository *repo)
2403 const struct got_error *err = NULL;
2404 struct got_object_id *id = NULL;
2405 char *in_repo_path = NULL;
2406 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2408 *entry_type = GOT_OBJ_TYPE_ANY;
2409 *tree_relpath = NULL;
2410 *tree_id = NULL;
2412 if (wt_relpath[0] == '\0') {
2413 /* Check out all files within the work tree. */
2414 *entry_type = GOT_OBJ_TYPE_TREE;
2415 *tree_relpath = strdup("");
2416 if (*tree_relpath == NULL) {
2417 err = got_error_from_errno("strdup");
2418 goto done;
2420 err = got_object_id_by_path(tree_id, repo,
2421 worktree->base_commit_id, worktree->path_prefix);
2422 if (err)
2423 goto done;
2424 return NULL;
2427 /* Check out a subset of files in the work tree. */
2429 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2430 is_root_wt ? "" : "/", wt_relpath) == -1) {
2431 err = got_error_from_errno("asprintf");
2432 goto done;
2435 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2436 in_repo_path);
2437 if (err)
2438 goto done;
2440 free(in_repo_path);
2441 in_repo_path = NULL;
2443 err = got_object_get_type(entry_type, repo, id);
2444 if (err)
2445 goto done;
2447 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2448 /* Check out a single file. */
2449 if (strchr(wt_relpath, '/') == NULL) {
2450 /* Check out a single file in work tree's root dir. */
2451 in_repo_path = strdup(worktree->path_prefix);
2452 if (in_repo_path == NULL) {
2453 err = got_error_from_errno("strdup");
2454 goto done;
2456 *tree_relpath = strdup("");
2457 if (*tree_relpath == NULL) {
2458 err = got_error_from_errno("strdup");
2459 goto done;
2461 } else {
2462 /* Check out a single file in a subdirectory. */
2463 err = got_path_dirname(tree_relpath, wt_relpath);
2464 if (err)
2465 return err;
2466 if (asprintf(&in_repo_path, "%s%s%s",
2467 worktree->path_prefix, is_root_wt ? "" : "/",
2468 *tree_relpath) == -1) {
2469 err = got_error_from_errno("asprintf");
2470 goto done;
2473 err = got_object_id_by_path(tree_id, repo,
2474 worktree->base_commit_id, in_repo_path);
2475 } else {
2476 /* Check out all files within a subdirectory. */
2477 *tree_id = got_object_id_dup(id);
2478 if (*tree_id == NULL) {
2479 err = got_error_from_errno("got_object_id_dup");
2480 goto done;
2482 *tree_relpath = strdup(wt_relpath);
2483 if (*tree_relpath == NULL) {
2484 err = got_error_from_errno("strdup");
2485 goto done;
2488 done:
2489 free(id);
2490 free(in_repo_path);
2491 if (err) {
2492 *entry_type = GOT_OBJ_TYPE_ANY;
2493 free(*tree_relpath);
2494 *tree_relpath = NULL;
2495 free(*tree_id);
2496 *tree_id = NULL;
2498 return err;
2501 static const struct got_error *
2502 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2503 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2504 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2505 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2507 const struct got_error *err = NULL;
2508 struct got_commit_object *commit = NULL;
2509 struct got_tree_object *tree = NULL;
2510 struct got_fileindex_diff_tree_cb diff_cb;
2511 struct diff_cb_arg arg;
2513 err = ref_base_commit(worktree, repo);
2514 if (err) {
2515 if (!(err->code == GOT_ERR_ERRNO &&
2516 (errno == EACCES || errno == EROFS)))
2517 goto done;
2518 err = (*progress_cb)(progress_arg,
2519 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2520 if (err)
2521 return err;
2524 err = got_object_open_as_commit(&commit, repo,
2525 worktree->base_commit_id);
2526 if (err)
2527 goto done;
2529 err = got_object_open_as_tree(&tree, repo, tree_id);
2530 if (err)
2531 goto done;
2533 if (entry_name &&
2534 got_object_tree_find_entry(tree, entry_name) == NULL) {
2535 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2536 goto done;
2539 diff_cb.diff_old_new = diff_old_new;
2540 diff_cb.diff_old = diff_old;
2541 diff_cb.diff_new = diff_new;
2542 arg.fileindex = fileindex;
2543 arg.worktree = worktree;
2544 arg.repo = repo;
2545 arg.progress_cb = progress_cb;
2546 arg.progress_arg = progress_arg;
2547 arg.cancel_cb = cancel_cb;
2548 arg.cancel_arg = cancel_arg;
2549 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2550 entry_name, repo, &diff_cb, &arg);
2551 done:
2552 if (tree)
2553 got_object_tree_close(tree);
2554 if (commit)
2555 got_object_commit_close(commit);
2556 return err;
2559 const struct got_error *
2560 got_worktree_checkout_files(struct got_worktree *worktree,
2561 struct got_pathlist_head *paths, struct got_repository *repo,
2562 got_worktree_checkout_cb progress_cb, void *progress_arg,
2563 got_cancel_cb cancel_cb, void *cancel_arg)
2565 const struct got_error *err = NULL, *sync_err, *unlockerr;
2566 struct got_commit_object *commit = NULL;
2567 struct got_tree_object *tree = NULL;
2568 struct got_fileindex *fileindex = NULL;
2569 char *fileindex_path = NULL;
2570 struct got_pathlist_entry *pe;
2571 struct tree_path_data {
2572 SIMPLEQ_ENTRY(tree_path_data) entry;
2573 struct got_object_id *tree_id;
2574 int entry_type;
2575 char *relpath;
2576 char *entry_name;
2577 } *tpd = NULL;
2578 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2580 SIMPLEQ_INIT(&tree_paths);
2582 err = lock_worktree(worktree, LOCK_EX);
2583 if (err)
2584 return err;
2586 /* Map all specified paths to in-repository trees. */
2587 TAILQ_FOREACH(pe, paths, entry) {
2588 tpd = malloc(sizeof(*tpd));
2589 if (tpd == NULL) {
2590 err = got_error_from_errno("malloc");
2591 goto done;
2594 err = find_tree_entry_for_checkout(&tpd->entry_type,
2595 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2596 if (err) {
2597 free(tpd);
2598 goto done;
2601 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2602 err = got_path_basename(&tpd->entry_name, pe->path);
2603 if (err) {
2604 free(tpd->relpath);
2605 free(tpd->tree_id);
2606 free(tpd);
2607 goto done;
2609 } else
2610 tpd->entry_name = NULL;
2612 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2616 * Read the file index.
2617 * Checking out files is supposed to be an idempotent operation.
2618 * If the on-disk file index is incomplete we will try to complete it.
2620 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2621 if (err)
2622 goto done;
2624 tpd = SIMPLEQ_FIRST(&tree_paths);
2625 TAILQ_FOREACH(pe, paths, entry) {
2626 struct bump_base_commit_id_arg bbc_arg;
2628 err = checkout_files(worktree, fileindex, tpd->relpath,
2629 tpd->tree_id, tpd->entry_name, repo,
2630 progress_cb, progress_arg, cancel_cb, cancel_arg);
2631 if (err)
2632 break;
2634 bbc_arg.base_commit_id = worktree->base_commit_id;
2635 bbc_arg.entry_name = tpd->entry_name;
2636 bbc_arg.path = pe->path;
2637 bbc_arg.path_len = pe->path_len;
2638 bbc_arg.progress_cb = progress_cb;
2639 bbc_arg.progress_arg = progress_arg;
2640 err = got_fileindex_for_each_entry_safe(fileindex,
2641 bump_base_commit_id, &bbc_arg);
2642 if (err)
2643 break;
2645 tpd = SIMPLEQ_NEXT(tpd, entry);
2647 sync_err = sync_fileindex(fileindex, fileindex_path);
2648 if (sync_err && err == NULL)
2649 err = sync_err;
2650 done:
2651 free(fileindex_path);
2652 if (tree)
2653 got_object_tree_close(tree);
2654 if (commit)
2655 got_object_commit_close(commit);
2656 if (fileindex)
2657 got_fileindex_free(fileindex);
2658 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2659 tpd = SIMPLEQ_FIRST(&tree_paths);
2660 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2661 free(tpd->relpath);
2662 free(tpd->tree_id);
2663 free(tpd);
2665 unlockerr = lock_worktree(worktree, LOCK_SH);
2666 if (unlockerr && err == NULL)
2667 err = unlockerr;
2668 return err;
2671 struct merge_file_cb_arg {
2672 struct got_worktree *worktree;
2673 struct got_fileindex *fileindex;
2674 got_worktree_checkout_cb progress_cb;
2675 void *progress_arg;
2676 got_cancel_cb cancel_cb;
2677 void *cancel_arg;
2678 const char *label_orig;
2679 struct got_object_id *commit_id2;
2682 static const struct got_error *
2683 merge_file_cb(void *arg, struct got_blob_object *blob1,
2684 struct got_blob_object *blob2, struct got_object_id *id1,
2685 struct got_object_id *id2, const char *path1, const char *path2,
2686 mode_t mode1, mode_t mode2, struct got_repository *repo)
2688 static const struct got_error *err = NULL;
2689 struct merge_file_cb_arg *a = arg;
2690 struct got_fileindex_entry *ie;
2691 char *ondisk_path = NULL;
2692 struct stat sb;
2693 unsigned char status;
2694 int local_changes_subsumed;
2696 if (blob1 && blob2) {
2697 ie = got_fileindex_entry_get(a->fileindex, path2,
2698 strlen(path2));
2699 if (ie == NULL)
2700 return (*a->progress_cb)(a->progress_arg,
2701 GOT_STATUS_MISSING, path2);
2703 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2704 path2) == -1)
2705 return got_error_from_errno("asprintf");
2707 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2708 repo);
2709 if (err)
2710 goto done;
2712 if (status == GOT_STATUS_DELETE) {
2713 err = (*a->progress_cb)(a->progress_arg,
2714 GOT_STATUS_MERGE, path2);
2715 goto done;
2717 if (status != GOT_STATUS_NO_CHANGE &&
2718 status != GOT_STATUS_MODIFY &&
2719 status != GOT_STATUS_CONFLICT &&
2720 status != GOT_STATUS_ADD) {
2721 err = (*a->progress_cb)(a->progress_arg, status, path2);
2722 goto done;
2725 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2726 err = merge_symlink(a->worktree, blob1,
2727 ondisk_path, path2, sb.st_mode, a->label_orig,
2728 blob2, a->commit_id2, repo, a->progress_cb,
2729 a->progress_arg);
2730 } else {
2731 err = merge_blob(&local_changes_subsumed, a->worktree,
2732 blob1, ondisk_path, path2, sb.st_mode,
2733 a->label_orig, blob2, a->commit_id2, repo,
2734 a->progress_cb, a->progress_arg);
2736 } else if (blob1) {
2737 ie = got_fileindex_entry_get(a->fileindex, path1,
2738 strlen(path1));
2739 if (ie == NULL)
2740 return (*a->progress_cb)(a->progress_arg,
2741 GOT_STATUS_MISSING, path1);
2743 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2744 path1) == -1)
2745 return got_error_from_errno("asprintf");
2747 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2748 repo);
2749 if (err)
2750 goto done;
2752 switch (status) {
2753 case GOT_STATUS_NO_CHANGE:
2754 err = (*a->progress_cb)(a->progress_arg,
2755 GOT_STATUS_DELETE, path1);
2756 if (err)
2757 goto done;
2758 err = remove_ondisk_file(a->worktree->root_path, path1);
2759 if (err)
2760 goto done;
2761 if (ie)
2762 got_fileindex_entry_mark_deleted_from_disk(ie);
2763 break;
2764 case GOT_STATUS_DELETE:
2765 case GOT_STATUS_MISSING:
2766 err = (*a->progress_cb)(a->progress_arg,
2767 GOT_STATUS_DELETE, path1);
2768 if (err)
2769 goto done;
2770 if (ie)
2771 got_fileindex_entry_mark_deleted_from_disk(ie);
2772 break;
2773 case GOT_STATUS_ADD:
2774 case GOT_STATUS_MODIFY:
2775 case GOT_STATUS_CONFLICT:
2776 err = (*a->progress_cb)(a->progress_arg,
2777 GOT_STATUS_CANNOT_DELETE, path1);
2778 if (err)
2779 goto done;
2780 break;
2781 case GOT_STATUS_OBSTRUCTED:
2782 err = (*a->progress_cb)(a->progress_arg, status, path1);
2783 if (err)
2784 goto done;
2785 break;
2786 default:
2787 break;
2789 } else if (blob2) {
2790 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2791 path2) == -1)
2792 return got_error_from_errno("asprintf");
2793 ie = got_fileindex_entry_get(a->fileindex, path2,
2794 strlen(path2));
2795 if (ie) {
2796 err = get_file_status(&status, &sb, ie, ondisk_path,
2797 -1, NULL, repo);
2798 if (err)
2799 goto done;
2800 if (status != GOT_STATUS_NO_CHANGE &&
2801 status != GOT_STATUS_MODIFY &&
2802 status != GOT_STATUS_CONFLICT &&
2803 status != GOT_STATUS_ADD) {
2804 err = (*a->progress_cb)(a->progress_arg,
2805 status, path2);
2806 goto done;
2808 if (S_ISLNK(mode2)) {
2809 err = merge_symlink(a->worktree, NULL,
2810 ondisk_path, path2, sb.st_mode,
2811 a->label_orig, blob2, a->commit_id2,
2812 repo, a->progress_cb, a->progress_arg);
2813 if (err)
2814 goto done;
2815 } else {
2816 err = merge_blob(&local_changes_subsumed,
2817 a->worktree, NULL, ondisk_path, path2,
2818 sb.st_mode, a->label_orig, blob2,
2819 a->commit_id2, repo, a->progress_cb,
2820 a->progress_arg);
2821 if (err)
2822 goto done;
2824 if (status == GOT_STATUS_DELETE) {
2825 err = got_fileindex_entry_update(ie,
2826 ondisk_path, blob2->id.sha1,
2827 a->worktree->base_commit_id->sha1, 0);
2828 if (err)
2829 goto done;
2831 } else {
2832 int is_bad_symlink = 0;
2833 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2834 if (S_ISLNK(mode2)) {
2835 err = install_symlink(&is_bad_symlink,
2836 a->worktree, ondisk_path, path2, blob2, 0,
2837 0, 1, repo, a->progress_cb, a->progress_arg);
2838 } else {
2839 err = install_blob(a->worktree, ondisk_path, path2,
2840 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2841 a->progress_cb, a->progress_arg);
2843 if (err)
2844 goto done;
2845 err = got_fileindex_entry_alloc(&ie, path2);
2846 if (err)
2847 goto done;
2848 err = got_fileindex_entry_update(ie, ondisk_path,
2849 NULL, NULL, 1);
2850 if (err) {
2851 got_fileindex_entry_free(ie);
2852 goto done;
2854 err = got_fileindex_entry_add(a->fileindex, ie);
2855 if (err) {
2856 got_fileindex_entry_free(ie);
2857 goto done;
2859 if (is_bad_symlink) {
2860 err = got_fileindex_entry_filetype_set(ie,
2861 GOT_FILEIDX_MODE_BAD_SYMLINK);
2862 if (err)
2863 goto done;
2867 done:
2868 free(ondisk_path);
2869 return err;
2872 struct check_merge_ok_arg {
2873 struct got_worktree *worktree;
2874 struct got_repository *repo;
2877 static const struct got_error *
2878 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2880 const struct got_error *err = NULL;
2881 struct check_merge_ok_arg *a = arg;
2882 unsigned char status;
2883 struct stat sb;
2884 char *ondisk_path;
2886 /* Reject merges into a work tree with mixed base commits. */
2887 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2888 SHA1_DIGEST_LENGTH))
2889 return got_error(GOT_ERR_MIXED_COMMITS);
2891 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2892 == -1)
2893 return got_error_from_errno("asprintf");
2895 /* Reject merges into a work tree with conflicted files. */
2896 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2897 if (err)
2898 return err;
2899 if (status == GOT_STATUS_CONFLICT)
2900 return got_error(GOT_ERR_CONFLICTS);
2902 return NULL;
2905 static const struct got_error *
2906 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2907 const char *fileindex_path, struct got_object_id *commit_id1,
2908 struct got_object_id *commit_id2, struct got_repository *repo,
2909 got_worktree_checkout_cb progress_cb, void *progress_arg,
2910 got_cancel_cb cancel_cb, void *cancel_arg)
2912 const struct got_error *err = NULL, *sync_err;
2913 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2914 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2915 struct merge_file_cb_arg arg;
2916 char *label_orig = NULL;
2918 if (commit_id1) {
2919 char *id_str;
2921 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2922 worktree->path_prefix);
2923 if (err)
2924 goto done;
2926 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2927 if (err)
2928 goto done;
2930 err = got_object_id_str(&id_str, commit_id1);
2931 if (err)
2932 goto done;
2934 if (asprintf(&label_orig, "%s: commit %s",
2935 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2936 err = got_error_from_errno("asprintf");
2937 free(id_str);
2938 goto done;
2940 free(id_str);
2943 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2944 worktree->path_prefix);
2945 if (err)
2946 goto done;
2948 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2949 if (err)
2950 goto done;
2952 arg.worktree = worktree;
2953 arg.fileindex = fileindex;
2954 arg.progress_cb = progress_cb;
2955 arg.progress_arg = progress_arg;
2956 arg.cancel_cb = cancel_cb;
2957 arg.cancel_arg = cancel_arg;
2958 arg.label_orig = label_orig;
2959 arg.commit_id2 = commit_id2;
2960 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2961 sync_err = sync_fileindex(fileindex, fileindex_path);
2962 if (sync_err && err == NULL)
2963 err = sync_err;
2964 done:
2965 if (tree1)
2966 got_object_tree_close(tree1);
2967 if (tree2)
2968 got_object_tree_close(tree2);
2969 free(label_orig);
2970 return err;
2973 const struct got_error *
2974 got_worktree_merge_files(struct got_worktree *worktree,
2975 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2976 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2977 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2979 const struct got_error *err, *unlockerr;
2980 char *fileindex_path = NULL;
2981 struct got_fileindex *fileindex = NULL;
2982 struct check_merge_ok_arg mok_arg;
2984 err = lock_worktree(worktree, LOCK_EX);
2985 if (err)
2986 return err;
2988 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2989 if (err)
2990 goto done;
2992 mok_arg.worktree = worktree;
2993 mok_arg.repo = repo;
2994 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2995 &mok_arg);
2996 if (err)
2997 goto done;
2999 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3000 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3001 done:
3002 if (fileindex)
3003 got_fileindex_free(fileindex);
3004 free(fileindex_path);
3005 unlockerr = lock_worktree(worktree, LOCK_SH);
3006 if (unlockerr && err == NULL)
3007 err = unlockerr;
3008 return err;
3011 struct diff_dir_cb_arg {
3012 struct got_fileindex *fileindex;
3013 struct got_worktree *worktree;
3014 const char *status_path;
3015 size_t status_path_len;
3016 struct got_repository *repo;
3017 got_worktree_status_cb status_cb;
3018 void *status_arg;
3019 got_cancel_cb cancel_cb;
3020 void *cancel_arg;
3021 /* A pathlist containing per-directory pathlists of ignore patterns. */
3022 struct got_pathlist_head ignores;
3023 int report_unchanged;
3024 int no_ignores;
3027 static const struct got_error *
3028 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3029 int dirfd, const char *de_name,
3030 got_worktree_status_cb status_cb, void *status_arg,
3031 struct got_repository *repo, int report_unchanged)
3033 const struct got_error *err = NULL;
3034 unsigned char status = GOT_STATUS_NO_CHANGE;
3035 unsigned char staged_status = get_staged_status(ie);
3036 struct stat sb;
3037 struct got_object_id blob_id, commit_id, staged_blob_id;
3038 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3039 struct got_object_id *staged_blob_idp = NULL;
3041 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3042 if (err)
3043 return err;
3045 if (status == GOT_STATUS_NO_CHANGE &&
3046 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3047 return NULL;
3049 if (got_fileindex_entry_has_blob(ie)) {
3050 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3051 blob_idp = &blob_id;
3053 if (got_fileindex_entry_has_commit(ie)) {
3054 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3055 commit_idp = &commit_id;
3057 if (staged_status == GOT_STATUS_ADD ||
3058 staged_status == GOT_STATUS_MODIFY) {
3059 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3060 SHA1_DIGEST_LENGTH);
3061 staged_blob_idp = &staged_blob_id;
3064 return (*status_cb)(status_arg, status, staged_status,
3065 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3068 static const struct got_error *
3069 status_old_new(void *arg, struct got_fileindex_entry *ie,
3070 struct dirent *de, const char *parent_path, int dirfd)
3072 const struct got_error *err = NULL;
3073 struct diff_dir_cb_arg *a = arg;
3074 char *abspath;
3076 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3077 return got_error(GOT_ERR_CANCELLED);
3079 if (got_path_cmp(parent_path, a->status_path,
3080 strlen(parent_path), a->status_path_len) != 0 &&
3081 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3082 return NULL;
3084 if (parent_path[0]) {
3085 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3086 parent_path, de->d_name) == -1)
3087 return got_error_from_errno("asprintf");
3088 } else {
3089 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3090 de->d_name) == -1)
3091 return got_error_from_errno("asprintf");
3094 err = report_file_status(ie, abspath, dirfd, de->d_name,
3095 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3096 free(abspath);
3097 return err;
3100 static const struct got_error *
3101 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3103 struct diff_dir_cb_arg *a = arg;
3104 struct got_object_id blob_id, commit_id;
3105 unsigned char status;
3107 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3108 return got_error(GOT_ERR_CANCELLED);
3110 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3111 return NULL;
3113 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3114 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3115 if (got_fileindex_entry_has_file_on_disk(ie))
3116 status = GOT_STATUS_MISSING;
3117 else
3118 status = GOT_STATUS_DELETE;
3119 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3120 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3123 void
3124 free_ignorelist(struct got_pathlist_head *ignorelist)
3126 struct got_pathlist_entry *pe;
3128 TAILQ_FOREACH(pe, ignorelist, entry)
3129 free((char *)pe->path);
3130 got_pathlist_free(ignorelist);
3133 void
3134 free_ignores(struct got_pathlist_head *ignores)
3136 struct got_pathlist_entry *pe;
3138 TAILQ_FOREACH(pe, ignores, entry) {
3139 struct got_pathlist_head *ignorelist = pe->data;
3140 free_ignorelist(ignorelist);
3141 free((char *)pe->path);
3143 got_pathlist_free(ignores);
3146 static const struct got_error *
3147 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3149 const struct got_error *err = NULL;
3150 struct got_pathlist_entry *pe = NULL;
3151 struct got_pathlist_head *ignorelist;
3152 char *line = NULL, *pattern, *dirpath = NULL;
3153 size_t linesize = 0;
3154 ssize_t linelen;
3156 ignorelist = calloc(1, sizeof(*ignorelist));
3157 if (ignorelist == NULL)
3158 return got_error_from_errno("calloc");
3159 TAILQ_INIT(ignorelist);
3161 while ((linelen = getline(&line, &linesize, f)) != -1) {
3162 if (linelen > 0 && line[linelen - 1] == '\n')
3163 line[linelen - 1] = '\0';
3165 /* Git's ignores may contain comments. */
3166 if (line[0] == '#')
3167 continue;
3169 /* Git's negated patterns are not (yet?) supported. */
3170 if (line[0] == '!')
3171 continue;
3173 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3174 line) == -1) {
3175 err = got_error_from_errno("asprintf");
3176 goto done;
3178 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3179 if (err)
3180 goto done;
3182 if (ferror(f)) {
3183 err = got_error_from_errno("getline");
3184 goto done;
3187 dirpath = strdup(path);
3188 if (dirpath == NULL) {
3189 err = got_error_from_errno("strdup");
3190 goto done;
3192 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3193 done:
3194 free(line);
3195 if (err || pe == NULL) {
3196 free(dirpath);
3197 free_ignorelist(ignorelist);
3199 return err;
3202 int
3203 match_ignores(struct got_pathlist_head *ignores, const char *path)
3205 struct got_pathlist_entry *pe;
3207 /* Handle patterns which match in all directories. */
3208 TAILQ_FOREACH(pe, ignores, entry) {
3209 struct got_pathlist_head *ignorelist = pe->data;
3210 struct got_pathlist_entry *pi;
3212 TAILQ_FOREACH(pi, ignorelist, entry) {
3213 const char *p, *pattern = pi->path;
3215 if (strncmp(pattern, "**/", 3) != 0)
3216 continue;
3217 pattern += 3;
3218 p = path;
3219 while (*p) {
3220 if (fnmatch(pattern, p,
3221 FNM_PATHNAME | FNM_LEADING_DIR)) {
3222 /* Retry in next directory. */
3223 while (*p && *p != '/')
3224 p++;
3225 while (*p == '/')
3226 p++;
3227 continue;
3229 return 1;
3235 * The ignores pathlist contains ignore lists from children before
3236 * parents, so we can find the most specific ignorelist by walking
3237 * ignores backwards.
3239 pe = TAILQ_LAST(ignores, got_pathlist_head);
3240 while (pe) {
3241 if (got_path_is_child(path, pe->path, pe->path_len)) {
3242 struct got_pathlist_head *ignorelist = pe->data;
3243 struct got_pathlist_entry *pi;
3244 TAILQ_FOREACH(pi, ignorelist, entry) {
3245 const char *pattern = pi->path;
3246 int flags = FNM_LEADING_DIR;
3247 if (strstr(pattern, "/**/") == NULL)
3248 flags |= FNM_PATHNAME;
3249 if (fnmatch(pattern, path, flags))
3250 continue;
3251 return 1;
3254 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3257 return 0;
3260 static const struct got_error *
3261 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3262 const char *path, int dirfd, const char *ignores_filename)
3264 const struct got_error *err = NULL;
3265 char *ignorespath;
3266 int fd = -1;
3267 FILE *ignoresfile = NULL;
3269 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3270 path[0] ? "/" : "", ignores_filename) == -1)
3271 return got_error_from_errno("asprintf");
3273 if (dirfd != -1) {
3274 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3275 if (fd == -1) {
3276 if (errno != ENOENT && errno != EACCES)
3277 err = got_error_from_errno2("openat",
3278 ignorespath);
3279 } else {
3280 ignoresfile = fdopen(fd, "r");
3281 if (ignoresfile == NULL)
3282 err = got_error_from_errno2("fdopen",
3283 ignorespath);
3284 else {
3285 fd = -1;
3286 err = read_ignores(ignores, path, ignoresfile);
3289 } else {
3290 ignoresfile = fopen(ignorespath, "r");
3291 if (ignoresfile == NULL) {
3292 if (errno != ENOENT && errno != EACCES)
3293 err = got_error_from_errno2("fopen",
3294 ignorespath);
3295 } else
3296 err = read_ignores(ignores, path, ignoresfile);
3299 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3300 err = got_error_from_errno2("fclose", path);
3301 if (fd != -1 && close(fd) == -1 && err == NULL)
3302 err = got_error_from_errno2("close", path);
3303 free(ignorespath);
3304 return err;
3307 static const struct got_error *
3308 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3310 const struct got_error *err = NULL;
3311 struct diff_dir_cb_arg *a = arg;
3312 char *path = NULL;
3314 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3315 return got_error(GOT_ERR_CANCELLED);
3317 if (parent_path[0]) {
3318 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3319 return got_error_from_errno("asprintf");
3320 } else {
3321 path = de->d_name;
3324 if (de->d_type != DT_DIR &&
3325 got_path_is_child(path, a->status_path, a->status_path_len)
3326 && !match_ignores(&a->ignores, path))
3327 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3328 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3329 if (parent_path[0])
3330 free(path);
3331 return err;
3334 static const struct got_error *
3335 status_traverse(void *arg, const char *path, int dirfd)
3337 const struct got_error *err = NULL;
3338 struct diff_dir_cb_arg *a = arg;
3340 if (a->no_ignores)
3341 return NULL;
3343 err = add_ignores(&a->ignores, a->worktree->root_path,
3344 path, dirfd, ".cvsignore");
3345 if (err)
3346 return err;
3348 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3349 dirfd, ".gitignore");
3351 return err;
3354 static const struct got_error *
3355 report_single_file_status(const char *path, const char *ondisk_path,
3356 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3357 void *status_arg, struct got_repository *repo, int report_unchanged)
3359 struct got_fileindex_entry *ie;
3360 struct stat sb;
3362 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3363 if (ie)
3364 return report_file_status(ie, ondisk_path, -1, NULL,
3365 status_cb, status_arg, repo, report_unchanged);
3367 if (lstat(ondisk_path, &sb) == -1) {
3368 if (errno != ENOENT)
3369 return got_error_from_errno2("lstat", ondisk_path);
3370 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3371 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3372 return NULL;
3375 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3376 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3377 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3379 return NULL;
3382 static const struct got_error *
3383 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3384 const char *root_path, const char *path)
3386 const struct got_error *err;
3387 char *parent_path, *next_parent_path = NULL;
3389 err = add_ignores(ignores, root_path, "", -1,
3390 ".cvsignore");
3391 if (err)
3392 return err;
3394 err = add_ignores(ignores, root_path, "", -1,
3395 ".gitignore");
3396 if (err)
3397 return err;
3399 err = got_path_dirname(&parent_path, path);
3400 if (err) {
3401 if (err->code == GOT_ERR_BAD_PATH)
3402 return NULL; /* cannot traverse parent */
3403 return err;
3405 for (;;) {
3406 err = add_ignores(ignores, root_path, parent_path, -1,
3407 ".cvsignore");
3408 if (err)
3409 break;
3410 err = add_ignores(ignores, root_path, parent_path, -1,
3411 ".gitignore");
3412 if (err)
3413 break;
3414 err = got_path_dirname(&next_parent_path, parent_path);
3415 if (err) {
3416 if (err->code == GOT_ERR_BAD_PATH)
3417 err = NULL; /* traversed everything */
3418 break;
3420 free(parent_path);
3421 parent_path = next_parent_path;
3422 next_parent_path = NULL;
3425 free(parent_path);
3426 free(next_parent_path);
3427 return err;
3430 static const struct got_error *
3431 worktree_status(struct got_worktree *worktree, const char *path,
3432 struct got_fileindex *fileindex, struct got_repository *repo,
3433 got_worktree_status_cb status_cb, void *status_arg,
3434 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3435 int report_unchanged)
3437 const struct got_error *err = NULL;
3438 int fd = -1;
3439 struct got_fileindex_diff_dir_cb fdiff_cb;
3440 struct diff_dir_cb_arg arg;
3441 char *ondisk_path = NULL;
3443 TAILQ_INIT(&arg.ignores);
3445 if (asprintf(&ondisk_path, "%s%s%s",
3446 worktree->root_path, path[0] ? "/" : "", path) == -1)
3447 return got_error_from_errno("asprintf");
3449 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3450 if (fd == -1) {
3451 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3452 errno != ELOOP)
3453 err = got_error_from_errno2("open", ondisk_path);
3454 else
3455 err = report_single_file_status(path, ondisk_path,
3456 fileindex, status_cb, status_arg, repo,
3457 report_unchanged);
3458 } else {
3459 fdiff_cb.diff_old_new = status_old_new;
3460 fdiff_cb.diff_old = status_old;
3461 fdiff_cb.diff_new = status_new;
3462 fdiff_cb.diff_traverse = status_traverse;
3463 arg.fileindex = fileindex;
3464 arg.worktree = worktree;
3465 arg.status_path = path;
3466 arg.status_path_len = strlen(path);
3467 arg.repo = repo;
3468 arg.status_cb = status_cb;
3469 arg.status_arg = status_arg;
3470 arg.cancel_cb = cancel_cb;
3471 arg.cancel_arg = cancel_arg;
3472 arg.report_unchanged = report_unchanged;
3473 arg.no_ignores = no_ignores;
3474 if (!no_ignores) {
3475 err = add_ignores_from_parent_paths(&arg.ignores,
3476 worktree->root_path, path);
3477 if (err)
3478 goto done;
3480 err = got_fileindex_diff_dir(fileindex, fd,
3481 worktree->root_path, path, repo, &fdiff_cb, &arg);
3483 done:
3484 free_ignores(&arg.ignores);
3485 if (fd != -1 && close(fd) != 0 && err == NULL)
3486 err = got_error_from_errno("close");
3487 free(ondisk_path);
3488 return err;
3491 const struct got_error *
3492 got_worktree_status(struct got_worktree *worktree,
3493 struct got_pathlist_head *paths, struct got_repository *repo,
3494 got_worktree_status_cb status_cb, void *status_arg,
3495 got_cancel_cb cancel_cb, void *cancel_arg)
3497 const struct got_error *err = NULL;
3498 char *fileindex_path = NULL;
3499 struct got_fileindex *fileindex = NULL;
3500 struct got_pathlist_entry *pe;
3502 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3503 if (err)
3504 return err;
3506 TAILQ_FOREACH(pe, paths, entry) {
3507 err = worktree_status(worktree, pe->path, fileindex, repo,
3508 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3509 if (err)
3510 break;
3512 free(fileindex_path);
3513 got_fileindex_free(fileindex);
3514 return err;
3517 const struct got_error *
3518 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3519 const char *arg)
3521 const struct got_error *err = NULL;
3522 char *resolved = NULL, *cwd = NULL, *path = NULL;
3523 size_t len;
3524 struct stat sb;
3526 *wt_path = NULL;
3528 cwd = getcwd(NULL, 0);
3529 if (cwd == NULL)
3530 return got_error_from_errno("getcwd");
3532 if (lstat(arg, &sb) == -1) {
3533 if (errno != ENOENT) {
3534 err = got_error_from_errno2("lstat", arg);
3535 goto done;
3538 if (S_ISLNK(sb.st_mode)) {
3540 * We cannot use realpath(3) with symlinks since we want to
3541 * operate on the symlink itself.
3542 * But we can make the path absolute, assuming it is relative
3543 * to the current working directory, and then canonicalize it.
3545 char *abspath = NULL;
3546 char canonpath[PATH_MAX];
3547 if (!got_path_is_absolute(arg)) {
3548 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3549 err = got_error_from_errno("asprintf");
3550 goto done;
3554 err = got_canonpath(abspath ? abspath : arg, canonpath,
3555 sizeof(canonpath));
3556 if (err)
3557 goto done;
3558 resolved = strdup(canonpath);
3559 if (resolved == NULL) {
3560 err = got_error_from_errno("strdup");
3561 goto done;
3563 } else {
3564 resolved = realpath(arg, NULL);
3565 if (resolved == NULL) {
3566 if (errno != ENOENT) {
3567 err = got_error_from_errno2("realpath", arg);
3568 goto done;
3570 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3571 err = got_error_from_errno("asprintf");
3572 goto done;
3577 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3578 strlen(got_worktree_get_root_path(worktree)))) {
3579 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3580 goto done;
3583 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3584 err = got_path_skip_common_ancestor(&path,
3585 got_worktree_get_root_path(worktree), resolved);
3586 if (err)
3587 goto done;
3588 } else {
3589 path = strdup("");
3590 if (path == NULL) {
3591 err = got_error_from_errno("strdup");
3592 goto done;
3596 /* XXX status walk can't deal with trailing slash! */
3597 len = strlen(path);
3598 while (len > 0 && path[len - 1] == '/') {
3599 path[len - 1] = '\0';
3600 len--;
3602 done:
3603 free(resolved);
3604 free(cwd);
3605 if (err == NULL)
3606 *wt_path = path;
3607 else
3608 free(path);
3609 return err;
3612 struct schedule_addition_args {
3613 struct got_worktree *worktree;
3614 struct got_fileindex *fileindex;
3615 got_worktree_checkout_cb progress_cb;
3616 void *progress_arg;
3617 struct got_repository *repo;
3620 static const struct got_error *
3621 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3622 const char *relpath, struct got_object_id *blob_id,
3623 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3624 int dirfd, const char *de_name)
3626 struct schedule_addition_args *a = arg;
3627 const struct got_error *err = NULL;
3628 struct got_fileindex_entry *ie;
3629 struct stat sb;
3630 char *ondisk_path;
3632 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3633 relpath) == -1)
3634 return got_error_from_errno("asprintf");
3636 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3637 if (ie) {
3638 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3639 de_name, a->repo);
3640 if (err)
3641 goto done;
3642 /* Re-adding an existing entry is a no-op. */
3643 if (status == GOT_STATUS_ADD)
3644 goto done;
3645 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3646 if (err)
3647 goto done;
3650 if (status != GOT_STATUS_UNVERSIONED) {
3651 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3652 goto done;
3655 err = got_fileindex_entry_alloc(&ie, relpath);
3656 if (err)
3657 goto done;
3658 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3659 if (err) {
3660 got_fileindex_entry_free(ie);
3661 goto done;
3663 err = got_fileindex_entry_add(a->fileindex, ie);
3664 if (err) {
3665 got_fileindex_entry_free(ie);
3666 goto done;
3668 done:
3669 free(ondisk_path);
3670 if (err)
3671 return err;
3672 if (status == GOT_STATUS_ADD)
3673 return NULL;
3674 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3677 const struct got_error *
3678 got_worktree_schedule_add(struct got_worktree *worktree,
3679 struct got_pathlist_head *paths,
3680 got_worktree_checkout_cb progress_cb, void *progress_arg,
3681 struct got_repository *repo, int no_ignores)
3683 struct got_fileindex *fileindex = NULL;
3684 char *fileindex_path = NULL;
3685 const struct got_error *err = NULL, *sync_err, *unlockerr;
3686 struct got_pathlist_entry *pe;
3687 struct schedule_addition_args saa;
3689 err = lock_worktree(worktree, LOCK_EX);
3690 if (err)
3691 return err;
3693 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3694 if (err)
3695 goto done;
3697 saa.worktree = worktree;
3698 saa.fileindex = fileindex;
3699 saa.progress_cb = progress_cb;
3700 saa.progress_arg = progress_arg;
3701 saa.repo = repo;
3703 TAILQ_FOREACH(pe, paths, entry) {
3704 err = worktree_status(worktree, pe->path, fileindex, repo,
3705 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3706 if (err)
3707 break;
3709 sync_err = sync_fileindex(fileindex, fileindex_path);
3710 if (sync_err && err == NULL)
3711 err = sync_err;
3712 done:
3713 free(fileindex_path);
3714 if (fileindex)
3715 got_fileindex_free(fileindex);
3716 unlockerr = lock_worktree(worktree, LOCK_SH);
3717 if (unlockerr && err == NULL)
3718 err = unlockerr;
3719 return err;
3722 struct schedule_deletion_args {
3723 struct got_worktree *worktree;
3724 struct got_fileindex *fileindex;
3725 got_worktree_delete_cb progress_cb;
3726 void *progress_arg;
3727 struct got_repository *repo;
3728 int delete_local_mods;
3729 int keep_on_disk;
3732 static const struct got_error *
3733 schedule_for_deletion(void *arg, unsigned char status,
3734 unsigned char staged_status, const char *relpath,
3735 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3736 struct got_object_id *commit_id, int dirfd, const char *de_name)
3738 struct schedule_deletion_args *a = arg;
3739 const struct got_error *err = NULL;
3740 struct got_fileindex_entry *ie = NULL;
3741 struct stat sb;
3742 char *ondisk_path, *parent = NULL;
3744 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3745 if (ie == NULL)
3746 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3748 staged_status = get_staged_status(ie);
3749 if (staged_status != GOT_STATUS_NO_CHANGE) {
3750 if (staged_status == GOT_STATUS_DELETE)
3751 return NULL;
3752 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3755 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3756 relpath) == -1)
3757 return got_error_from_errno("asprintf");
3759 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3760 a->repo);
3761 if (err)
3762 goto done;
3764 if (status != GOT_STATUS_NO_CHANGE) {
3765 if (status == GOT_STATUS_DELETE)
3766 goto done;
3767 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3768 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3769 goto done;
3771 if (status != GOT_STATUS_MODIFY &&
3772 status != GOT_STATUS_MISSING) {
3773 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3774 goto done;
3778 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3779 if (dirfd != -1) {
3780 if (unlinkat(dirfd, de_name, 0) != 0) {
3781 err = got_error_from_errno2("unlinkat",
3782 ondisk_path);
3783 goto done;
3785 } else if (unlink(ondisk_path) != 0) {
3786 err = got_error_from_errno2("unlink", ondisk_path);
3787 goto done;
3790 parent = dirname(ondisk_path);
3792 if (parent == NULL) {
3793 err = got_error_from_errno2("dirname", ondisk_path);
3794 goto done;
3796 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3797 if (rmdir(parent) == -1) {
3798 if (errno != ENOTEMPTY)
3799 err = got_error_from_errno2("rmdir",
3800 parent);
3801 break;
3803 parent = dirname(parent);
3804 if (parent == NULL) {
3805 err = got_error_from_errno2("dirname", parent);
3806 goto done;
3811 got_fileindex_entry_mark_deleted_from_disk(ie);
3812 done:
3813 free(ondisk_path);
3814 if (err)
3815 return err;
3816 if (status == GOT_STATUS_DELETE)
3817 return NULL;
3818 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3819 staged_status, relpath);
3822 const struct got_error *
3823 got_worktree_schedule_delete(struct got_worktree *worktree,
3824 struct got_pathlist_head *paths, int delete_local_mods,
3825 got_worktree_delete_cb progress_cb, void *progress_arg,
3826 struct got_repository *repo, int keep_on_disk)
3828 struct got_fileindex *fileindex = NULL;
3829 char *fileindex_path = NULL;
3830 const struct got_error *err = NULL, *sync_err, *unlockerr;
3831 struct got_pathlist_entry *pe;
3832 struct schedule_deletion_args sda;
3834 err = lock_worktree(worktree, LOCK_EX);
3835 if (err)
3836 return err;
3838 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3839 if (err)
3840 goto done;
3842 sda.worktree = worktree;
3843 sda.fileindex = fileindex;
3844 sda.progress_cb = progress_cb;
3845 sda.progress_arg = progress_arg;
3846 sda.repo = repo;
3847 sda.delete_local_mods = delete_local_mods;
3848 sda.keep_on_disk = keep_on_disk;
3850 TAILQ_FOREACH(pe, paths, entry) {
3851 err = worktree_status(worktree, pe->path, fileindex, repo,
3852 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3853 if (err)
3854 break;
3856 sync_err = sync_fileindex(fileindex, fileindex_path);
3857 if (sync_err && err == NULL)
3858 err = sync_err;
3859 done:
3860 free(fileindex_path);
3861 if (fileindex)
3862 got_fileindex_free(fileindex);
3863 unlockerr = lock_worktree(worktree, LOCK_SH);
3864 if (unlockerr && err == NULL)
3865 err = unlockerr;
3866 return err;
3869 static const struct got_error *
3870 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3872 const struct got_error *err = NULL;
3873 char *line = NULL;
3874 size_t linesize = 0, n;
3875 ssize_t linelen;
3877 linelen = getline(&line, &linesize, infile);
3878 if (linelen == -1) {
3879 if (ferror(infile)) {
3880 err = got_error_from_errno("getline");
3881 goto done;
3883 return NULL;
3885 if (outfile) {
3886 n = fwrite(line, 1, linelen, outfile);
3887 if (n != linelen) {
3888 err = got_ferror(outfile, GOT_ERR_IO);
3889 goto done;
3892 if (rejectfile) {
3893 n = fwrite(line, 1, linelen, rejectfile);
3894 if (n != linelen)
3895 err = got_ferror(outfile, GOT_ERR_IO);
3897 done:
3898 free(line);
3899 return err;
3902 static const struct got_error *
3903 skip_one_line(FILE *f)
3905 char *line = NULL;
3906 size_t linesize = 0;
3907 ssize_t linelen;
3909 linelen = getline(&line, &linesize, f);
3910 if (linelen == -1) {
3911 if (ferror(f))
3912 return got_error_from_errno("getline");
3913 return NULL;
3915 free(line);
3916 return NULL;
3919 static const struct got_error *
3920 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3921 int start_old, int end_old, int start_new, int end_new,
3922 FILE *outfile, FILE *rejectfile)
3924 const struct got_error *err;
3926 /* Copy old file's lines leading up to patch. */
3927 while (!feof(f1) && *line_cur1 < start_old) {
3928 err = copy_one_line(f1, outfile, NULL);
3929 if (err)
3930 return err;
3931 (*line_cur1)++;
3933 /* Skip new file's lines leading up to patch. */
3934 while (!feof(f2) && *line_cur2 < start_new) {
3935 if (rejectfile)
3936 err = copy_one_line(f2, NULL, rejectfile);
3937 else
3938 err = skip_one_line(f2);
3939 if (err)
3940 return err;
3941 (*line_cur2)++;
3943 /* Copy patched lines. */
3944 while (!feof(f2) && *line_cur2 <= end_new) {
3945 err = copy_one_line(f2, outfile, NULL);
3946 if (err)
3947 return err;
3948 (*line_cur2)++;
3950 /* Skip over old file's replaced lines. */
3951 while (!feof(f1) && *line_cur1 <= end_old) {
3952 if (rejectfile)
3953 err = copy_one_line(f1, NULL, rejectfile);
3954 else
3955 err = skip_one_line(f1);
3956 if (err)
3957 return err;
3958 (*line_cur1)++;
3961 return NULL;
3964 static const struct got_error *
3965 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3966 FILE *outfile, FILE *rejectfile)
3968 const struct got_error *err;
3970 if (outfile) {
3971 /* Copy old file's lines until EOF. */
3972 while (!feof(f1)) {
3973 err = copy_one_line(f1, outfile, NULL);
3974 if (err)
3975 return err;
3976 (*line_cur1)++;
3979 if (rejectfile) {
3980 /* Copy new file's lines until EOF. */
3981 while (!feof(f2)) {
3982 err = copy_one_line(f2, NULL, rejectfile);
3983 if (err)
3984 return err;
3985 (*line_cur2)++;
3989 return NULL;
3992 static const struct got_error *
3993 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3994 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3995 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3996 int *line_cur2, FILE *outfile, FILE *rejectfile,
3997 got_worktree_patch_cb patch_cb, void *patch_arg)
3999 const struct got_error *err = NULL;
4000 int start_old = change->cv.a;
4001 int end_old = change->cv.b;
4002 int start_new = change->cv.c;
4003 int end_new = change->cv.d;
4004 long pos1, pos2;
4005 FILE *hunkfile;
4007 *choice = GOT_PATCH_CHOICE_NONE;
4009 hunkfile = got_opentemp();
4010 if (hunkfile == NULL)
4011 return got_error_from_errno("got_opentemp");
4013 pos1 = ftell(f1);
4014 pos2 = ftell(f2);
4016 /* XXX TODO needs error checking */
4017 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4019 if (fseek(f1, pos1, SEEK_SET) == -1) {
4020 err = got_ferror(f1, GOT_ERR_IO);
4021 goto done;
4023 if (fseek(f2, pos2, SEEK_SET) == -1) {
4024 err = got_ferror(f1, GOT_ERR_IO);
4025 goto done;
4027 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4028 err = got_ferror(hunkfile, GOT_ERR_IO);
4029 goto done;
4032 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4033 hunkfile, n, nchanges);
4034 if (err)
4035 goto done;
4037 switch (*choice) {
4038 case GOT_PATCH_CHOICE_YES:
4039 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4040 end_old, start_new, end_new, outfile, rejectfile);
4041 break;
4042 case GOT_PATCH_CHOICE_NO:
4043 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4044 end_old, start_new, end_new, rejectfile, outfile);
4045 break;
4046 case GOT_PATCH_CHOICE_QUIT:
4047 break;
4048 default:
4049 err = got_error(GOT_ERR_PATCH_CHOICE);
4050 break;
4052 done:
4053 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4054 err = got_error_from_errno("fclose");
4055 return err;
4058 struct revert_file_args {
4059 struct got_worktree *worktree;
4060 struct got_fileindex *fileindex;
4061 got_worktree_checkout_cb progress_cb;
4062 void *progress_arg;
4063 got_worktree_patch_cb patch_cb;
4064 void *patch_arg;
4065 struct got_repository *repo;
4068 static const struct got_error *
4069 create_patched_content(char **path_outfile, int reverse_patch,
4070 struct got_object_id *blob_id, const char *path2,
4071 int dirfd2, const char *de_name2,
4072 const char *relpath, struct got_repository *repo,
4073 got_worktree_patch_cb patch_cb, void *patch_arg)
4075 const struct got_error *err;
4076 struct got_blob_object *blob = NULL;
4077 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4078 int fd2 = -1;
4079 char *path1 = NULL, *id_str = NULL;
4080 struct stat sb1, sb2;
4081 struct got_diff_changes *changes = NULL;
4082 struct got_diff_state *ds = NULL;
4083 struct got_diff_args *args = NULL;
4084 struct got_diff_change *change;
4085 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4086 int n = 0;
4088 *path_outfile = NULL;
4090 err = got_object_id_str(&id_str, blob_id);
4091 if (err)
4092 return err;
4094 if (dirfd2 != -1) {
4095 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4096 if (fd2 == -1) {
4097 err = got_error_from_errno2("openat", path2);
4098 goto done;
4100 } else {
4101 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4102 if (fd2 == -1) {
4103 err = got_error_from_errno2("open", path2);
4104 goto done;
4107 if (fstat(fd2, &sb2) == -1) {
4108 err = got_error_from_errno2("fstat", path2);
4109 goto done;
4112 f2 = fdopen(fd2, "r");
4113 if (f2 == NULL) {
4114 err = got_error_from_errno2("fdopen", path2);
4115 goto done;
4117 fd2 = -1;
4119 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4120 if (err)
4121 goto done;
4123 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4124 if (err)
4125 goto done;
4127 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4128 if (err)
4129 goto done;
4131 if (stat(path1, &sb1) == -1) {
4132 err = got_error_from_errno2("stat", path1);
4133 goto done;
4136 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4137 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4138 if (err)
4139 goto done;
4141 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4142 if (err)
4143 goto done;
4145 if (fseek(f1, 0L, SEEK_SET) == -1)
4146 return got_ferror(f1, GOT_ERR_IO);
4147 if (fseek(f2, 0L, SEEK_SET) == -1)
4148 return got_ferror(f2, GOT_ERR_IO);
4149 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4150 int choice;
4151 err = apply_or_reject_change(&choice, change, ++n,
4152 changes->nchanges, ds, args, diff_flags, relpath,
4153 f1, f2, &line_cur1, &line_cur2,
4154 reverse_patch ? NULL : outfile,
4155 reverse_patch ? outfile : NULL,
4156 patch_cb, patch_arg);
4157 if (err)
4158 goto done;
4159 if (choice == GOT_PATCH_CHOICE_YES)
4160 have_content = 1;
4161 else if (choice == GOT_PATCH_CHOICE_QUIT)
4162 break;
4164 if (have_content) {
4165 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4166 reverse_patch ? NULL : outfile,
4167 reverse_patch ? outfile : NULL);
4168 if (err)
4169 goto done;
4171 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4172 err = got_error_from_errno2("chmod", path2);
4173 goto done;
4176 done:
4177 free(id_str);
4178 if (blob)
4179 got_object_blob_close(blob);
4180 if (f1 && fclose(f1) == EOF && err == NULL)
4181 err = got_error_from_errno2("fclose", path1);
4182 if (f2 && fclose(f2) == EOF && err == NULL)
4183 err = got_error_from_errno2("fclose", path2);
4184 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4185 err = got_error_from_errno2("close", path2);
4186 if (outfile && fclose(outfile) == EOF && err == NULL)
4187 err = got_error_from_errno2("fclose", *path_outfile);
4188 if (path1 && unlink(path1) == -1 && err == NULL)
4189 err = got_error_from_errno2("unlink", path1);
4190 if (err || !have_content) {
4191 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4192 err = got_error_from_errno2("unlink", *path_outfile);
4193 free(*path_outfile);
4194 *path_outfile = NULL;
4196 free(args);
4197 if (ds) {
4198 got_diff_state_free(ds);
4199 free(ds);
4201 if (changes)
4202 got_diff_free_changes(changes);
4203 free(path1);
4204 return err;
4207 static const struct got_error *
4208 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4209 const char *relpath, struct got_object_id *blob_id,
4210 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4211 int dirfd, const char *de_name)
4213 struct revert_file_args *a = arg;
4214 const struct got_error *err = NULL;
4215 char *parent_path = NULL;
4216 struct got_fileindex_entry *ie;
4217 struct got_tree_object *tree = NULL;
4218 struct got_object_id *tree_id = NULL;
4219 const struct got_tree_entry *te = NULL;
4220 char *tree_path = NULL, *te_name;
4221 char *ondisk_path = NULL, *path_content = NULL;
4222 struct got_blob_object *blob = NULL;
4224 /* Reverting a staged deletion is a no-op. */
4225 if (status == GOT_STATUS_DELETE &&
4226 staged_status != GOT_STATUS_NO_CHANGE)
4227 return NULL;
4229 if (status == GOT_STATUS_UNVERSIONED)
4230 return (*a->progress_cb)(a->progress_arg,
4231 GOT_STATUS_UNVERSIONED, relpath);
4233 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4234 if (ie == NULL)
4235 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4237 /* Construct in-repository path of tree which contains this blob. */
4238 err = got_path_dirname(&parent_path, ie->path);
4239 if (err) {
4240 if (err->code != GOT_ERR_BAD_PATH)
4241 goto done;
4242 parent_path = strdup("/");
4243 if (parent_path == NULL) {
4244 err = got_error_from_errno("strdup");
4245 goto done;
4248 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4249 tree_path = strdup(parent_path);
4250 if (tree_path == NULL) {
4251 err = got_error_from_errno("strdup");
4252 goto done;
4254 } else {
4255 if (got_path_is_root_dir(parent_path)) {
4256 tree_path = strdup(a->worktree->path_prefix);
4257 if (tree_path == NULL) {
4258 err = got_error_from_errno("strdup");
4259 goto done;
4261 } else {
4262 if (asprintf(&tree_path, "%s/%s",
4263 a->worktree->path_prefix, parent_path) == -1) {
4264 err = got_error_from_errno("asprintf");
4265 goto done;
4270 err = got_object_id_by_path(&tree_id, a->repo,
4271 a->worktree->base_commit_id, tree_path);
4272 if (err) {
4273 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4274 (status == GOT_STATUS_ADD ||
4275 staged_status == GOT_STATUS_ADD)))
4276 goto done;
4277 } else {
4278 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4279 if (err)
4280 goto done;
4282 te_name = basename(ie->path);
4283 if (te_name == NULL) {
4284 err = got_error_from_errno2("basename", ie->path);
4285 goto done;
4288 te = got_object_tree_find_entry(tree, te_name);
4289 if (te == NULL && status != GOT_STATUS_ADD &&
4290 staged_status != GOT_STATUS_ADD) {
4291 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4292 goto done;
4296 switch (status) {
4297 case GOT_STATUS_ADD:
4298 if (a->patch_cb) {
4299 int choice = GOT_PATCH_CHOICE_NONE;
4300 err = (*a->patch_cb)(&choice, a->patch_arg,
4301 status, ie->path, NULL, 1, 1);
4302 if (err)
4303 goto done;
4304 if (choice != GOT_PATCH_CHOICE_YES)
4305 break;
4307 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4308 ie->path);
4309 if (err)
4310 goto done;
4311 got_fileindex_entry_remove(a->fileindex, ie);
4312 break;
4313 case GOT_STATUS_DELETE:
4314 if (a->patch_cb) {
4315 int choice = GOT_PATCH_CHOICE_NONE;
4316 err = (*a->patch_cb)(&choice, a->patch_arg,
4317 status, ie->path, NULL, 1, 1);
4318 if (err)
4319 goto done;
4320 if (choice != GOT_PATCH_CHOICE_YES)
4321 break;
4323 /* fall through */
4324 case GOT_STATUS_MODIFY:
4325 case GOT_STATUS_MODE_CHANGE:
4326 case GOT_STATUS_CONFLICT:
4327 case GOT_STATUS_MISSING: {
4328 struct got_object_id id;
4329 if (staged_status == GOT_STATUS_ADD ||
4330 staged_status == GOT_STATUS_MODIFY) {
4331 memcpy(id.sha1, ie->staged_blob_sha1,
4332 SHA1_DIGEST_LENGTH);
4333 } else
4334 memcpy(id.sha1, ie->blob_sha1,
4335 SHA1_DIGEST_LENGTH);
4336 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4337 if (err)
4338 goto done;
4340 if (asprintf(&ondisk_path, "%s/%s",
4341 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4342 err = got_error_from_errno("asprintf");
4343 goto done;
4346 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4347 status == GOT_STATUS_CONFLICT)) {
4348 err = create_patched_content(&path_content, 1, &id,
4349 ondisk_path, dirfd, de_name, ie->path, a->repo,
4350 a->patch_cb, a->patch_arg);
4351 if (err || path_content == NULL)
4352 break;
4353 if (rename(path_content, ondisk_path) == -1) {
4354 err = got_error_from_errno3("rename",
4355 path_content, ondisk_path);
4356 goto done;
4358 } else {
4359 int is_bad_symlink = 0;
4360 if (te && S_ISLNK(te->mode)) {
4361 err = install_symlink(&is_bad_symlink,
4362 a->worktree, ondisk_path, ie->path,
4363 blob, 0, 1, 0, a->repo,
4364 a->progress_cb, a->progress_arg);
4365 } else {
4366 err = install_blob(a->worktree, ondisk_path,
4367 ie->path,
4368 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4369 got_fileindex_perms_to_st(ie), blob,
4370 0, 1, 0, 0, a->repo,
4371 a->progress_cb, a->progress_arg);
4373 if (err)
4374 goto done;
4375 if (status == GOT_STATUS_DELETE ||
4376 status == GOT_STATUS_MODE_CHANGE) {
4377 err = got_fileindex_entry_update(ie,
4378 ondisk_path, blob->id.sha1,
4379 a->worktree->base_commit_id->sha1, 1);
4380 if (err)
4381 goto done;
4383 if (is_bad_symlink) {
4384 err = got_fileindex_entry_filetype_set(ie,
4385 GOT_FILEIDX_MODE_BAD_SYMLINK);
4386 if (err)
4387 goto done;
4390 break;
4392 default:
4393 break;
4395 done:
4396 free(ondisk_path);
4397 free(path_content);
4398 free(parent_path);
4399 free(tree_path);
4400 if (blob)
4401 got_object_blob_close(blob);
4402 if (tree)
4403 got_object_tree_close(tree);
4404 free(tree_id);
4405 return err;
4408 const struct got_error *
4409 got_worktree_revert(struct got_worktree *worktree,
4410 struct got_pathlist_head *paths,
4411 got_worktree_checkout_cb progress_cb, void *progress_arg,
4412 got_worktree_patch_cb patch_cb, void *patch_arg,
4413 struct got_repository *repo)
4415 struct got_fileindex *fileindex = NULL;
4416 char *fileindex_path = NULL;
4417 const struct got_error *err = NULL, *unlockerr = NULL;
4418 const struct got_error *sync_err = NULL;
4419 struct got_pathlist_entry *pe;
4420 struct revert_file_args rfa;
4422 err = lock_worktree(worktree, LOCK_EX);
4423 if (err)
4424 return err;
4426 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4427 if (err)
4428 goto done;
4430 rfa.worktree = worktree;
4431 rfa.fileindex = fileindex;
4432 rfa.progress_cb = progress_cb;
4433 rfa.progress_arg = progress_arg;
4434 rfa.patch_cb = patch_cb;
4435 rfa.patch_arg = patch_arg;
4436 rfa.repo = repo;
4437 TAILQ_FOREACH(pe, paths, entry) {
4438 err = worktree_status(worktree, pe->path, fileindex, repo,
4439 revert_file, &rfa, NULL, NULL, 0, 0);
4440 if (err)
4441 break;
4443 sync_err = sync_fileindex(fileindex, fileindex_path);
4444 if (sync_err && err == NULL)
4445 err = sync_err;
4446 done:
4447 free(fileindex_path);
4448 if (fileindex)
4449 got_fileindex_free(fileindex);
4450 unlockerr = lock_worktree(worktree, LOCK_SH);
4451 if (unlockerr && err == NULL)
4452 err = unlockerr;
4453 return err;
4456 static void
4457 free_commitable(struct got_commitable *ct)
4459 free(ct->path);
4460 free(ct->in_repo_path);
4461 free(ct->ondisk_path);
4462 free(ct->blob_id);
4463 free(ct->base_blob_id);
4464 free(ct->staged_blob_id);
4465 free(ct->base_commit_id);
4466 free(ct);
4469 struct collect_commitables_arg {
4470 struct got_pathlist_head *commitable_paths;
4471 struct got_repository *repo;
4472 struct got_worktree *worktree;
4473 int have_staged_files;
4476 static const struct got_error *
4477 collect_commitables(void *arg, unsigned char status,
4478 unsigned char staged_status, const char *relpath,
4479 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4480 struct got_object_id *commit_id, int dirfd, const char *de_name)
4482 struct collect_commitables_arg *a = arg;
4483 const struct got_error *err = NULL;
4484 struct got_commitable *ct = NULL;
4485 struct got_pathlist_entry *new = NULL;
4486 char *parent_path = NULL, *path = NULL;
4487 struct stat sb;
4489 if (a->have_staged_files) {
4490 if (staged_status != GOT_STATUS_MODIFY &&
4491 staged_status != GOT_STATUS_ADD &&
4492 staged_status != GOT_STATUS_DELETE)
4493 return NULL;
4494 } else {
4495 if (status == GOT_STATUS_CONFLICT)
4496 return got_error(GOT_ERR_COMMIT_CONFLICT);
4498 if (status != GOT_STATUS_MODIFY &&
4499 status != GOT_STATUS_MODE_CHANGE &&
4500 status != GOT_STATUS_ADD &&
4501 status != GOT_STATUS_DELETE)
4502 return NULL;
4505 if (asprintf(&path, "/%s", relpath) == -1) {
4506 err = got_error_from_errno("asprintf");
4507 goto done;
4509 if (strcmp(path, "/") == 0) {
4510 parent_path = strdup("");
4511 if (parent_path == NULL)
4512 return got_error_from_errno("strdup");
4513 } else {
4514 err = got_path_dirname(&parent_path, path);
4515 if (err)
4516 return err;
4519 ct = calloc(1, sizeof(*ct));
4520 if (ct == NULL) {
4521 err = got_error_from_errno("calloc");
4522 goto done;
4525 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4526 relpath) == -1) {
4527 err = got_error_from_errno("asprintf");
4528 goto done;
4530 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4531 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4532 } else {
4533 if (dirfd != -1) {
4534 if (fstatat(dirfd, de_name, &sb,
4535 AT_SYMLINK_NOFOLLOW) == -1) {
4536 err = got_error_from_errno2("fstatat",
4537 ct->ondisk_path);
4538 goto done;
4540 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4541 err = got_error_from_errno2("lstat", ct->ondisk_path);
4542 goto done;
4544 ct->mode = sb.st_mode;
4547 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4548 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4549 relpath) == -1) {
4550 err = got_error_from_errno("asprintf");
4551 goto done;
4554 ct->status = status;
4555 ct->staged_status = staged_status;
4556 ct->blob_id = NULL; /* will be filled in when blob gets created */
4557 if (ct->status != GOT_STATUS_ADD &&
4558 ct->staged_status != GOT_STATUS_ADD) {
4559 ct->base_blob_id = got_object_id_dup(blob_id);
4560 if (ct->base_blob_id == NULL) {
4561 err = got_error_from_errno("got_object_id_dup");
4562 goto done;
4564 ct->base_commit_id = got_object_id_dup(commit_id);
4565 if (ct->base_commit_id == NULL) {
4566 err = got_error_from_errno("got_object_id_dup");
4567 goto done;
4570 if (ct->staged_status == GOT_STATUS_ADD ||
4571 ct->staged_status == GOT_STATUS_MODIFY) {
4572 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4573 if (ct->staged_blob_id == NULL) {
4574 err = got_error_from_errno("got_object_id_dup");
4575 goto done;
4578 ct->path = strdup(path);
4579 if (ct->path == NULL) {
4580 err = got_error_from_errno("strdup");
4581 goto done;
4583 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4584 done:
4585 if (ct && (err || new == NULL))
4586 free_commitable(ct);
4587 free(parent_path);
4588 free(path);
4589 return err;
4592 static const struct got_error *write_tree(struct got_object_id **, int *,
4593 struct got_tree_object *, const char *, struct got_pathlist_head *,
4594 got_worktree_status_cb status_cb, void *status_arg,
4595 struct got_repository *);
4597 static const struct got_error *
4598 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4599 struct got_tree_entry *te, const char *parent_path,
4600 struct got_pathlist_head *commitable_paths,
4601 got_worktree_status_cb status_cb, void *status_arg,
4602 struct got_repository *repo)
4604 const struct got_error *err = NULL;
4605 struct got_tree_object *subtree;
4606 char *subpath;
4608 if (asprintf(&subpath, "%s%s%s", parent_path,
4609 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4610 return got_error_from_errno("asprintf");
4612 err = got_object_open_as_tree(&subtree, repo, &te->id);
4613 if (err)
4614 return err;
4616 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4617 commitable_paths, status_cb, status_arg, repo);
4618 got_object_tree_close(subtree);
4619 free(subpath);
4620 return err;
4623 static const struct got_error *
4624 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4626 const struct got_error *err = NULL;
4627 char *ct_parent_path = NULL;
4629 *match = 0;
4631 if (strchr(ct->in_repo_path, '/') == NULL) {
4632 *match = got_path_is_root_dir(path);
4633 return NULL;
4636 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4637 if (err)
4638 return err;
4639 *match = (strcmp(path, ct_parent_path) == 0);
4640 free(ct_parent_path);
4641 return err;
4644 static mode_t
4645 get_ct_file_mode(struct got_commitable *ct)
4647 if (S_ISLNK(ct->mode))
4648 return S_IFLNK;
4650 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4653 static const struct got_error *
4654 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4655 struct got_tree_entry *te, struct got_commitable *ct)
4657 const struct got_error *err = NULL;
4659 *new_te = NULL;
4661 err = got_object_tree_entry_dup(new_te, te);
4662 if (err)
4663 goto done;
4665 (*new_te)->mode = get_ct_file_mode(ct);
4667 if (ct->staged_status == GOT_STATUS_MODIFY)
4668 memcpy(&(*new_te)->id, ct->staged_blob_id,
4669 sizeof((*new_te)->id));
4670 else
4671 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4672 done:
4673 if (err && *new_te) {
4674 free(*new_te);
4675 *new_te = NULL;
4677 return err;
4680 static const struct got_error *
4681 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4682 struct got_commitable *ct)
4684 const struct got_error *err = NULL;
4685 char *ct_name;
4687 *new_te = NULL;
4689 *new_te = calloc(1, sizeof(**new_te));
4690 if (*new_te == NULL)
4691 return got_error_from_errno("calloc");
4693 ct_name = basename(ct->path);
4694 if (ct_name == NULL) {
4695 err = got_error_from_errno2("basename", ct->path);
4696 goto done;
4698 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4699 sizeof((*new_te)->name)) {
4700 err = got_error(GOT_ERR_NO_SPACE);
4701 goto done;
4704 (*new_te)->mode = get_ct_file_mode(ct);
4706 if (ct->staged_status == GOT_STATUS_ADD)
4707 memcpy(&(*new_te)->id, ct->staged_blob_id,
4708 sizeof((*new_te)->id));
4709 else
4710 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4711 done:
4712 if (err && *new_te) {
4713 free(*new_te);
4714 *new_te = NULL;
4716 return err;
4719 static const struct got_error *
4720 insert_tree_entry(struct got_tree_entry *new_te,
4721 struct got_pathlist_head *paths)
4723 const struct got_error *err = NULL;
4724 struct got_pathlist_entry *new_pe;
4726 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4727 if (err)
4728 return err;
4729 if (new_pe == NULL)
4730 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4731 return NULL;
4734 static const struct got_error *
4735 report_ct_status(struct got_commitable *ct,
4736 got_worktree_status_cb status_cb, void *status_arg)
4738 const char *ct_path = ct->path;
4739 unsigned char status;
4741 while (ct_path[0] == '/')
4742 ct_path++;
4744 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4745 status = ct->staged_status;
4746 else
4747 status = ct->status;
4749 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4750 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4753 static const struct got_error *
4754 match_modified_subtree(int *modified, struct got_tree_entry *te,
4755 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4757 const struct got_error *err = NULL;
4758 struct got_pathlist_entry *pe;
4759 char *te_path;
4761 *modified = 0;
4763 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4764 got_path_is_root_dir(base_tree_path) ? "" : "/",
4765 te->name) == -1)
4766 return got_error_from_errno("asprintf");
4768 TAILQ_FOREACH(pe, commitable_paths, entry) {
4769 struct got_commitable *ct = pe->data;
4770 *modified = got_path_is_child(ct->in_repo_path, te_path,
4771 strlen(te_path));
4772 if (*modified)
4773 break;
4776 free(te_path);
4777 return err;
4780 static const struct got_error *
4781 match_deleted_or_modified_ct(struct got_commitable **ctp,
4782 struct got_tree_entry *te, const char *base_tree_path,
4783 struct got_pathlist_head *commitable_paths)
4785 const struct got_error *err = NULL;
4786 struct got_pathlist_entry *pe;
4788 *ctp = NULL;
4790 TAILQ_FOREACH(pe, commitable_paths, entry) {
4791 struct got_commitable *ct = pe->data;
4792 char *ct_name = NULL;
4793 int path_matches;
4795 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4796 if (ct->status != GOT_STATUS_MODIFY &&
4797 ct->status != GOT_STATUS_MODE_CHANGE &&
4798 ct->status != GOT_STATUS_DELETE)
4799 continue;
4800 } else {
4801 if (ct->staged_status != GOT_STATUS_MODIFY &&
4802 ct->staged_status != GOT_STATUS_DELETE)
4803 continue;
4806 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4807 continue;
4809 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4810 if (err)
4811 return err;
4812 if (!path_matches)
4813 continue;
4815 ct_name = basename(pe->path);
4816 if (ct_name == NULL)
4817 return got_error_from_errno2("basename", pe->path);
4819 if (strcmp(te->name, ct_name) != 0)
4820 continue;
4822 *ctp = ct;
4823 break;
4826 return err;
4829 static const struct got_error *
4830 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4831 const char *child_path, const char *path_base_tree,
4832 struct got_pathlist_head *commitable_paths,
4833 got_worktree_status_cb status_cb, void *status_arg,
4834 struct got_repository *repo)
4836 const struct got_error *err = NULL;
4837 struct got_tree_entry *new_te;
4838 char *subtree_path;
4839 struct got_object_id *id = NULL;
4840 int nentries;
4842 *new_tep = NULL;
4844 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4845 got_path_is_root_dir(path_base_tree) ? "" : "/",
4846 child_path) == -1)
4847 return got_error_from_errno("asprintf");
4849 new_te = calloc(1, sizeof(*new_te));
4850 if (new_te == NULL)
4851 return got_error_from_errno("calloc");
4852 new_te->mode = S_IFDIR;
4854 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4855 sizeof(new_te->name)) {
4856 err = got_error(GOT_ERR_NO_SPACE);
4857 goto done;
4859 err = write_tree(&id, &nentries, NULL, subtree_path,
4860 commitable_paths, status_cb, status_arg, repo);
4861 if (err) {
4862 free(new_te);
4863 goto done;
4865 memcpy(&new_te->id, id, sizeof(new_te->id));
4866 done:
4867 free(id);
4868 free(subtree_path);
4869 if (err == NULL)
4870 *new_tep = new_te;
4871 return err;
4874 static const struct got_error *
4875 write_tree(struct got_object_id **new_tree_id, int *nentries,
4876 struct got_tree_object *base_tree, const char *path_base_tree,
4877 struct got_pathlist_head *commitable_paths,
4878 got_worktree_status_cb status_cb, void *status_arg,
4879 struct got_repository *repo)
4881 const struct got_error *err = NULL;
4882 struct got_pathlist_head paths;
4883 struct got_tree_entry *te, *new_te = NULL;
4884 struct got_pathlist_entry *pe;
4886 TAILQ_INIT(&paths);
4887 *nentries = 0;
4889 /* Insert, and recurse into, newly added entries first. */
4890 TAILQ_FOREACH(pe, commitable_paths, entry) {
4891 struct got_commitable *ct = pe->data;
4892 char *child_path = NULL, *slash;
4894 if ((ct->status != GOT_STATUS_ADD &&
4895 ct->staged_status != GOT_STATUS_ADD) ||
4896 (ct->flags & GOT_COMMITABLE_ADDED))
4897 continue;
4899 if (!got_path_is_child(pe->path, path_base_tree,
4900 strlen(path_base_tree)))
4901 continue;
4903 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4904 pe->path);
4905 if (err)
4906 goto done;
4908 slash = strchr(child_path, '/');
4909 if (slash == NULL) {
4910 err = alloc_added_blob_tree_entry(&new_te, ct);
4911 if (err)
4912 goto done;
4913 err = report_ct_status(ct, status_cb, status_arg);
4914 if (err)
4915 goto done;
4916 ct->flags |= GOT_COMMITABLE_ADDED;
4917 err = insert_tree_entry(new_te, &paths);
4918 if (err)
4919 goto done;
4920 (*nentries)++;
4921 } else {
4922 *slash = '\0'; /* trim trailing path components */
4923 if (base_tree == NULL ||
4924 got_object_tree_find_entry(base_tree, child_path)
4925 == NULL) {
4926 err = make_subtree_for_added_blob(&new_te,
4927 child_path, path_base_tree,
4928 commitable_paths, status_cb, status_arg,
4929 repo);
4930 if (err)
4931 goto done;
4932 err = insert_tree_entry(new_te, &paths);
4933 if (err)
4934 goto done;
4935 (*nentries)++;
4940 if (base_tree) {
4941 int i, nbase_entries;
4942 /* Handle modified and deleted entries. */
4943 nbase_entries = got_object_tree_get_nentries(base_tree);
4944 for (i = 0; i < nbase_entries; i++) {
4945 struct got_commitable *ct = NULL;
4947 te = got_object_tree_get_entry(base_tree, i);
4948 if (got_object_tree_entry_is_submodule(te)) {
4949 /* Entry is a submodule; just copy it. */
4950 err = got_object_tree_entry_dup(&new_te, te);
4951 if (err)
4952 goto done;
4953 err = insert_tree_entry(new_te, &paths);
4954 if (err)
4955 goto done;
4956 (*nentries)++;
4957 continue;
4960 if (S_ISDIR(te->mode)) {
4961 int modified;
4962 err = got_object_tree_entry_dup(&new_te, te);
4963 if (err)
4964 goto done;
4965 err = match_modified_subtree(&modified, te,
4966 path_base_tree, commitable_paths);
4967 if (err)
4968 goto done;
4969 /* Avoid recursion into unmodified subtrees. */
4970 if (modified) {
4971 struct got_object_id *new_id;
4972 int nsubentries;
4973 err = write_subtree(&new_id,
4974 &nsubentries, te,
4975 path_base_tree, commitable_paths,
4976 status_cb, status_arg, repo);
4977 if (err)
4978 goto done;
4979 if (nsubentries == 0) {
4980 /* All entries were deleted. */
4981 free(new_id);
4982 continue;
4984 memcpy(&new_te->id, new_id,
4985 sizeof(new_te->id));
4986 free(new_id);
4988 err = insert_tree_entry(new_te, &paths);
4989 if (err)
4990 goto done;
4991 (*nentries)++;
4992 continue;
4995 err = match_deleted_or_modified_ct(&ct, te,
4996 path_base_tree, commitable_paths);
4997 if (err)
4998 goto done;
4999 if (ct) {
5000 /* NB: Deleted entries get dropped here. */
5001 if (ct->status == GOT_STATUS_MODIFY ||
5002 ct->status == GOT_STATUS_MODE_CHANGE ||
5003 ct->staged_status == GOT_STATUS_MODIFY) {
5004 err = alloc_modified_blob_tree_entry(
5005 &new_te, te, ct);
5006 if (err)
5007 goto done;
5008 err = insert_tree_entry(new_te, &paths);
5009 if (err)
5010 goto done;
5011 (*nentries)++;
5013 err = report_ct_status(ct, status_cb,
5014 status_arg);
5015 if (err)
5016 goto done;
5017 } else {
5018 /* Entry is unchanged; just copy it. */
5019 err = got_object_tree_entry_dup(&new_te, te);
5020 if (err)
5021 goto done;
5022 err = insert_tree_entry(new_te, &paths);
5023 if (err)
5024 goto done;
5025 (*nentries)++;
5030 /* Write new list of entries; deleted entries have been dropped. */
5031 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5032 done:
5033 got_pathlist_free(&paths);
5034 return err;
5037 static const struct got_error *
5038 reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
5039 struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
5040 struct got_repository *repo)
5042 const struct got_error *err = NULL;
5043 struct got_blob_object *blob = NULL;
5044 struct got_object_id *tree_id = NULL;
5045 char *tree_path = NULL;
5046 struct got_tree_object *tree = NULL;
5047 struct got_tree_entry *te;
5048 char *entry_name;
5050 err = got_object_open_as_blob(&blob, repo, ct->blob_id, PATH_MAX);
5051 if (err)
5052 return err;
5054 err = got_path_dirname(&tree_path, ct->in_repo_path);
5055 if (err) {
5056 if (err->code != GOT_ERR_BAD_PATH)
5057 goto done;
5058 err = got_object_id_by_path(&tree_id, repo,
5059 new_base_commit_id, "");
5060 if (err)
5061 goto done;
5062 } else {
5063 err = got_object_id_by_path(&tree_id, repo,
5064 new_base_commit_id, tree_path);
5065 if (err)
5066 goto done;
5069 err = got_object_open_as_tree(&tree, repo, tree_id);
5070 if (err)
5071 goto done;
5073 entry_name = basename(ct->path);
5074 if (entry_name == NULL) {
5075 err = got_error_from_errno2("basename", ct->path);
5076 goto done;
5079 te = got_object_tree_find_entry(tree, entry_name);
5080 if (te == NULL) {
5081 err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5082 goto done;
5085 err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5086 ct->path, blob, 0, 0, 0, repo, NULL, NULL);
5087 done:
5088 if (blob)
5089 got_object_blob_close(blob);
5090 if (tree)
5091 got_object_tree_close(tree);
5092 free(tree_id);
5093 free(tree_path);
5094 return err;
5098 * After comitting a symlink we have a chance to convert "bad" symlinks
5099 * (those which point outside the work tree or into .got) to regular files.
5100 * This way, the post-commit work tree state matches a fresh checkout of
5101 * the tree which was just committed. We also mark such newly committed
5102 * symlinks as "bad" in the work tree's fileindex.
5104 static const struct got_error *
5105 reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5106 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5107 struct got_worktree *worktree, struct got_repository *repo)
5109 const struct got_error *err = NULL;
5110 struct got_pathlist_entry *pe;
5112 TAILQ_FOREACH(pe, commitable_paths, entry) {
5113 struct got_commitable *ct = pe->data;
5114 struct got_fileindex_entry *ie;
5115 int is_bad_symlink = 0;
5117 if (!S_ISLNK(get_ct_file_mode(ct)))
5118 continue;
5120 err = reinstall_symlink_after_commit(&is_bad_symlink,
5121 ct, new_base_commit_id, worktree, repo);
5122 if (err)
5123 break;
5124 ie = got_fileindex_entry_get(fileindex, ct->path,
5125 strlen(ct->path));
5126 if (ie && is_bad_symlink) {
5127 err = got_fileindex_entry_filetype_set(ie,
5128 GOT_FILEIDX_MODE_BAD_SYMLINK);
5129 if (err)
5130 break;
5134 return err;
5137 static const struct got_error *
5138 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5139 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5140 int have_staged_files)
5142 const struct got_error *err = NULL;
5143 struct got_pathlist_entry *pe;
5145 TAILQ_FOREACH(pe, commitable_paths, entry) {
5146 struct got_fileindex_entry *ie;
5147 struct got_commitable *ct = pe->data;
5149 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5150 if (ie) {
5151 if (ct->status == GOT_STATUS_DELETE ||
5152 ct->staged_status == GOT_STATUS_DELETE) {
5153 got_fileindex_entry_remove(fileindex, ie);
5154 } else if (ct->staged_status == GOT_STATUS_ADD ||
5155 ct->staged_status == GOT_STATUS_MODIFY) {
5156 got_fileindex_entry_stage_set(ie,
5157 GOT_FILEIDX_STAGE_NONE);
5158 err = got_fileindex_entry_update(ie,
5159 ct->ondisk_path, ct->staged_blob_id->sha1,
5160 new_base_commit_id->sha1,
5161 !have_staged_files);
5162 } else
5163 err = got_fileindex_entry_update(ie,
5164 ct->ondisk_path, ct->blob_id->sha1,
5165 new_base_commit_id->sha1,
5166 !have_staged_files);
5167 } else {
5168 err = got_fileindex_entry_alloc(&ie, pe->path);
5169 if (err)
5170 break;
5171 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5172 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5173 if (err) {
5174 got_fileindex_entry_free(ie);
5175 break;
5177 err = got_fileindex_entry_add(fileindex, ie);
5178 if (err) {
5179 got_fileindex_entry_free(ie);
5180 break;
5184 return err;
5188 static const struct got_error *
5189 check_out_of_date(const char *in_repo_path, unsigned char status,
5190 unsigned char staged_status, struct got_object_id *base_blob_id,
5191 struct got_object_id *base_commit_id,
5192 struct got_object_id *head_commit_id, struct got_repository *repo,
5193 int ood_errcode)
5195 const struct got_error *err = NULL;
5196 struct got_object_id *id = NULL;
5198 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5199 /* Trivial case: base commit == head commit */
5200 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5201 return NULL;
5203 * Ensure file content which local changes were based
5204 * on matches file content in the branch head.
5206 err = got_object_id_by_path(&id, repo, head_commit_id,
5207 in_repo_path);
5208 if (err) {
5209 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5210 err = got_error(ood_errcode);
5211 goto done;
5212 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5213 err = got_error(ood_errcode);
5214 } else {
5215 /* Require that added files don't exist in the branch head. */
5216 err = got_object_id_by_path(&id, repo, head_commit_id,
5217 in_repo_path);
5218 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5219 goto done;
5220 err = id ? got_error(ood_errcode) : NULL;
5222 done:
5223 free(id);
5224 return err;
5227 const struct got_error *
5228 commit_worktree(struct got_object_id **new_commit_id,
5229 struct got_pathlist_head *commitable_paths,
5230 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5231 const char *author, const char *committer,
5232 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5233 got_worktree_status_cb status_cb, void *status_arg,
5234 struct got_repository *repo)
5236 const struct got_error *err = NULL, *unlockerr = NULL;
5237 struct got_pathlist_entry *pe;
5238 const char *head_ref_name = NULL;
5239 struct got_commit_object *head_commit = NULL;
5240 struct got_reference *head_ref2 = NULL;
5241 struct got_object_id *head_commit_id2 = NULL;
5242 struct got_tree_object *head_tree = NULL;
5243 struct got_object_id *new_tree_id = NULL;
5244 int nentries;
5245 struct got_object_id_queue parent_ids;
5246 struct got_object_qid *pid = NULL;
5247 char *logmsg = NULL;
5249 *new_commit_id = NULL;
5251 SIMPLEQ_INIT(&parent_ids);
5253 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5254 if (err)
5255 goto done;
5257 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5258 if (err)
5259 goto done;
5261 if (commit_msg_cb != NULL) {
5262 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5263 if (err)
5264 goto done;
5267 if (logmsg == NULL || strlen(logmsg) == 0) {
5268 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5269 goto done;
5272 /* Create blobs from added and modified files and record their IDs. */
5273 TAILQ_FOREACH(pe, commitable_paths, entry) {
5274 struct got_commitable *ct = pe->data;
5275 char *ondisk_path;
5277 /* Blobs for staged files already exist. */
5278 if (ct->staged_status == GOT_STATUS_ADD ||
5279 ct->staged_status == GOT_STATUS_MODIFY)
5280 continue;
5282 if (ct->status != GOT_STATUS_ADD &&
5283 ct->status != GOT_STATUS_MODIFY &&
5284 ct->status != GOT_STATUS_MODE_CHANGE)
5285 continue;
5287 if (asprintf(&ondisk_path, "%s/%s",
5288 worktree->root_path, pe->path) == -1) {
5289 err = got_error_from_errno("asprintf");
5290 goto done;
5292 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5293 free(ondisk_path);
5294 if (err)
5295 goto done;
5298 /* Recursively write new tree objects. */
5299 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5300 commitable_paths, status_cb, status_arg, repo);
5301 if (err)
5302 goto done;
5304 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5305 if (err)
5306 goto done;
5307 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5308 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5309 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5310 got_object_qid_free(pid);
5311 if (logmsg != NULL)
5312 free(logmsg);
5313 if (err)
5314 goto done;
5316 /* Check if a concurrent commit to our branch has occurred. */
5317 head_ref_name = got_worktree_get_head_ref_name(worktree);
5318 if (head_ref_name == NULL) {
5319 err = got_error_from_errno("got_worktree_get_head_ref_name");
5320 goto done;
5322 /* Lock the reference here to prevent concurrent modification. */
5323 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5324 if (err)
5325 goto done;
5326 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5327 if (err)
5328 goto done;
5329 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5330 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5331 goto done;
5333 /* Update branch head in repository. */
5334 err = got_ref_change_ref(head_ref2, *new_commit_id);
5335 if (err)
5336 goto done;
5337 err = got_ref_write(head_ref2, repo);
5338 if (err)
5339 goto done;
5341 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5342 if (err)
5343 goto done;
5345 err = ref_base_commit(worktree, repo);
5346 if (err)
5347 goto done;
5348 done:
5349 if (head_tree)
5350 got_object_tree_close(head_tree);
5351 if (head_commit)
5352 got_object_commit_close(head_commit);
5353 free(head_commit_id2);
5354 if (head_ref2) {
5355 unlockerr = got_ref_unlock(head_ref2);
5356 if (unlockerr && err == NULL)
5357 err = unlockerr;
5358 got_ref_close(head_ref2);
5360 return err;
5363 static const struct got_error *
5364 check_path_is_commitable(const char *path,
5365 struct got_pathlist_head *commitable_paths)
5367 struct got_pathlist_entry *cpe = NULL;
5368 size_t path_len = strlen(path);
5370 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5371 struct got_commitable *ct = cpe->data;
5372 const char *ct_path = ct->path;
5374 while (ct_path[0] == '/')
5375 ct_path++;
5377 if (strcmp(path, ct_path) == 0 ||
5378 got_path_is_child(ct_path, path, path_len))
5379 break;
5382 if (cpe == NULL)
5383 return got_error_path(path, GOT_ERR_BAD_PATH);
5385 return NULL;
5388 static const struct got_error *
5389 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5391 int *have_staged_files = arg;
5393 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5394 *have_staged_files = 1;
5395 return got_error(GOT_ERR_CANCELLED);
5398 return NULL;
5401 static const struct got_error *
5402 check_non_staged_files(struct got_fileindex *fileindex,
5403 struct got_pathlist_head *paths)
5405 struct got_pathlist_entry *pe;
5406 struct got_fileindex_entry *ie;
5408 TAILQ_FOREACH(pe, paths, entry) {
5409 if (pe->path[0] == '\0')
5410 continue;
5411 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5412 if (ie == NULL)
5413 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5414 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5415 return got_error_path(pe->path,
5416 GOT_ERR_FILE_NOT_STAGED);
5419 return NULL;
5422 const struct got_error *
5423 got_worktree_commit(struct got_object_id **new_commit_id,
5424 struct got_worktree *worktree, struct got_pathlist_head *paths,
5425 const char *author, const char *committer,
5426 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5427 got_worktree_status_cb status_cb, void *status_arg,
5428 struct got_repository *repo)
5430 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5431 struct got_fileindex *fileindex = NULL;
5432 char *fileindex_path = NULL;
5433 struct got_pathlist_head commitable_paths;
5434 struct collect_commitables_arg cc_arg;
5435 struct got_pathlist_entry *pe;
5436 struct got_reference *head_ref = NULL;
5437 struct got_object_id *head_commit_id = NULL;
5438 int have_staged_files = 0;
5440 *new_commit_id = NULL;
5442 TAILQ_INIT(&commitable_paths);
5444 err = lock_worktree(worktree, LOCK_EX);
5445 if (err)
5446 goto done;
5448 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5449 if (err)
5450 goto done;
5452 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5453 if (err)
5454 goto done;
5456 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5457 if (err)
5458 goto done;
5460 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5461 &have_staged_files);
5462 if (err && err->code != GOT_ERR_CANCELLED)
5463 goto done;
5464 if (have_staged_files) {
5465 err = check_non_staged_files(fileindex, paths);
5466 if (err)
5467 goto done;
5470 cc_arg.commitable_paths = &commitable_paths;
5471 cc_arg.worktree = worktree;
5472 cc_arg.repo = repo;
5473 cc_arg.have_staged_files = have_staged_files;
5474 TAILQ_FOREACH(pe, paths, entry) {
5475 err = worktree_status(worktree, pe->path, fileindex, repo,
5476 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5477 if (err)
5478 goto done;
5481 if (TAILQ_EMPTY(&commitable_paths)) {
5482 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5483 goto done;
5486 TAILQ_FOREACH(pe, paths, entry) {
5487 err = check_path_is_commitable(pe->path, &commitable_paths);
5488 if (err)
5489 goto done;
5492 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5493 struct got_commitable *ct = pe->data;
5494 const char *ct_path = ct->in_repo_path;
5496 while (ct_path[0] == '/')
5497 ct_path++;
5498 err = check_out_of_date(ct_path, ct->status,
5499 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5500 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5501 if (err)
5502 goto done;
5506 err = commit_worktree(new_commit_id, &commitable_paths,
5507 head_commit_id, worktree, author, committer,
5508 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5509 if (err)
5510 goto done;
5512 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5513 fileindex, have_staged_files);
5514 if (err == NULL) {
5515 err = reinstall_symlinks_after_commit(&commitable_paths,
5516 *new_commit_id, fileindex, worktree, repo);
5518 sync_err = sync_fileindex(fileindex, fileindex_path);
5519 if (sync_err && err == NULL)
5520 err = sync_err;
5521 done:
5522 if (fileindex)
5523 got_fileindex_free(fileindex);
5524 free(fileindex_path);
5525 unlockerr = lock_worktree(worktree, LOCK_SH);
5526 if (unlockerr && err == NULL)
5527 err = unlockerr;
5528 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5529 struct got_commitable *ct = pe->data;
5530 free_commitable(ct);
5532 got_pathlist_free(&commitable_paths);
5533 return err;
5536 const char *
5537 got_commitable_get_path(struct got_commitable *ct)
5539 return ct->path;
5542 unsigned int
5543 got_commitable_get_status(struct got_commitable *ct)
5545 return ct->status;
5548 struct check_rebase_ok_arg {
5549 struct got_worktree *worktree;
5550 struct got_repository *repo;
5553 static const struct got_error *
5554 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5556 const struct got_error *err = NULL;
5557 struct check_rebase_ok_arg *a = arg;
5558 unsigned char status;
5559 struct stat sb;
5560 char *ondisk_path;
5562 /* Reject rebase of a work tree with mixed base commits. */
5563 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5564 SHA1_DIGEST_LENGTH))
5565 return got_error(GOT_ERR_MIXED_COMMITS);
5567 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5568 == -1)
5569 return got_error_from_errno("asprintf");
5571 /* Reject rebase of a work tree with modified or staged files. */
5572 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5573 free(ondisk_path);
5574 if (err)
5575 return err;
5577 if (status != GOT_STATUS_NO_CHANGE)
5578 return got_error(GOT_ERR_MODIFIED);
5579 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5580 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5582 return NULL;
5585 const struct got_error *
5586 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5587 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5588 struct got_worktree *worktree, struct got_reference *branch,
5589 struct got_repository *repo)
5591 const struct got_error *err = NULL;
5592 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5593 char *branch_ref_name = NULL;
5594 char *fileindex_path = NULL;
5595 struct check_rebase_ok_arg ok_arg;
5596 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5597 struct got_object_id *wt_branch_tip = NULL;
5599 *new_base_branch_ref = NULL;
5600 *tmp_branch = NULL;
5601 *fileindex = NULL;
5603 err = lock_worktree(worktree, LOCK_EX);
5604 if (err)
5605 return err;
5607 err = open_fileindex(fileindex, &fileindex_path, worktree);
5608 if (err)
5609 goto done;
5611 ok_arg.worktree = worktree;
5612 ok_arg.repo = repo;
5613 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5614 &ok_arg);
5615 if (err)
5616 goto done;
5618 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5619 if (err)
5620 goto done;
5622 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5623 if (err)
5624 goto done;
5626 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5627 if (err)
5628 goto done;
5630 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5631 0);
5632 if (err)
5633 goto done;
5635 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5636 if (err)
5637 goto done;
5638 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5639 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5640 goto done;
5643 err = got_ref_alloc_symref(new_base_branch_ref,
5644 new_base_branch_ref_name, wt_branch);
5645 if (err)
5646 goto done;
5647 err = got_ref_write(*new_base_branch_ref, repo);
5648 if (err)
5649 goto done;
5651 /* TODO Lock original branch's ref while rebasing? */
5653 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5654 if (err)
5655 goto done;
5657 err = got_ref_write(branch_ref, repo);
5658 if (err)
5659 goto done;
5661 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5662 worktree->base_commit_id);
5663 if (err)
5664 goto done;
5665 err = got_ref_write(*tmp_branch, repo);
5666 if (err)
5667 goto done;
5669 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5670 if (err)
5671 goto done;
5672 done:
5673 free(fileindex_path);
5674 free(tmp_branch_name);
5675 free(new_base_branch_ref_name);
5676 free(branch_ref_name);
5677 if (branch_ref)
5678 got_ref_close(branch_ref);
5679 if (wt_branch)
5680 got_ref_close(wt_branch);
5681 free(wt_branch_tip);
5682 if (err) {
5683 if (*new_base_branch_ref) {
5684 got_ref_close(*new_base_branch_ref);
5685 *new_base_branch_ref = NULL;
5687 if (*tmp_branch) {
5688 got_ref_close(*tmp_branch);
5689 *tmp_branch = NULL;
5691 if (*fileindex) {
5692 got_fileindex_free(*fileindex);
5693 *fileindex = NULL;
5695 lock_worktree(worktree, LOCK_SH);
5697 return err;
5700 const struct got_error *
5701 got_worktree_rebase_continue(struct got_object_id **commit_id,
5702 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5703 struct got_reference **branch, struct got_fileindex **fileindex,
5704 struct got_worktree *worktree, struct got_repository *repo)
5706 const struct got_error *err;
5707 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5708 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5709 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5710 char *fileindex_path = NULL;
5711 int have_staged_files = 0;
5713 *commit_id = NULL;
5714 *new_base_branch = NULL;
5715 *tmp_branch = NULL;
5716 *branch = NULL;
5717 *fileindex = NULL;
5719 err = lock_worktree(worktree, LOCK_EX);
5720 if (err)
5721 return err;
5723 err = open_fileindex(fileindex, &fileindex_path, worktree);
5724 if (err)
5725 goto done;
5727 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5728 &have_staged_files);
5729 if (err && err->code != GOT_ERR_CANCELLED)
5730 goto done;
5731 if (have_staged_files) {
5732 err = got_error(GOT_ERR_STAGED_PATHS);
5733 goto done;
5736 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5737 if (err)
5738 goto done;
5740 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5741 if (err)
5742 goto done;
5744 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5745 if (err)
5746 goto done;
5748 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5749 if (err)
5750 goto done;
5752 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5753 if (err)
5754 goto done;
5756 err = got_ref_open(branch, repo,
5757 got_ref_get_symref_target(branch_ref), 0);
5758 if (err)
5759 goto done;
5761 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5762 if (err)
5763 goto done;
5765 err = got_ref_resolve(commit_id, repo, commit_ref);
5766 if (err)
5767 goto done;
5769 err = got_ref_open(new_base_branch, repo,
5770 new_base_branch_ref_name, 0);
5771 if (err)
5772 goto done;
5774 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5775 if (err)
5776 goto done;
5777 done:
5778 free(commit_ref_name);
5779 free(branch_ref_name);
5780 free(fileindex_path);
5781 if (commit_ref)
5782 got_ref_close(commit_ref);
5783 if (branch_ref)
5784 got_ref_close(branch_ref);
5785 if (err) {
5786 free(*commit_id);
5787 *commit_id = NULL;
5788 if (*tmp_branch) {
5789 got_ref_close(*tmp_branch);
5790 *tmp_branch = NULL;
5792 if (*new_base_branch) {
5793 got_ref_close(*new_base_branch);
5794 *new_base_branch = NULL;
5796 if (*branch) {
5797 got_ref_close(*branch);
5798 *branch = NULL;
5800 if (*fileindex) {
5801 got_fileindex_free(*fileindex);
5802 *fileindex = NULL;
5804 lock_worktree(worktree, LOCK_SH);
5806 return err;
5809 const struct got_error *
5810 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5812 const struct got_error *err;
5813 char *tmp_branch_name = NULL;
5815 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5816 if (err)
5817 return err;
5819 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5820 free(tmp_branch_name);
5821 return NULL;
5824 static const struct got_error *
5825 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5826 char **logmsg, void *arg)
5828 *logmsg = arg;
5829 return NULL;
5832 static const struct got_error *
5833 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5834 const char *path, struct got_object_id *blob_id,
5835 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5836 int dirfd, const char *de_name)
5838 return NULL;
5841 struct collect_merged_paths_arg {
5842 got_worktree_checkout_cb progress_cb;
5843 void *progress_arg;
5844 struct got_pathlist_head *merged_paths;
5847 static const struct got_error *
5848 collect_merged_paths(void *arg, unsigned char status, const char *path)
5850 const struct got_error *err;
5851 struct collect_merged_paths_arg *a = arg;
5852 char *p;
5853 struct got_pathlist_entry *new;
5855 err = (*a->progress_cb)(a->progress_arg, status, path);
5856 if (err)
5857 return err;
5859 if (status != GOT_STATUS_MERGE &&
5860 status != GOT_STATUS_ADD &&
5861 status != GOT_STATUS_DELETE &&
5862 status != GOT_STATUS_CONFLICT)
5863 return NULL;
5865 p = strdup(path);
5866 if (p == NULL)
5867 return got_error_from_errno("strdup");
5869 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5870 if (err || new == NULL)
5871 free(p);
5872 return err;
5875 void
5876 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5878 struct got_pathlist_entry *pe;
5880 TAILQ_FOREACH(pe, merged_paths, entry)
5881 free((char *)pe->path);
5883 got_pathlist_free(merged_paths);
5886 static const struct got_error *
5887 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5888 int is_rebase, struct got_repository *repo)
5890 const struct got_error *err;
5891 struct got_reference *commit_ref = NULL;
5893 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5894 if (err) {
5895 if (err->code != GOT_ERR_NOT_REF)
5896 goto done;
5897 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5898 if (err)
5899 goto done;
5900 err = got_ref_write(commit_ref, repo);
5901 if (err)
5902 goto done;
5903 } else if (is_rebase) {
5904 struct got_object_id *stored_id;
5905 int cmp;
5907 err = got_ref_resolve(&stored_id, repo, commit_ref);
5908 if (err)
5909 goto done;
5910 cmp = got_object_id_cmp(commit_id, stored_id);
5911 free(stored_id);
5912 if (cmp != 0) {
5913 err = got_error(GOT_ERR_REBASE_COMMITID);
5914 goto done;
5917 done:
5918 if (commit_ref)
5919 got_ref_close(commit_ref);
5920 return err;
5923 static const struct got_error *
5924 rebase_merge_files(struct got_pathlist_head *merged_paths,
5925 const char *commit_ref_name, struct got_worktree *worktree,
5926 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5927 struct got_object_id *commit_id, struct got_repository *repo,
5928 got_worktree_checkout_cb progress_cb, void *progress_arg,
5929 got_cancel_cb cancel_cb, void *cancel_arg)
5931 const struct got_error *err;
5932 struct got_reference *commit_ref = NULL;
5933 struct collect_merged_paths_arg cmp_arg;
5934 char *fileindex_path;
5936 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5938 err = get_fileindex_path(&fileindex_path, worktree);
5939 if (err)
5940 return err;
5942 cmp_arg.progress_cb = progress_cb;
5943 cmp_arg.progress_arg = progress_arg;
5944 cmp_arg.merged_paths = merged_paths;
5945 err = merge_files(worktree, fileindex, fileindex_path,
5946 parent_commit_id, commit_id, repo, collect_merged_paths,
5947 &cmp_arg, cancel_cb, cancel_arg);
5948 if (commit_ref)
5949 got_ref_close(commit_ref);
5950 return err;
5953 const struct got_error *
5954 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5955 struct got_worktree *worktree, struct got_fileindex *fileindex,
5956 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5957 struct got_repository *repo,
5958 got_worktree_checkout_cb progress_cb, void *progress_arg,
5959 got_cancel_cb cancel_cb, void *cancel_arg)
5961 const struct got_error *err;
5962 char *commit_ref_name;
5964 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5965 if (err)
5966 return err;
5968 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5969 if (err)
5970 goto done;
5972 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5973 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5974 progress_arg, cancel_cb, cancel_arg);
5975 done:
5976 free(commit_ref_name);
5977 return err;
5980 const struct got_error *
5981 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5982 struct got_worktree *worktree, struct got_fileindex *fileindex,
5983 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5984 struct got_repository *repo,
5985 got_worktree_checkout_cb progress_cb, void *progress_arg,
5986 got_cancel_cb cancel_cb, void *cancel_arg)
5988 const struct got_error *err;
5989 char *commit_ref_name;
5991 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5992 if (err)
5993 return err;
5995 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5996 if (err)
5997 goto done;
5999 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6000 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6001 progress_arg, cancel_cb, cancel_arg);
6002 done:
6003 free(commit_ref_name);
6004 return err;
6007 static const struct got_error *
6008 rebase_commit(struct got_object_id **new_commit_id,
6009 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6010 struct got_worktree *worktree, struct got_fileindex *fileindex,
6011 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6012 const char *new_logmsg, struct got_repository *repo)
6014 const struct got_error *err, *sync_err;
6015 struct got_pathlist_head commitable_paths;
6016 struct collect_commitables_arg cc_arg;
6017 char *fileindex_path = NULL;
6018 struct got_reference *head_ref = NULL;
6019 struct got_object_id *head_commit_id = NULL;
6020 char *logmsg = NULL;
6022 TAILQ_INIT(&commitable_paths);
6023 *new_commit_id = NULL;
6025 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6027 err = get_fileindex_path(&fileindex_path, worktree);
6028 if (err)
6029 return err;
6031 cc_arg.commitable_paths = &commitable_paths;
6032 cc_arg.worktree = worktree;
6033 cc_arg.repo = repo;
6034 cc_arg.have_staged_files = 0;
6036 * If possible get the status of individual files directly to
6037 * avoid crawling the entire work tree once per rebased commit.
6038 * TODO: Ideally, merged_paths would contain a list of commitables
6039 * we could use so we could skip worktree_status() entirely.
6041 if (merged_paths) {
6042 struct got_pathlist_entry *pe;
6043 TAILQ_FOREACH(pe, merged_paths, entry) {
6044 err = worktree_status(worktree, pe->path, fileindex,
6045 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6046 0);
6047 if (err)
6048 goto done;
6050 } else {
6051 err = worktree_status(worktree, "", fileindex, repo,
6052 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6053 if (err)
6054 goto done;
6057 if (TAILQ_EMPTY(&commitable_paths)) {
6058 /* No-op change; commit will be elided. */
6059 err = got_ref_delete(commit_ref, repo);
6060 if (err)
6061 goto done;
6062 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6063 goto done;
6066 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6067 if (err)
6068 goto done;
6070 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6071 if (err)
6072 goto done;
6074 if (new_logmsg) {
6075 logmsg = strdup(new_logmsg);
6076 if (logmsg == NULL) {
6077 err = got_error_from_errno("strdup");
6078 goto done;
6080 } else {
6081 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6082 if (err)
6083 goto done;
6086 /* NB: commit_worktree will call free(logmsg) */
6087 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6088 worktree, got_object_commit_get_author(orig_commit),
6089 got_object_commit_get_committer(orig_commit),
6090 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6091 if (err)
6092 goto done;
6094 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6095 if (err)
6096 goto done;
6098 err = got_ref_delete(commit_ref, repo);
6099 if (err)
6100 goto done;
6102 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6103 fileindex, 0);
6104 sync_err = sync_fileindex(fileindex, fileindex_path);
6105 if (sync_err && err == NULL)
6106 err = sync_err;
6107 done:
6108 free(fileindex_path);
6109 free(head_commit_id);
6110 if (head_ref)
6111 got_ref_close(head_ref);
6112 if (err) {
6113 free(*new_commit_id);
6114 *new_commit_id = NULL;
6116 return err;
6119 const struct got_error *
6120 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6121 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6122 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6123 struct got_commit_object *orig_commit,
6124 struct got_object_id *orig_commit_id, struct got_repository *repo)
6126 const struct got_error *err;
6127 char *commit_ref_name;
6128 struct got_reference *commit_ref = NULL;
6129 struct got_object_id *commit_id = NULL;
6131 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6132 if (err)
6133 return err;
6135 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6136 if (err)
6137 goto done;
6138 err = got_ref_resolve(&commit_id, repo, commit_ref);
6139 if (err)
6140 goto done;
6141 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6142 err = got_error(GOT_ERR_REBASE_COMMITID);
6143 goto done;
6146 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6147 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6148 done:
6149 if (commit_ref)
6150 got_ref_close(commit_ref);
6151 free(commit_ref_name);
6152 free(commit_id);
6153 return err;
6156 const struct got_error *
6157 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6158 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6159 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6160 struct got_commit_object *orig_commit,
6161 struct got_object_id *orig_commit_id, const char *new_logmsg,
6162 struct got_repository *repo)
6164 const struct got_error *err;
6165 char *commit_ref_name;
6166 struct got_reference *commit_ref = NULL;
6168 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6169 if (err)
6170 return err;
6172 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6173 if (err)
6174 goto done;
6176 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6177 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6178 done:
6179 if (commit_ref)
6180 got_ref_close(commit_ref);
6181 free(commit_ref_name);
6182 return err;
6185 const struct got_error *
6186 got_worktree_rebase_postpone(struct got_worktree *worktree,
6187 struct got_fileindex *fileindex)
6189 if (fileindex)
6190 got_fileindex_free(fileindex);
6191 return lock_worktree(worktree, LOCK_SH);
6194 static const struct got_error *
6195 delete_ref(const char *name, struct got_repository *repo)
6197 const struct got_error *err;
6198 struct got_reference *ref;
6200 err = got_ref_open(&ref, repo, name, 0);
6201 if (err) {
6202 if (err->code == GOT_ERR_NOT_REF)
6203 return NULL;
6204 return err;
6207 err = got_ref_delete(ref, repo);
6208 got_ref_close(ref);
6209 return err;
6212 static const struct got_error *
6213 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6215 const struct got_error *err;
6216 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6217 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6219 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6220 if (err)
6221 goto done;
6222 err = delete_ref(tmp_branch_name, repo);
6223 if (err)
6224 goto done;
6226 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6227 if (err)
6228 goto done;
6229 err = delete_ref(new_base_branch_ref_name, repo);
6230 if (err)
6231 goto done;
6233 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6234 if (err)
6235 goto done;
6236 err = delete_ref(branch_ref_name, repo);
6237 if (err)
6238 goto done;
6240 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6241 if (err)
6242 goto done;
6243 err = delete_ref(commit_ref_name, repo);
6244 if (err)
6245 goto done;
6247 done:
6248 free(tmp_branch_name);
6249 free(new_base_branch_ref_name);
6250 free(branch_ref_name);
6251 free(commit_ref_name);
6252 return err;
6255 const struct got_error *
6256 got_worktree_rebase_complete(struct got_worktree *worktree,
6257 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6258 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6259 struct got_repository *repo)
6261 const struct got_error *err, *unlockerr;
6262 struct got_object_id *new_head_commit_id = NULL;
6264 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6265 if (err)
6266 return err;
6268 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6269 if (err)
6270 goto done;
6272 err = got_ref_write(rebased_branch, repo);
6273 if (err)
6274 goto done;
6276 err = got_worktree_set_head_ref(worktree, rebased_branch);
6277 if (err)
6278 goto done;
6280 err = delete_rebase_refs(worktree, repo);
6281 done:
6282 if (fileindex)
6283 got_fileindex_free(fileindex);
6284 free(new_head_commit_id);
6285 unlockerr = lock_worktree(worktree, LOCK_SH);
6286 if (unlockerr && err == NULL)
6287 err = unlockerr;
6288 return err;
6291 const struct got_error *
6292 got_worktree_rebase_abort(struct got_worktree *worktree,
6293 struct got_fileindex *fileindex, struct got_repository *repo,
6294 struct got_reference *new_base_branch,
6295 got_worktree_checkout_cb progress_cb, void *progress_arg)
6297 const struct got_error *err, *unlockerr, *sync_err;
6298 struct got_reference *resolved = NULL;
6299 struct got_object_id *commit_id = NULL;
6300 char *fileindex_path = NULL;
6301 struct revert_file_args rfa;
6302 struct got_object_id *tree_id = NULL;
6304 err = lock_worktree(worktree, LOCK_EX);
6305 if (err)
6306 return err;
6308 err = got_ref_open(&resolved, repo,
6309 got_ref_get_symref_target(new_base_branch), 0);
6310 if (err)
6311 goto done;
6313 err = got_worktree_set_head_ref(worktree, resolved);
6314 if (err)
6315 goto done;
6318 * XXX commits to the base branch could have happened while
6319 * we were busy rebasing; should we store the original commit ID
6320 * when rebase begins and read it back here?
6322 err = got_ref_resolve(&commit_id, repo, resolved);
6323 if (err)
6324 goto done;
6326 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6327 if (err)
6328 goto done;
6330 err = got_object_id_by_path(&tree_id, repo,
6331 worktree->base_commit_id, worktree->path_prefix);
6332 if (err)
6333 goto done;
6335 err = delete_rebase_refs(worktree, repo);
6336 if (err)
6337 goto done;
6339 err = get_fileindex_path(&fileindex_path, worktree);
6340 if (err)
6341 goto done;
6343 rfa.worktree = worktree;
6344 rfa.fileindex = fileindex;
6345 rfa.progress_cb = progress_cb;
6346 rfa.progress_arg = progress_arg;
6347 rfa.patch_cb = NULL;
6348 rfa.patch_arg = NULL;
6349 rfa.repo = repo;
6350 err = worktree_status(worktree, "", fileindex, repo,
6351 revert_file, &rfa, NULL, NULL, 0, 0);
6352 if (err)
6353 goto sync;
6355 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6356 repo, progress_cb, progress_arg, NULL, NULL);
6357 sync:
6358 sync_err = sync_fileindex(fileindex, fileindex_path);
6359 if (sync_err && err == NULL)
6360 err = sync_err;
6361 done:
6362 got_ref_close(resolved);
6363 free(tree_id);
6364 free(commit_id);
6365 if (fileindex)
6366 got_fileindex_free(fileindex);
6367 free(fileindex_path);
6369 unlockerr = lock_worktree(worktree, LOCK_SH);
6370 if (unlockerr && err == NULL)
6371 err = unlockerr;
6372 return err;
6375 const struct got_error *
6376 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6377 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6378 struct got_fileindex **fileindex, struct got_worktree *worktree,
6379 struct got_repository *repo)
6381 const struct got_error *err = NULL;
6382 char *tmp_branch_name = NULL;
6383 char *branch_ref_name = NULL;
6384 char *base_commit_ref_name = NULL;
6385 char *fileindex_path = NULL;
6386 struct check_rebase_ok_arg ok_arg;
6387 struct got_reference *wt_branch = NULL;
6388 struct got_reference *base_commit_ref = NULL;
6390 *tmp_branch = NULL;
6391 *branch_ref = NULL;
6392 *base_commit_id = NULL;
6393 *fileindex = NULL;
6395 err = lock_worktree(worktree, LOCK_EX);
6396 if (err)
6397 return err;
6399 err = open_fileindex(fileindex, &fileindex_path, worktree);
6400 if (err)
6401 goto done;
6403 ok_arg.worktree = worktree;
6404 ok_arg.repo = repo;
6405 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6406 &ok_arg);
6407 if (err)
6408 goto done;
6410 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6411 if (err)
6412 goto done;
6414 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6415 if (err)
6416 goto done;
6418 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6419 worktree);
6420 if (err)
6421 goto done;
6423 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6424 0);
6425 if (err)
6426 goto done;
6428 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6429 if (err)
6430 goto done;
6432 err = got_ref_write(*branch_ref, repo);
6433 if (err)
6434 goto done;
6436 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6437 worktree->base_commit_id);
6438 if (err)
6439 goto done;
6440 err = got_ref_write(base_commit_ref, repo);
6441 if (err)
6442 goto done;
6443 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6444 if (*base_commit_id == NULL) {
6445 err = got_error_from_errno("got_object_id_dup");
6446 goto done;
6449 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6450 worktree->base_commit_id);
6451 if (err)
6452 goto done;
6453 err = got_ref_write(*tmp_branch, repo);
6454 if (err)
6455 goto done;
6457 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6458 if (err)
6459 goto done;
6460 done:
6461 free(fileindex_path);
6462 free(tmp_branch_name);
6463 free(branch_ref_name);
6464 free(base_commit_ref_name);
6465 if (wt_branch)
6466 got_ref_close(wt_branch);
6467 if (err) {
6468 if (*branch_ref) {
6469 got_ref_close(*branch_ref);
6470 *branch_ref = NULL;
6472 if (*tmp_branch) {
6473 got_ref_close(*tmp_branch);
6474 *tmp_branch = NULL;
6476 free(*base_commit_id);
6477 if (*fileindex) {
6478 got_fileindex_free(*fileindex);
6479 *fileindex = NULL;
6481 lock_worktree(worktree, LOCK_SH);
6483 return err;
6486 const struct got_error *
6487 got_worktree_histedit_postpone(struct got_worktree *worktree,
6488 struct got_fileindex *fileindex)
6490 if (fileindex)
6491 got_fileindex_free(fileindex);
6492 return lock_worktree(worktree, LOCK_SH);
6495 const struct got_error *
6496 got_worktree_histedit_in_progress(int *in_progress,
6497 struct got_worktree *worktree)
6499 const struct got_error *err;
6500 char *tmp_branch_name = NULL;
6502 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6503 if (err)
6504 return err;
6506 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6507 free(tmp_branch_name);
6508 return NULL;
6511 const struct got_error *
6512 got_worktree_histedit_continue(struct got_object_id **commit_id,
6513 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6514 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6515 struct got_worktree *worktree, struct got_repository *repo)
6517 const struct got_error *err;
6518 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6519 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6520 struct got_reference *commit_ref = NULL;
6521 struct got_reference *base_commit_ref = NULL;
6522 char *fileindex_path = NULL;
6523 int have_staged_files = 0;
6525 *commit_id = NULL;
6526 *tmp_branch = NULL;
6527 *base_commit_id = NULL;
6528 *fileindex = NULL;
6530 err = lock_worktree(worktree, LOCK_EX);
6531 if (err)
6532 return err;
6534 err = open_fileindex(fileindex, &fileindex_path, worktree);
6535 if (err)
6536 goto done;
6538 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6539 &have_staged_files);
6540 if (err && err->code != GOT_ERR_CANCELLED)
6541 goto done;
6542 if (have_staged_files) {
6543 err = got_error(GOT_ERR_STAGED_PATHS);
6544 goto done;
6547 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6548 if (err)
6549 goto done;
6551 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6552 if (err)
6553 goto done;
6555 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6556 if (err)
6557 goto done;
6559 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6560 worktree);
6561 if (err)
6562 goto done;
6564 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6565 if (err)
6566 goto done;
6568 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6569 if (err)
6570 goto done;
6571 err = got_ref_resolve(commit_id, repo, commit_ref);
6572 if (err)
6573 goto done;
6575 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6576 if (err)
6577 goto done;
6578 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6579 if (err)
6580 goto done;
6582 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6583 if (err)
6584 goto done;
6585 done:
6586 free(commit_ref_name);
6587 free(branch_ref_name);
6588 free(fileindex_path);
6589 if (commit_ref)
6590 got_ref_close(commit_ref);
6591 if (base_commit_ref)
6592 got_ref_close(base_commit_ref);
6593 if (err) {
6594 free(*commit_id);
6595 *commit_id = NULL;
6596 free(*base_commit_id);
6597 *base_commit_id = NULL;
6598 if (*tmp_branch) {
6599 got_ref_close(*tmp_branch);
6600 *tmp_branch = NULL;
6602 if (*fileindex) {
6603 got_fileindex_free(*fileindex);
6604 *fileindex = NULL;
6606 lock_worktree(worktree, LOCK_EX);
6608 return err;
6611 static const struct got_error *
6612 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6614 const struct got_error *err;
6615 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6616 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6618 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6619 if (err)
6620 goto done;
6621 err = delete_ref(tmp_branch_name, repo);
6622 if (err)
6623 goto done;
6625 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6626 worktree);
6627 if (err)
6628 goto done;
6629 err = delete_ref(base_commit_ref_name, repo);
6630 if (err)
6631 goto done;
6633 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6634 if (err)
6635 goto done;
6636 err = delete_ref(branch_ref_name, repo);
6637 if (err)
6638 goto done;
6640 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6641 if (err)
6642 goto done;
6643 err = delete_ref(commit_ref_name, repo);
6644 if (err)
6645 goto done;
6646 done:
6647 free(tmp_branch_name);
6648 free(base_commit_ref_name);
6649 free(branch_ref_name);
6650 free(commit_ref_name);
6651 return err;
6654 const struct got_error *
6655 got_worktree_histedit_abort(struct got_worktree *worktree,
6656 struct got_fileindex *fileindex, struct got_repository *repo,
6657 struct got_reference *branch, struct got_object_id *base_commit_id,
6658 got_worktree_checkout_cb progress_cb, void *progress_arg)
6660 const struct got_error *err, *unlockerr, *sync_err;
6661 struct got_reference *resolved = NULL;
6662 char *fileindex_path = NULL;
6663 struct got_object_id *tree_id = NULL;
6664 struct revert_file_args rfa;
6666 err = lock_worktree(worktree, LOCK_EX);
6667 if (err)
6668 return err;
6670 err = got_ref_open(&resolved, repo,
6671 got_ref_get_symref_target(branch), 0);
6672 if (err)
6673 goto done;
6675 err = got_worktree_set_head_ref(worktree, resolved);
6676 if (err)
6677 goto done;
6679 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6680 if (err)
6681 goto done;
6683 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6684 worktree->path_prefix);
6685 if (err)
6686 goto done;
6688 err = delete_histedit_refs(worktree, repo);
6689 if (err)
6690 goto done;
6692 err = get_fileindex_path(&fileindex_path, worktree);
6693 if (err)
6694 goto done;
6696 rfa.worktree = worktree;
6697 rfa.fileindex = fileindex;
6698 rfa.progress_cb = progress_cb;
6699 rfa.progress_arg = progress_arg;
6700 rfa.patch_cb = NULL;
6701 rfa.patch_arg = NULL;
6702 rfa.repo = repo;
6703 err = worktree_status(worktree, "", fileindex, repo,
6704 revert_file, &rfa, NULL, NULL, 0, 0);
6705 if (err)
6706 goto sync;
6708 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6709 repo, progress_cb, progress_arg, NULL, NULL);
6710 sync:
6711 sync_err = sync_fileindex(fileindex, fileindex_path);
6712 if (sync_err && err == NULL)
6713 err = sync_err;
6714 done:
6715 got_ref_close(resolved);
6716 free(tree_id);
6717 free(fileindex_path);
6719 unlockerr = lock_worktree(worktree, LOCK_SH);
6720 if (unlockerr && err == NULL)
6721 err = unlockerr;
6722 return err;
6725 const struct got_error *
6726 got_worktree_histedit_complete(struct got_worktree *worktree,
6727 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6728 struct got_reference *edited_branch, struct got_repository *repo)
6730 const struct got_error *err, *unlockerr;
6731 struct got_object_id *new_head_commit_id = NULL;
6732 struct got_reference *resolved = NULL;
6734 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6735 if (err)
6736 return err;
6738 err = got_ref_open(&resolved, repo,
6739 got_ref_get_symref_target(edited_branch), 0);
6740 if (err)
6741 goto done;
6743 err = got_ref_change_ref(resolved, new_head_commit_id);
6744 if (err)
6745 goto done;
6747 err = got_ref_write(resolved, repo);
6748 if (err)
6749 goto done;
6751 err = got_worktree_set_head_ref(worktree, resolved);
6752 if (err)
6753 goto done;
6755 err = delete_histedit_refs(worktree, repo);
6756 done:
6757 if (fileindex)
6758 got_fileindex_free(fileindex);
6759 free(new_head_commit_id);
6760 unlockerr = lock_worktree(worktree, LOCK_SH);
6761 if (unlockerr && err == NULL)
6762 err = unlockerr;
6763 return err;
6766 const struct got_error *
6767 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6768 struct got_object_id *commit_id, struct got_repository *repo)
6770 const struct got_error *err;
6771 char *commit_ref_name;
6773 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6774 if (err)
6775 return err;
6777 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6778 if (err)
6779 goto done;
6781 err = delete_ref(commit_ref_name, repo);
6782 done:
6783 free(commit_ref_name);
6784 return err;
6787 const struct got_error *
6788 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6789 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6790 struct got_worktree *worktree, const char *refname,
6791 struct got_repository *repo)
6793 const struct got_error *err = NULL;
6794 char *fileindex_path = NULL;
6795 struct check_rebase_ok_arg ok_arg;
6797 *fileindex = NULL;
6798 *branch_ref = NULL;
6799 *base_branch_ref = NULL;
6801 err = lock_worktree(worktree, LOCK_EX);
6802 if (err)
6803 return err;
6805 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6806 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6807 "cannot integrate a branch into itself; "
6808 "update -b or different branch name required");
6809 goto done;
6812 err = open_fileindex(fileindex, &fileindex_path, worktree);
6813 if (err)
6814 goto done;
6816 /* Preconditions are the same as for rebase. */
6817 ok_arg.worktree = worktree;
6818 ok_arg.repo = repo;
6819 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6820 &ok_arg);
6821 if (err)
6822 goto done;
6824 err = got_ref_open(branch_ref, repo, refname, 1);
6825 if (err)
6826 goto done;
6828 err = got_ref_open(base_branch_ref, repo,
6829 got_worktree_get_head_ref_name(worktree), 1);
6830 done:
6831 if (err) {
6832 if (*branch_ref) {
6833 got_ref_close(*branch_ref);
6834 *branch_ref = NULL;
6836 if (*base_branch_ref) {
6837 got_ref_close(*base_branch_ref);
6838 *base_branch_ref = NULL;
6840 if (*fileindex) {
6841 got_fileindex_free(*fileindex);
6842 *fileindex = NULL;
6844 lock_worktree(worktree, LOCK_SH);
6846 return err;
6849 const struct got_error *
6850 got_worktree_integrate_continue(struct got_worktree *worktree,
6851 struct got_fileindex *fileindex, struct got_repository *repo,
6852 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6853 got_worktree_checkout_cb progress_cb, void *progress_arg,
6854 got_cancel_cb cancel_cb, void *cancel_arg)
6856 const struct got_error *err = NULL, *sync_err, *unlockerr;
6857 char *fileindex_path = NULL;
6858 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6860 err = get_fileindex_path(&fileindex_path, worktree);
6861 if (err)
6862 goto done;
6864 err = got_ref_resolve(&commit_id, repo, branch_ref);
6865 if (err)
6866 goto done;
6868 err = got_object_id_by_path(&tree_id, repo, commit_id,
6869 worktree->path_prefix);
6870 if (err)
6871 goto done;
6873 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6874 if (err)
6875 goto done;
6877 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6878 progress_cb, progress_arg, cancel_cb, cancel_arg);
6879 if (err)
6880 goto sync;
6882 err = got_ref_change_ref(base_branch_ref, commit_id);
6883 if (err)
6884 goto sync;
6886 err = got_ref_write(base_branch_ref, repo);
6887 sync:
6888 sync_err = sync_fileindex(fileindex, fileindex_path);
6889 if (sync_err && err == NULL)
6890 err = sync_err;
6892 done:
6893 unlockerr = got_ref_unlock(branch_ref);
6894 if (unlockerr && err == NULL)
6895 err = unlockerr;
6896 got_ref_close(branch_ref);
6898 unlockerr = got_ref_unlock(base_branch_ref);
6899 if (unlockerr && err == NULL)
6900 err = unlockerr;
6901 got_ref_close(base_branch_ref);
6903 got_fileindex_free(fileindex);
6904 free(fileindex_path);
6905 free(tree_id);
6907 unlockerr = lock_worktree(worktree, LOCK_SH);
6908 if (unlockerr && err == NULL)
6909 err = unlockerr;
6910 return err;
6913 const struct got_error *
6914 got_worktree_integrate_abort(struct got_worktree *worktree,
6915 struct got_fileindex *fileindex, struct got_repository *repo,
6916 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6918 const struct got_error *err = NULL, *unlockerr = NULL;
6920 got_fileindex_free(fileindex);
6922 err = lock_worktree(worktree, LOCK_SH);
6924 unlockerr = got_ref_unlock(branch_ref);
6925 if (unlockerr && err == NULL)
6926 err = unlockerr;
6927 got_ref_close(branch_ref);
6929 unlockerr = got_ref_unlock(base_branch_ref);
6930 if (unlockerr && err == NULL)
6931 err = unlockerr;
6932 got_ref_close(base_branch_ref);
6934 return err;
6937 struct check_stage_ok_arg {
6938 struct got_object_id *head_commit_id;
6939 struct got_worktree *worktree;
6940 struct got_fileindex *fileindex;
6941 struct got_repository *repo;
6942 int have_changes;
6945 const struct got_error *
6946 check_stage_ok(void *arg, unsigned char status,
6947 unsigned char staged_status, const char *relpath,
6948 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6949 struct got_object_id *commit_id, int dirfd, const char *de_name)
6951 struct check_stage_ok_arg *a = arg;
6952 const struct got_error *err = NULL;
6953 struct got_fileindex_entry *ie;
6954 struct got_object_id base_commit_id;
6955 struct got_object_id *base_commit_idp = NULL;
6956 char *in_repo_path = NULL, *p;
6958 if (status == GOT_STATUS_UNVERSIONED ||
6959 status == GOT_STATUS_NO_CHANGE)
6960 return NULL;
6961 if (status == GOT_STATUS_NONEXISTENT)
6962 return got_error_set_errno(ENOENT, relpath);
6964 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6965 if (ie == NULL)
6966 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6968 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6969 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6970 relpath) == -1)
6971 return got_error_from_errno("asprintf");
6973 if (got_fileindex_entry_has_commit(ie)) {
6974 memcpy(base_commit_id.sha1, ie->commit_sha1,
6975 SHA1_DIGEST_LENGTH);
6976 base_commit_idp = &base_commit_id;
6979 if (status == GOT_STATUS_CONFLICT) {
6980 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6981 goto done;
6982 } else if (status != GOT_STATUS_ADD &&
6983 status != GOT_STATUS_MODIFY &&
6984 status != GOT_STATUS_DELETE) {
6985 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6986 goto done;
6989 a->have_changes = 1;
6991 p = in_repo_path;
6992 while (p[0] == '/')
6993 p++;
6994 err = check_out_of_date(p, status, staged_status,
6995 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6996 GOT_ERR_STAGE_OUT_OF_DATE);
6997 done:
6998 free(in_repo_path);
6999 return err;
7002 struct stage_path_arg {
7003 struct got_worktree *worktree;
7004 struct got_fileindex *fileindex;
7005 struct got_repository *repo;
7006 got_worktree_status_cb status_cb;
7007 void *status_arg;
7008 got_worktree_patch_cb patch_cb;
7009 void *patch_arg;
7010 int staged_something;
7013 static const struct got_error *
7014 stage_path(void *arg, unsigned char status,
7015 unsigned char staged_status, const char *relpath,
7016 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7017 struct got_object_id *commit_id, int dirfd, const char *de_name)
7019 struct stage_path_arg *a = arg;
7020 const struct got_error *err = NULL;
7021 struct got_fileindex_entry *ie;
7022 char *ondisk_path = NULL, *path_content = NULL;
7023 uint32_t stage;
7024 struct got_object_id *new_staged_blob_id = NULL;
7026 if (status == GOT_STATUS_UNVERSIONED)
7027 return NULL;
7029 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7030 if (ie == NULL)
7031 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7033 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7034 relpath)== -1)
7035 return got_error_from_errno("asprintf");
7037 switch (status) {
7038 case GOT_STATUS_ADD:
7039 case GOT_STATUS_MODIFY:
7040 if (a->patch_cb) {
7041 if (status == GOT_STATUS_ADD) {
7042 int choice = GOT_PATCH_CHOICE_NONE;
7043 err = (*a->patch_cb)(&choice, a->patch_arg,
7044 status, ie->path, NULL, 1, 1);
7045 if (err)
7046 break;
7047 if (choice != GOT_PATCH_CHOICE_YES)
7048 break;
7049 } else {
7050 err = create_patched_content(&path_content, 0,
7051 staged_blob_id ? staged_blob_id : blob_id,
7052 ondisk_path, dirfd, de_name, ie->path,
7053 a->repo, a->patch_cb, a->patch_arg);
7054 if (err || path_content == NULL)
7055 break;
7058 err = got_object_blob_create(&new_staged_blob_id,
7059 path_content ? path_content : ondisk_path, a->repo);
7060 if (err)
7061 break;
7062 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7063 SHA1_DIGEST_LENGTH);
7064 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7065 stage = GOT_FILEIDX_STAGE_ADD;
7066 else
7067 stage = GOT_FILEIDX_STAGE_MODIFY;
7068 got_fileindex_entry_stage_set(ie, stage);
7069 a->staged_something = 1;
7070 if (a->status_cb == NULL)
7071 break;
7072 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7073 get_staged_status(ie), relpath, blob_id,
7074 new_staged_blob_id, NULL, dirfd, de_name);
7075 break;
7076 case GOT_STATUS_DELETE:
7077 if (staged_status == GOT_STATUS_DELETE)
7078 break;
7079 if (a->patch_cb) {
7080 int choice = GOT_PATCH_CHOICE_NONE;
7081 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7082 ie->path, NULL, 1, 1);
7083 if (err)
7084 break;
7085 if (choice == GOT_PATCH_CHOICE_NO)
7086 break;
7087 if (choice != GOT_PATCH_CHOICE_YES) {
7088 err = got_error(GOT_ERR_PATCH_CHOICE);
7089 break;
7092 stage = GOT_FILEIDX_STAGE_DELETE;
7093 got_fileindex_entry_stage_set(ie, stage);
7094 a->staged_something = 1;
7095 if (a->status_cb == NULL)
7096 break;
7097 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7098 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7099 de_name);
7100 break;
7101 case GOT_STATUS_NO_CHANGE:
7102 break;
7103 case GOT_STATUS_CONFLICT:
7104 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7105 break;
7106 case GOT_STATUS_NONEXISTENT:
7107 err = got_error_set_errno(ENOENT, relpath);
7108 break;
7109 default:
7110 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7111 break;
7114 if (path_content && unlink(path_content) == -1 && err == NULL)
7115 err = got_error_from_errno2("unlink", path_content);
7116 free(path_content);
7117 free(ondisk_path);
7118 free(new_staged_blob_id);
7119 return err;
7122 const struct got_error *
7123 got_worktree_stage(struct got_worktree *worktree,
7124 struct got_pathlist_head *paths,
7125 got_worktree_status_cb status_cb, void *status_arg,
7126 got_worktree_patch_cb patch_cb, void *patch_arg,
7127 struct got_repository *repo)
7129 const struct got_error *err = NULL, *sync_err, *unlockerr;
7130 struct got_pathlist_entry *pe;
7131 struct got_fileindex *fileindex = NULL;
7132 char *fileindex_path = NULL;
7133 struct got_reference *head_ref = NULL;
7134 struct got_object_id *head_commit_id = NULL;
7135 struct check_stage_ok_arg oka;
7136 struct stage_path_arg spa;
7138 err = lock_worktree(worktree, LOCK_EX);
7139 if (err)
7140 return err;
7142 err = got_ref_open(&head_ref, repo,
7143 got_worktree_get_head_ref_name(worktree), 0);
7144 if (err)
7145 goto done;
7146 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7147 if (err)
7148 goto done;
7149 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7150 if (err)
7151 goto done;
7153 /* Check pre-conditions before staging anything. */
7154 oka.head_commit_id = head_commit_id;
7155 oka.worktree = worktree;
7156 oka.fileindex = fileindex;
7157 oka.repo = repo;
7158 oka.have_changes = 0;
7159 TAILQ_FOREACH(pe, paths, entry) {
7160 err = worktree_status(worktree, pe->path, fileindex, repo,
7161 check_stage_ok, &oka, NULL, NULL, 0, 0);
7162 if (err)
7163 goto done;
7165 if (!oka.have_changes) {
7166 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7167 goto done;
7170 spa.worktree = worktree;
7171 spa.fileindex = fileindex;
7172 spa.repo = repo;
7173 spa.patch_cb = patch_cb;
7174 spa.patch_arg = patch_arg;
7175 spa.status_cb = status_cb;
7176 spa.status_arg = status_arg;
7177 spa.staged_something = 0;
7178 TAILQ_FOREACH(pe, paths, entry) {
7179 err = worktree_status(worktree, pe->path, fileindex, repo,
7180 stage_path, &spa, NULL, NULL, 0, 0);
7181 if (err)
7182 goto done;
7184 if (!spa.staged_something) {
7185 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7186 goto done;
7189 sync_err = sync_fileindex(fileindex, fileindex_path);
7190 if (sync_err && err == NULL)
7191 err = sync_err;
7192 done:
7193 if (head_ref)
7194 got_ref_close(head_ref);
7195 free(head_commit_id);
7196 free(fileindex_path);
7197 if (fileindex)
7198 got_fileindex_free(fileindex);
7199 unlockerr = lock_worktree(worktree, LOCK_SH);
7200 if (unlockerr && err == NULL)
7201 err = unlockerr;
7202 return err;
7205 struct unstage_path_arg {
7206 struct got_worktree *worktree;
7207 struct got_fileindex *fileindex;
7208 struct got_repository *repo;
7209 got_worktree_checkout_cb progress_cb;
7210 void *progress_arg;
7211 got_worktree_patch_cb patch_cb;
7212 void *patch_arg;
7215 static const struct got_error *
7216 create_unstaged_content(char **path_unstaged_content,
7217 char **path_new_staged_content, struct got_object_id *blob_id,
7218 struct got_object_id *staged_blob_id, const char *relpath,
7219 struct got_repository *repo,
7220 got_worktree_patch_cb patch_cb, void *patch_arg)
7222 const struct got_error *err;
7223 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7224 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7225 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7226 struct stat sb1, sb2;
7227 struct got_diff_changes *changes = NULL;
7228 struct got_diff_state *ds = NULL;
7229 struct got_diff_args *args = NULL;
7230 struct got_diff_change *change;
7231 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7232 int have_content = 0, have_rejected_content = 0;
7234 *path_unstaged_content = NULL;
7235 *path_new_staged_content = NULL;
7237 err = got_object_id_str(&label1, blob_id);
7238 if (err)
7239 return err;
7240 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7241 if (err)
7242 goto done;
7244 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7245 if (err)
7246 goto done;
7248 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7249 if (err)
7250 goto done;
7252 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7253 if (err)
7254 goto done;
7256 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7257 if (err)
7258 goto done;
7260 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7261 if (err)
7262 goto done;
7264 if (stat(path1, &sb1) == -1) {
7265 err = got_error_from_errno2("stat", path1);
7266 goto done;
7269 if (stat(path2, &sb2) == -1) {
7270 err = got_error_from_errno2("stat", path2);
7271 goto done;
7274 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7275 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7276 if (err)
7277 goto done;
7279 err = got_opentemp_named(path_unstaged_content, &outfile,
7280 "got-unstaged-content");
7281 if (err)
7282 goto done;
7283 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7284 "got-new-staged-content");
7285 if (err)
7286 goto done;
7288 if (fseek(f1, 0L, SEEK_SET) == -1) {
7289 err = got_ferror(f1, GOT_ERR_IO);
7290 goto done;
7292 if (fseek(f2, 0L, SEEK_SET) == -1) {
7293 err = got_ferror(f2, GOT_ERR_IO);
7294 goto done;
7296 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7297 int choice;
7298 err = apply_or_reject_change(&choice, change, ++n,
7299 changes->nchanges, ds, args, diff_flags, relpath,
7300 f1, f2, &line_cur1, &line_cur2,
7301 outfile, rejectfile, patch_cb, patch_arg);
7302 if (err)
7303 goto done;
7304 if (choice == GOT_PATCH_CHOICE_YES)
7305 have_content = 1;
7306 else
7307 have_rejected_content = 1;
7308 if (choice == GOT_PATCH_CHOICE_QUIT)
7309 break;
7311 if (have_content || have_rejected_content)
7312 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7313 outfile, rejectfile);
7314 done:
7315 free(label1);
7316 if (blob)
7317 got_object_blob_close(blob);
7318 if (staged_blob)
7319 got_object_blob_close(staged_blob);
7320 if (f1 && fclose(f1) == EOF && err == NULL)
7321 err = got_error_from_errno2("fclose", path1);
7322 if (f2 && fclose(f2) == EOF && err == NULL)
7323 err = got_error_from_errno2("fclose", path2);
7324 if (outfile && fclose(outfile) == EOF && err == NULL)
7325 err = got_error_from_errno2("fclose", *path_unstaged_content);
7326 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7327 err = got_error_from_errno2("fclose", *path_new_staged_content);
7328 if (path1 && unlink(path1) == -1 && err == NULL)
7329 err = got_error_from_errno2("unlink", path1);
7330 if (path2 && unlink(path2) == -1 && err == NULL)
7331 err = got_error_from_errno2("unlink", path2);
7332 if (err || !have_content) {
7333 if (*path_unstaged_content &&
7334 unlink(*path_unstaged_content) == -1 && err == NULL)
7335 err = got_error_from_errno2("unlink",
7336 *path_unstaged_content);
7337 free(*path_unstaged_content);
7338 *path_unstaged_content = NULL;
7340 if (err || !have_rejected_content) {
7341 if (*path_new_staged_content &&
7342 unlink(*path_new_staged_content) == -1 && err == NULL)
7343 err = got_error_from_errno2("unlink",
7344 *path_new_staged_content);
7345 free(*path_new_staged_content);
7346 *path_new_staged_content = NULL;
7348 free(args);
7349 if (ds) {
7350 got_diff_state_free(ds);
7351 free(ds);
7353 if (changes)
7354 got_diff_free_changes(changes);
7355 free(path1);
7356 free(path2);
7357 return err;
7360 static const struct got_error *
7361 unstage_path(void *arg, unsigned char status,
7362 unsigned char staged_status, const char *relpath,
7363 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7364 struct got_object_id *commit_id, int dirfd, const char *de_name)
7366 const struct got_error *err = NULL;
7367 struct unstage_path_arg *a = arg;
7368 struct got_fileindex_entry *ie;
7369 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7370 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7371 char *path_new_staged_content = NULL;
7372 char *id_str = NULL, *label_orig = NULL;
7373 int local_changes_subsumed;
7374 struct stat sb;
7376 if (staged_status != GOT_STATUS_ADD &&
7377 staged_status != GOT_STATUS_MODIFY &&
7378 staged_status != GOT_STATUS_DELETE)
7379 return NULL;
7381 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7382 if (ie == NULL)
7383 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7385 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7386 == -1)
7387 return got_error_from_errno("asprintf");
7389 err = got_object_id_str(&id_str,
7390 commit_id ? commit_id : a->worktree->base_commit_id);
7391 if (err)
7392 goto done;
7393 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7394 id_str) == -1) {
7395 err = got_error_from_errno("asprintf");
7396 goto done;
7399 switch (staged_status) {
7400 case GOT_STATUS_MODIFY:
7401 err = got_object_open_as_blob(&blob_base, a->repo,
7402 blob_id, 8192);
7403 if (err)
7404 break;
7405 /* fall through */
7406 case GOT_STATUS_ADD:
7407 if (a->patch_cb) {
7408 if (staged_status == GOT_STATUS_ADD) {
7409 int choice = GOT_PATCH_CHOICE_NONE;
7410 err = (*a->patch_cb)(&choice, a->patch_arg,
7411 staged_status, ie->path, NULL, 1, 1);
7412 if (err)
7413 break;
7414 if (choice != GOT_PATCH_CHOICE_YES)
7415 break;
7416 } else {
7417 err = create_unstaged_content(
7418 &path_unstaged_content,
7419 &path_new_staged_content, blob_id,
7420 staged_blob_id, ie->path, a->repo,
7421 a->patch_cb, a->patch_arg);
7422 if (err || path_unstaged_content == NULL)
7423 break;
7424 if (path_new_staged_content) {
7425 err = got_object_blob_create(
7426 &staged_blob_id,
7427 path_new_staged_content,
7428 a->repo);
7429 if (err)
7430 break;
7431 memcpy(ie->staged_blob_sha1,
7432 staged_blob_id->sha1,
7433 SHA1_DIGEST_LENGTH);
7435 err = merge_file(&local_changes_subsumed,
7436 a->worktree, blob_base, ondisk_path,
7437 relpath, got_fileindex_perms_to_st(ie),
7438 path_unstaged_content, label_orig,
7439 "unstaged", a->repo, a->progress_cb,
7440 a->progress_arg);
7441 if (err == NULL &&
7442 path_new_staged_content == NULL)
7443 got_fileindex_entry_stage_set(ie,
7444 GOT_FILEIDX_STAGE_NONE);
7445 break; /* Done with this file. */
7448 err = got_object_open_as_blob(&blob_staged, a->repo,
7449 staged_blob_id, 8192);
7450 if (err)
7451 break;
7452 err = merge_blob(&local_changes_subsumed, a->worktree,
7453 blob_base, ondisk_path, relpath,
7454 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7455 commit_id ? commit_id : a->worktree->base_commit_id,
7456 a->repo, a->progress_cb, a->progress_arg);
7457 if (err == NULL)
7458 got_fileindex_entry_stage_set(ie,
7459 GOT_FILEIDX_STAGE_NONE);
7460 break;
7461 case GOT_STATUS_DELETE:
7462 if (a->patch_cb) {
7463 int choice = GOT_PATCH_CHOICE_NONE;
7464 err = (*a->patch_cb)(&choice, a->patch_arg,
7465 staged_status, ie->path, NULL, 1, 1);
7466 if (err)
7467 break;
7468 if (choice == GOT_PATCH_CHOICE_NO)
7469 break;
7470 if (choice != GOT_PATCH_CHOICE_YES) {
7471 err = got_error(GOT_ERR_PATCH_CHOICE);
7472 break;
7475 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7476 err = get_file_status(&status, &sb, ie, ondisk_path,
7477 dirfd, de_name, a->repo);
7478 if (err)
7479 break;
7480 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7481 break;
7483 done:
7484 free(ondisk_path);
7485 if (path_unstaged_content &&
7486 unlink(path_unstaged_content) == -1 && err == NULL)
7487 err = got_error_from_errno2("unlink", path_unstaged_content);
7488 if (path_new_staged_content &&
7489 unlink(path_new_staged_content) == -1 && err == NULL)
7490 err = got_error_from_errno2("unlink", path_new_staged_content);
7491 free(path_unstaged_content);
7492 free(path_new_staged_content);
7493 if (blob_base)
7494 got_object_blob_close(blob_base);
7495 if (blob_staged)
7496 got_object_blob_close(blob_staged);
7497 free(id_str);
7498 free(label_orig);
7499 return err;
7502 const struct got_error *
7503 got_worktree_unstage(struct got_worktree *worktree,
7504 struct got_pathlist_head *paths,
7505 got_worktree_checkout_cb progress_cb, void *progress_arg,
7506 got_worktree_patch_cb patch_cb, void *patch_arg,
7507 struct got_repository *repo)
7509 const struct got_error *err = NULL, *sync_err, *unlockerr;
7510 struct got_pathlist_entry *pe;
7511 struct got_fileindex *fileindex = NULL;
7512 char *fileindex_path = NULL;
7513 struct unstage_path_arg upa;
7515 err = lock_worktree(worktree, LOCK_EX);
7516 if (err)
7517 return err;
7519 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7520 if (err)
7521 goto done;
7523 upa.worktree = worktree;
7524 upa.fileindex = fileindex;
7525 upa.repo = repo;
7526 upa.progress_cb = progress_cb;
7527 upa.progress_arg = progress_arg;
7528 upa.patch_cb = patch_cb;
7529 upa.patch_arg = patch_arg;
7530 TAILQ_FOREACH(pe, paths, entry) {
7531 err = worktree_status(worktree, pe->path, fileindex, repo,
7532 unstage_path, &upa, NULL, NULL, 0, 0);
7533 if (err)
7534 goto done;
7537 sync_err = sync_fileindex(fileindex, fileindex_path);
7538 if (sync_err && err == NULL)
7539 err = sync_err;
7540 done:
7541 free(fileindex_path);
7542 if (fileindex)
7543 got_fileindex_free(fileindex);
7544 unlockerr = lock_worktree(worktree, LOCK_SH);
7545 if (unlockerr && err == NULL)
7546 err = unlockerr;
7547 return err;