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 %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
928 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
929 deriv_target ? deriv_target : "(symlink was deleted)",
930 orig_target ? label_orig : "",
931 orig_target ? "\n" : "",
932 orig_target ? orig_target : "",
933 orig_target ? "\n" : "",
934 GOT_DIFF_CONFLICT_MARKER_SEP,
935 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
936 err = got_error_from_errno2("fprintf", path);
937 goto done;
940 if (unlink(ondisk_path) == -1) {
941 err = got_error_from_errno2("unlink", ondisk_path);
942 goto done;
944 if (rename(path, ondisk_path) == -1) {
945 err = got_error_from_errno3("rename", path, ondisk_path);
946 goto done;
948 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
949 err = got_error_from_errno2("chmod", ondisk_path);
950 goto done;
952 done:
953 if (f != NULL && fclose(f) == EOF && err == NULL)
954 err = got_error_from_errno2("fclose", path);
955 free(path);
956 free(id_str);
957 free(label_deriv);
958 return err;
961 /* forward declaration */
962 static const struct got_error *
963 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
964 const char *, const char *, uint16_t, const char *,
965 struct got_blob_object *, struct got_object_id *,
966 struct got_repository *, got_worktree_checkout_cb, void *);
968 /*
969 * Merge a symlink into the work tree, where blob_orig acts as the common
970 * ancestor, deriv_target is the link target of the first derived version,
971 * and the symlink on disk acts as the second derived version.
972 * Assume that contents of both blobs represent symlinks.
973 */
974 static const struct got_error *
975 merge_symlink(struct got_worktree *worktree,
976 struct got_blob_object *blob_orig, const char *ondisk_path,
977 const char *path, const char *label_orig, const char *deriv_target,
978 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
979 got_worktree_checkout_cb progress_cb, void *progress_arg)
981 const struct got_error *err = NULL;
982 char *ancestor_target = NULL;
983 struct stat sb;
984 ssize_t ondisk_len, deriv_len;
985 char ondisk_target[PATH_MAX];
986 int have_local_change = 0;
987 int have_incoming_change = 0;
989 if (lstat(ondisk_path, &sb) == -1)
990 return got_error_from_errno2("lstat", ondisk_path);
992 ondisk_len = readlink(ondisk_path, ondisk_target,
993 sizeof(ondisk_target));
994 if (ondisk_len == -1) {
995 err = got_error_from_errno2("readlink",
996 ondisk_path);
997 goto done;
999 ondisk_target[ondisk_len] = '\0';
1001 if (blob_orig) {
1002 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1003 if (err)
1004 goto done;
1007 if (ancestor_target == NULL ||
1008 (ondisk_len != strlen(ancestor_target) ||
1009 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1010 have_local_change = 1;
1012 deriv_len = strlen(deriv_target);
1013 if (ancestor_target == NULL ||
1014 (deriv_len != strlen(ancestor_target) ||
1015 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1016 have_incoming_change = 1;
1018 if (!have_local_change && !have_incoming_change) {
1019 if (ancestor_target) {
1020 /* Both sides made the same change. */
1021 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1022 path);
1023 } else if (deriv_len == ondisk_len &&
1024 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1025 /* Both sides added the same symlink. */
1026 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1027 path);
1028 } else {
1029 /* Both sides added symlinks which don't match. */
1030 err = install_symlink_conflict(deriv_target,
1031 deriv_base_commit_id, ancestor_target,
1032 label_orig, ondisk_target, ondisk_path);
1033 if (err)
1034 goto done;
1035 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1036 path);
1038 } else if (!have_local_change && have_incoming_change) {
1039 /* Apply the incoming change. */
1040 err = update_symlink(ondisk_path, deriv_target,
1041 strlen(deriv_target));
1042 if (err)
1043 goto done;
1044 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1045 } else if (have_local_change && have_incoming_change) {
1046 if (deriv_len == ondisk_len &&
1047 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1048 /* Both sides made the same change. */
1049 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1050 path);
1051 } else {
1052 err = install_symlink_conflict(deriv_target,
1053 deriv_base_commit_id, ancestor_target, label_orig,
1054 ondisk_target, ondisk_path);
1055 if (err)
1056 goto done;
1057 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1058 path);
1062 done:
1063 free(ancestor_target);
1064 return err;
1068 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1069 * blob_deriv acts as the first derived version, and the file on disk
1070 * acts as the second derived version.
1072 static const struct got_error *
1073 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1074 struct got_blob_object *blob_orig, const char *ondisk_path,
1075 const char *path, uint16_t st_mode, const char *label_orig,
1076 struct got_blob_object *blob_deriv,
1077 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1078 got_worktree_checkout_cb progress_cb, void *progress_arg)
1080 const struct got_error *err = NULL;
1081 FILE *f_deriv = NULL;
1082 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1083 char *label_deriv = NULL, *parent;
1085 *local_changes_subsumed = 0;
1087 parent = dirname(ondisk_path);
1088 if (parent == NULL)
1089 return got_error_from_errno2("dirname", ondisk_path);
1091 free(base_path);
1092 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1093 err = got_error_from_errno("asprintf");
1094 base_path = NULL;
1095 goto done;
1098 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1099 if (err)
1100 goto done;
1101 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1102 blob_deriv);
1103 if (err)
1104 goto done;
1106 err = got_object_id_str(&id_str, deriv_base_commit_id);
1107 if (err)
1108 goto done;
1109 if (asprintf(&label_deriv, "%s: commit %s",
1110 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1111 err = got_error_from_errno("asprintf");
1112 goto done;
1115 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1116 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1117 label_deriv, repo, progress_cb, progress_arg);
1118 done:
1119 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1120 err = got_error_from_errno("fclose");
1121 free(base_path);
1122 if (blob_deriv_path) {
1123 unlink(blob_deriv_path);
1124 free(blob_deriv_path);
1126 free(id_str);
1127 free(label_deriv);
1128 return err;
1131 static const struct got_error *
1132 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1133 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1134 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1136 const struct got_error *err = NULL;
1137 struct got_fileindex_entry *new_ie;
1139 *new_iep = NULL;
1141 err = got_fileindex_entry_alloc(&new_ie, path);
1142 if (err)
1143 return err;
1145 err = got_fileindex_entry_update(new_ie, ondisk_path,
1146 blob_id->sha1, base_commit_id->sha1, 1);
1147 if (err)
1148 goto done;
1150 err = got_fileindex_entry_add(fileindex, new_ie);
1151 done:
1152 if (err)
1153 got_fileindex_entry_free(new_ie);
1154 else
1155 *new_iep = new_ie;
1156 return err;
1159 static mode_t
1160 get_ondisk_perms(int executable, mode_t st_mode)
1162 mode_t xbits = S_IXUSR;
1164 if (executable) {
1165 /* Map read bits to execute bits. */
1166 if (st_mode & S_IRGRP)
1167 xbits |= S_IXGRP;
1168 if (st_mode & S_IROTH)
1169 xbits |= S_IXOTH;
1170 return st_mode | xbits;
1173 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1176 /* forward declaration */
1177 static const struct got_error *
1178 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1179 const char *path, mode_t te_mode, mode_t st_mode,
1180 struct got_blob_object *blob, int restoring_missing_file,
1181 int reverting_versioned_file, int installing_bad_symlink,
1182 int path_is_unversioned, struct got_repository *repo,
1183 got_worktree_checkout_cb progress_cb, void *progress_arg);
1186 * This function assumes that the provided symlink target points at a
1187 * safe location in the work tree!
1189 static const struct got_error *
1190 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1191 size_t target_len)
1193 const struct got_error *err = NULL;
1194 ssize_t elen;
1195 char etarget[PATH_MAX];
1196 int fd;
1199 * "Bad" symlinks (those pointing outside the work tree or into the
1200 * .got directory) are installed in the work tree as a regular file
1201 * which contains the bad symlink target path.
1202 * The new symlink target has already been checked for safety by our
1203 * caller. If we can successfully open a regular file then we simply
1204 * replace this file with a symlink below.
1206 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1207 if (fd == -1) {
1208 if (errno != ELOOP)
1209 return got_error_from_errno2("open", ondisk_path);
1211 /* We are updating an existing on-disk symlink. */
1212 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1213 if (elen == -1)
1214 return got_error_from_errno2("readlink", ondisk_path);
1216 if (elen == target_len &&
1217 memcmp(etarget, target_path, target_len) == 0)
1218 return NULL; /* nothing to do */
1221 err = update_symlink(ondisk_path, target_path, target_len);
1222 if (fd != -1 && close(fd) == -1 && err == NULL)
1223 err = got_error_from_errno2("close", ondisk_path);
1224 return err;
1227 static const struct got_error *
1228 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1229 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1231 const struct got_error *err = NULL;
1232 char canonpath[PATH_MAX];
1233 char *path_got = NULL;
1235 *is_bad_symlink = 0;
1237 if (target_len >= sizeof(canonpath)) {
1238 *is_bad_symlink = 1;
1239 return NULL;
1243 * We do not use realpath(3) to resolve the symlink's target
1244 * path because we don't want to resolve symlinks recursively.
1245 * Instead we make the path absolute and then canonicalize it.
1246 * Relative symlink target lookup should begin at the directory
1247 * in which the blob object is being installed.
1249 if (!got_path_is_absolute(target_path)) {
1250 char *abspath;
1251 char *parent = dirname(ondisk_path);
1252 if (parent == NULL)
1253 return got_error_from_errno2("dirname", ondisk_path);
1254 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1255 return got_error_from_errno("asprintf");
1256 if (strlen(abspath) >= sizeof(canonpath)) {
1257 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1258 free(abspath);
1259 return err;
1261 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1262 free(abspath);
1263 if (err)
1264 return err;
1265 } else {
1266 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1267 if (err)
1268 return err;
1271 /* Only allow symlinks pointing at paths within the work tree. */
1272 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1273 *is_bad_symlink = 1;
1274 return NULL;
1277 /* Do not allow symlinks pointing into the .got directory. */
1278 if (asprintf(&path_got, "%s/%s", wtroot_path,
1279 GOT_WORKTREE_GOT_DIR) == -1)
1280 return got_error_from_errno("asprintf");
1281 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1282 *is_bad_symlink = 1;
1284 free(path_got);
1285 return NULL;
1288 static const struct got_error *
1289 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1290 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1291 int restoring_missing_file, int reverting_versioned_file,
1292 int path_is_unversioned, struct got_repository *repo,
1293 got_worktree_checkout_cb progress_cb, void *progress_arg)
1295 const struct got_error *err = NULL;
1296 char target_path[PATH_MAX];
1297 size_t len, target_len = 0;
1298 char *path_got = NULL;
1299 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1300 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1302 *is_bad_symlink = 0;
1305 * Blob object content specifies the target path of the link.
1306 * If a symbolic link cannot be installed we instead create
1307 * a regular file which contains the link target path stored
1308 * in the blob object.
1310 do {
1311 err = got_object_blob_read_block(&len, blob);
1312 if (len + target_len >= sizeof(target_path)) {
1313 /* Path too long; install as a regular file. */
1314 *is_bad_symlink = 1;
1315 got_object_blob_rewind(blob);
1316 return install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file,
1319 1, path_is_unversioned, repo, progress_cb,
1320 progress_arg);
1322 if (len > 0) {
1323 /* Skip blob object header first time around. */
1324 memcpy(target_path + target_len, buf + hdrlen,
1325 len - hdrlen);
1326 target_len += len - hdrlen;
1327 hdrlen = 0;
1329 } while (len != 0);
1330 target_path[target_len] = '\0';
1332 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1333 ondisk_path, worktree->root_path);
1334 if (err)
1335 return err;
1337 if (*is_bad_symlink) {
1338 /* install as a regular file */
1339 *is_bad_symlink = 1;
1340 got_object_blob_rewind(blob);
1341 err = install_blob(worktree, ondisk_path, path,
1342 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1343 restoring_missing_file, reverting_versioned_file, 1,
1344 path_is_unversioned, repo, progress_cb, progress_arg);
1345 goto done;
1348 if (symlink(target_path, ondisk_path) == -1) {
1349 if (errno == EEXIST) {
1350 if (path_is_unversioned) {
1351 err = (*progress_cb)(progress_arg,
1352 GOT_STATUS_UNVERSIONED, path);
1353 goto done;
1355 err = replace_existing_symlink(ondisk_path,
1356 target_path, target_len);
1357 if (err)
1358 goto done;
1359 if (progress_cb) {
1360 err = (*progress_cb)(progress_arg,
1361 reverting_versioned_file ?
1362 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1363 path);
1365 goto done; /* Nothing else to do. */
1368 if (errno == ENOENT) {
1369 char *parent = dirname(ondisk_path);
1370 if (parent == NULL) {
1371 err = got_error_from_errno2("dirname",
1372 ondisk_path);
1373 goto done;
1375 err = add_dir_on_disk(worktree, parent);
1376 if (err)
1377 goto done;
1379 * Retry, and fall through to error handling
1380 * below if this second attempt fails.
1382 if (symlink(target_path, ondisk_path) != -1) {
1383 err = NULL; /* success */
1384 goto done;
1388 /* Handle errors from first or second creation attempt. */
1389 if (errno == ENAMETOOLONG) {
1390 /* bad target path; install as a regular file */
1391 *is_bad_symlink = 1;
1392 got_object_blob_rewind(blob);
1393 err = install_blob(worktree, ondisk_path, path,
1394 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1395 restoring_missing_file, reverting_versioned_file, 1,
1396 path_is_unversioned, repo,
1397 progress_cb, progress_arg);
1398 } else if (errno == ENOTDIR) {
1399 err = got_error_path(ondisk_path,
1400 GOT_ERR_FILE_OBSTRUCTED);
1401 } else {
1402 err = got_error_from_errno3("symlink",
1403 target_path, ondisk_path);
1405 } else if (progress_cb)
1406 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1407 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1408 done:
1409 free(path_got);
1410 return err;
1413 static const struct got_error *
1414 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1415 const char *path, mode_t te_mode, mode_t st_mode,
1416 struct got_blob_object *blob, int restoring_missing_file,
1417 int reverting_versioned_file, int installing_bad_symlink,
1418 int path_is_unversioned, struct got_repository *repo,
1419 got_worktree_checkout_cb progress_cb, void *progress_arg)
1421 const struct got_error *err = NULL;
1422 int fd = -1;
1423 size_t len, hdrlen;
1424 int update = 0;
1425 char *tmppath = NULL;
1427 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1428 GOT_DEFAULT_FILE_MODE);
1429 if (fd == -1) {
1430 if (errno == ENOENT) {
1431 char *parent = dirname(path);
1432 if (parent == NULL)
1433 return got_error_from_errno2("dirname", path);
1434 err = add_dir_on_disk(worktree, parent);
1435 if (err)
1436 return err;
1437 fd = open(ondisk_path,
1438 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 GOT_DEFAULT_FILE_MODE);
1440 if (fd == -1)
1441 return got_error_from_errno2("open",
1442 ondisk_path);
1443 } else if (errno == EEXIST) {
1444 if (path_is_unversioned) {
1445 err = (*progress_cb)(progress_arg,
1446 GOT_STATUS_UNVERSIONED, path);
1447 goto done;
1449 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1450 /* TODO file is obstructed; do something */
1451 err = got_error_path(ondisk_path,
1452 GOT_ERR_FILE_OBSTRUCTED);
1453 goto done;
1454 } else {
1455 err = got_opentemp_named_fd(&tmppath, &fd,
1456 ondisk_path);
1457 if (err)
1458 goto done;
1459 update = 1;
1461 } else
1462 return got_error_from_errno2("open", ondisk_path);
1465 if (progress_cb) {
1466 if (restoring_missing_file)
1467 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1468 path);
1469 else if (reverting_versioned_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1471 path);
1472 else
1473 err = (*progress_cb)(progress_arg,
1474 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1475 if (err)
1476 goto done;
1479 hdrlen = got_object_blob_get_hdrlen(blob);
1480 do {
1481 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 err = got_object_blob_read_block(&len, blob);
1483 if (err)
1484 break;
1485 if (len > 0) {
1486 /* Skip blob object header first time around. */
1487 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1488 if (outlen == -1) {
1489 err = got_error_from_errno("write");
1490 goto done;
1491 } else if (outlen != len - hdrlen) {
1492 err = got_error(GOT_ERR_IO);
1493 goto done;
1495 hdrlen = 0;
1497 } while (len != 0);
1499 if (fsync(fd) != 0) {
1500 err = got_error_from_errno("fsync");
1501 goto done;
1504 if (update) {
1505 if (rename(tmppath, ondisk_path) != 0) {
1506 err = got_error_from_errno3("rename", tmppath,
1507 ondisk_path);
1508 unlink(tmppath);
1509 goto done;
1513 if (chmod(ondisk_path,
1514 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1515 err = got_error_from_errno2("chmod", ondisk_path);
1516 goto done;
1519 done:
1520 if (fd != -1 && close(fd) != 0 && err == NULL)
1521 err = got_error_from_errno("close");
1522 free(tmppath);
1523 return err;
1526 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1527 static const struct got_error *
1528 get_modified_file_content_status(unsigned char *status, FILE *f)
1530 const struct got_error *err = NULL;
1531 const char *markers[3] = {
1532 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1533 GOT_DIFF_CONFLICT_MARKER_SEP,
1534 GOT_DIFF_CONFLICT_MARKER_END
1536 int i = 0;
1537 char *line;
1538 size_t len;
1539 const char delim[3] = {'\0', '\0', '\0'};
1541 while (*status == GOT_STATUS_MODIFY) {
1542 line = fparseln(f, &len, NULL, delim, 0);
1543 if (line == NULL) {
1544 if (feof(f))
1545 break;
1546 err = got_ferror(f, GOT_ERR_IO);
1547 break;
1550 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1551 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1552 == 0)
1553 *status = GOT_STATUS_CONFLICT;
1554 else
1555 i++;
1559 return err;
1562 static int
1563 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1565 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1566 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1569 static int
1570 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1572 return !(ie->ctime_sec == sb->st_ctime &&
1573 ie->ctime_nsec == sb->st_ctimensec &&
1574 ie->mtime_sec == sb->st_mtime &&
1575 ie->mtime_nsec == sb->st_mtimensec &&
1576 ie->size == (sb->st_size & 0xffffffff) &&
1577 !xbit_differs(ie, sb->st_mode));
1580 static unsigned char
1581 get_staged_status(struct got_fileindex_entry *ie)
1583 switch (got_fileindex_entry_stage_get(ie)) {
1584 case GOT_FILEIDX_STAGE_ADD:
1585 return GOT_STATUS_ADD;
1586 case GOT_FILEIDX_STAGE_DELETE:
1587 return GOT_STATUS_DELETE;
1588 case GOT_FILEIDX_STAGE_MODIFY:
1589 return GOT_STATUS_MODIFY;
1590 default:
1591 return GOT_STATUS_NO_CHANGE;
1595 static const struct got_error *
1596 get_symlink_modification_status(unsigned char *status,
1597 struct got_fileindex_entry *ie, const char *abspath,
1598 int dirfd, const char *de_name, struct got_blob_object *blob)
1600 const struct got_error *err = NULL;
1601 char target_path[PATH_MAX];
1602 char etarget[PATH_MAX];
1603 ssize_t elen;
1604 size_t len, target_len = 0;
1605 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1606 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1608 *status = GOT_STATUS_NO_CHANGE;
1610 /* Blob object content specifies the target path of the link. */
1611 do {
1612 err = got_object_blob_read_block(&len, blob);
1613 if (err)
1614 return err;
1615 if (len + target_len >= sizeof(target_path)) {
1617 * Should not happen. The blob contents were OK
1618 * when this symlink was installed.
1620 return got_error(GOT_ERR_NO_SPACE);
1622 if (len > 0) {
1623 /* Skip blob object header first time around. */
1624 memcpy(target_path + target_len, buf + hdrlen,
1625 len - hdrlen);
1626 target_len += len - hdrlen;
1627 hdrlen = 0;
1629 } while (len != 0);
1630 target_path[target_len] = '\0';
1632 if (dirfd != -1) {
1633 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1634 if (elen == -1)
1635 return got_error_from_errno2("readlinkat", abspath);
1636 } else {
1637 elen = readlink(abspath, etarget, sizeof(etarget));
1638 if (elen == -1)
1639 return got_error_from_errno2("readlink", abspath);
1642 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1643 *status = GOT_STATUS_MODIFY;
1645 return NULL;
1648 static const struct got_error *
1649 get_file_status(unsigned char *status, struct stat *sb,
1650 struct got_fileindex_entry *ie, const char *abspath,
1651 int dirfd, const char *de_name, struct got_repository *repo)
1653 const struct got_error *err = NULL;
1654 struct got_object_id id;
1655 size_t hdrlen;
1656 int fd = -1;
1657 FILE *f = NULL;
1658 uint8_t fbuf[8192];
1659 struct got_blob_object *blob = NULL;
1660 size_t flen, blen;
1661 unsigned char staged_status = get_staged_status(ie);
1663 *status = GOT_STATUS_NO_CHANGE;
1666 * Whenever the caller provides a directory descriptor and a
1667 * directory entry name for the file, use them! This prevents
1668 * race conditions if filesystem paths change beneath our feet.
1670 if (dirfd != -1) {
1671 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1672 if (errno == ENOENT) {
1673 if (got_fileindex_entry_has_file_on_disk(ie))
1674 *status = GOT_STATUS_MISSING;
1675 else
1676 *status = GOT_STATUS_DELETE;
1677 goto done;
1679 err = got_error_from_errno2("fstatat", abspath);
1680 goto done;
1682 } else {
1683 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1684 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1685 return got_error_from_errno2("open", abspath);
1686 else if (fd == -1 && errno == ELOOP) {
1687 if (lstat(abspath, sb) == -1)
1688 return got_error_from_errno2("lstat", abspath);
1689 } else if (fd == -1 || fstat(fd, sb) == -1) {
1690 if (errno == ENOENT) {
1691 if (got_fileindex_entry_has_file_on_disk(ie))
1692 *status = GOT_STATUS_MISSING;
1693 else
1694 *status = GOT_STATUS_DELETE;
1695 goto done;
1697 err = got_error_from_errno2("fstat", abspath);
1698 goto done;
1702 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1703 *status = GOT_STATUS_OBSTRUCTED;
1704 goto done;
1707 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1708 *status = GOT_STATUS_DELETE;
1709 goto done;
1710 } else if (!got_fileindex_entry_has_blob(ie) &&
1711 staged_status != GOT_STATUS_ADD) {
1712 *status = GOT_STATUS_ADD;
1713 goto done;
1716 if (!stat_info_differs(ie, sb))
1717 goto done;
1719 if (S_ISLNK(sb->st_mode) &&
1720 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1721 *status = GOT_STATUS_MODIFY;
1722 goto done;
1725 if (staged_status == GOT_STATUS_MODIFY ||
1726 staged_status == GOT_STATUS_ADD)
1727 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1728 else
1729 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1731 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1732 if (err)
1733 goto done;
1735 if (S_ISLNK(sb->st_mode)) {
1736 err = get_symlink_modification_status(status, ie,
1737 abspath, dirfd, de_name, blob);
1738 goto done;
1741 if (dirfd != -1) {
1742 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1743 if (fd == -1) {
1744 err = got_error_from_errno2("openat", abspath);
1745 goto done;
1749 f = fdopen(fd, "r");
1750 if (f == NULL) {
1751 err = got_error_from_errno2("fdopen", abspath);
1752 goto done;
1754 fd = -1;
1755 hdrlen = got_object_blob_get_hdrlen(blob);
1756 for (;;) {
1757 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1758 err = got_object_blob_read_block(&blen, blob);
1759 if (err)
1760 goto done;
1761 /* Skip length of blob object header first time around. */
1762 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1763 if (flen == 0 && ferror(f)) {
1764 err = got_error_from_errno("fread");
1765 goto done;
1767 if (blen == 0) {
1768 if (flen != 0)
1769 *status = GOT_STATUS_MODIFY;
1770 break;
1771 } else if (flen == 0) {
1772 if (blen != 0)
1773 *status = GOT_STATUS_MODIFY;
1774 break;
1775 } else if (blen - hdrlen == flen) {
1776 /* Skip blob object header first time around. */
1777 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1778 *status = GOT_STATUS_MODIFY;
1779 break;
1781 } else {
1782 *status = GOT_STATUS_MODIFY;
1783 break;
1785 hdrlen = 0;
1788 if (*status == GOT_STATUS_MODIFY) {
1789 rewind(f);
1790 err = get_modified_file_content_status(status, f);
1791 } else if (xbit_differs(ie, sb->st_mode))
1792 *status = GOT_STATUS_MODE_CHANGE;
1793 done:
1794 if (blob)
1795 got_object_blob_close(blob);
1796 if (f != NULL && fclose(f) == EOF && err == NULL)
1797 err = got_error_from_errno2("fclose", abspath);
1798 if (fd != -1 && close(fd) == -1 && err == NULL)
1799 err = got_error_from_errno2("close", abspath);
1800 return err;
1804 * Update timestamps in the file index if a file is unmodified and
1805 * we had to run a full content comparison to find out.
1807 static const struct got_error *
1808 sync_timestamps(char *ondisk_path, unsigned char status,
1809 struct got_fileindex_entry *ie, struct stat *sb)
1811 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1812 return got_fileindex_entry_update(ie, ondisk_path,
1813 ie->blob_sha1, ie->commit_sha1, 1);
1815 return NULL;
1818 static const struct got_error *
1819 update_blob(struct got_worktree *worktree,
1820 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1821 struct got_tree_entry *te, const char *path,
1822 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1823 void *progress_arg)
1825 const struct got_error *err = NULL;
1826 struct got_blob_object *blob = NULL;
1827 char *ondisk_path;
1828 unsigned char status = GOT_STATUS_NO_CHANGE;
1829 struct stat sb;
1831 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 return got_error_from_errno("asprintf");
1834 if (ie) {
1835 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1837 goto done;
1839 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1840 repo);
1841 if (err)
1842 goto done;
1843 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 sb.st_mode = got_fileindex_perms_to_st(ie);
1845 } else {
1846 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1847 status = GOT_STATUS_UNVERSIONED;
1850 if (status == GOT_STATUS_OBSTRUCTED) {
1851 err = (*progress_cb)(progress_arg, status, path);
1852 goto done;
1854 if (status == GOT_STATUS_CONFLICT) {
1855 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1856 path);
1857 goto done;
1860 if (ie && status != GOT_STATUS_MISSING &&
1861 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1862 if (got_fileindex_entry_has_commit(ie) &&
1863 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1864 SHA1_DIGEST_LENGTH) == 0) {
1865 err = sync_timestamps(ondisk_path, status, ie, &sb);
1866 if (err)
1867 goto done;
1868 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1869 path);
1870 goto done;
1872 if (got_fileindex_entry_has_blob(ie) &&
1873 memcmp(ie->blob_sha1, te->id.sha1,
1874 SHA1_DIGEST_LENGTH) == 0) {
1875 err = sync_timestamps(ondisk_path, status, ie, &sb);
1876 goto done;
1880 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1881 if (err)
1882 goto done;
1884 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1885 int update_timestamps;
1886 struct got_blob_object *blob2 = NULL;
1887 char *label_orig = NULL;
1888 if (got_fileindex_entry_has_blob(ie)) {
1889 struct got_object_id id2;
1890 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1891 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1892 if (err)
1893 goto done;
1895 if (got_fileindex_entry_has_commit(ie)) {
1896 char id_str[SHA1_DIGEST_STRING_LENGTH];
1897 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1898 sizeof(id_str)) == NULL) {
1899 err = got_error_path(id_str,
1900 GOT_ERR_BAD_OBJ_ID_STR);
1901 goto done;
1903 if (asprintf(&label_orig, "%s: commit %s",
1904 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1905 err = got_error_from_errno("asprintf");
1906 goto done;
1909 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1910 char *link_target;
1911 err = got_object_blob_read_to_str(&link_target, blob);
1912 if (err)
1913 goto done;
1914 err = merge_symlink(worktree, blob2, ondisk_path, path,
1915 label_orig, link_target, worktree->base_commit_id,
1916 repo, progress_cb, progress_arg);
1917 free(link_target);
1918 } else {
1919 err = merge_blob(&update_timestamps, worktree, blob2,
1920 ondisk_path, path, sb.st_mode, label_orig, blob,
1921 worktree->base_commit_id, repo,
1922 progress_cb, progress_arg);
1924 free(label_orig);
1925 if (blob2)
1926 got_object_blob_close(blob2);
1927 if (err)
1928 goto done;
1930 * Do not update timestamps of files with local changes.
1931 * Otherwise, a future status walk would treat them as
1932 * unmodified files again.
1934 err = got_fileindex_entry_update(ie, ondisk_path,
1935 blob->id.sha1, worktree->base_commit_id->sha1,
1936 update_timestamps);
1937 } else if (status == GOT_STATUS_MODE_CHANGE) {
1938 err = got_fileindex_entry_update(ie, ondisk_path,
1939 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1940 } else if (status == GOT_STATUS_DELETE) {
1941 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1942 if (err)
1943 goto done;
1944 err = got_fileindex_entry_update(ie, ondisk_path,
1945 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1946 if (err)
1947 goto done;
1948 } else {
1949 int is_bad_symlink = 0;
1950 if (S_ISLNK(te->mode)) {
1951 err = install_symlink(&is_bad_symlink, worktree,
1952 ondisk_path, path, blob,
1953 status == GOT_STATUS_MISSING, 0,
1954 status == GOT_STATUS_UNVERSIONED, repo,
1955 progress_cb, progress_arg);
1956 } else {
1957 err = install_blob(worktree, ondisk_path, path,
1958 te->mode, sb.st_mode, blob,
1959 status == GOT_STATUS_MISSING, 0, 0,
1960 status == GOT_STATUS_UNVERSIONED, repo,
1961 progress_cb, progress_arg);
1963 if (err)
1964 goto done;
1966 if (ie) {
1967 err = got_fileindex_entry_update(ie, ondisk_path,
1968 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1969 } else {
1970 err = create_fileindex_entry(&ie, fileindex,
1971 worktree->base_commit_id, ondisk_path, path,
1972 &blob->id);
1974 if (err)
1975 goto done;
1977 if (is_bad_symlink) {
1978 got_fileindex_entry_filetype_set(ie,
1979 GOT_FILEIDX_MODE_BAD_SYMLINK);
1982 got_object_blob_close(blob);
1983 done:
1984 free(ondisk_path);
1985 return err;
1988 static const struct got_error *
1989 remove_ondisk_file(const char *root_path, const char *path)
1991 const struct got_error *err = NULL;
1992 char *ondisk_path = NULL;
1994 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1995 return got_error_from_errno("asprintf");
1997 if (unlink(ondisk_path) == -1) {
1998 if (errno != ENOENT)
1999 err = got_error_from_errno2("unlink", ondisk_path);
2000 } else {
2001 char *parent = dirname(ondisk_path);
2002 while (parent && strcmp(parent, root_path) != 0) {
2003 if (rmdir(parent) == -1) {
2004 if (errno != ENOTEMPTY)
2005 err = got_error_from_errno2("rmdir",
2006 parent);
2007 break;
2009 parent = dirname(parent);
2012 free(ondisk_path);
2013 return err;
2016 static const struct got_error *
2017 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2018 struct got_fileindex_entry *ie, struct got_repository *repo,
2019 got_worktree_checkout_cb progress_cb, void *progress_arg)
2021 const struct got_error *err = NULL;
2022 unsigned char status;
2023 struct stat sb;
2024 char *ondisk_path;
2026 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2027 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2029 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2030 == -1)
2031 return got_error_from_errno("asprintf");
2033 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2034 if (err)
2035 goto done;
2037 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2038 char ondisk_target[PATH_MAX];
2039 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2040 sizeof(ondisk_target));
2041 if (ondisk_len == -1) {
2042 err = got_error_from_errno2("readlink", ondisk_path);
2043 goto done;
2045 ondisk_target[ondisk_len] = '\0';
2046 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2047 NULL, NULL, /* XXX pass common ancestor info? */
2048 ondisk_target, ondisk_path);
2049 if (err)
2050 goto done;
2051 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2052 ie->path);
2053 goto done;
2056 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2057 status == GOT_STATUS_ADD) {
2058 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2059 if (err)
2060 goto done;
2062 * Preserve the working file and change the deleted blob's
2063 * entry into a schedule-add entry.
2065 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2066 0);
2067 } else {
2068 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2069 if (err)
2070 goto done;
2071 if (status == GOT_STATUS_NO_CHANGE) {
2072 err = remove_ondisk_file(worktree->root_path, ie->path);
2073 if (err)
2074 goto done;
2076 got_fileindex_entry_remove(fileindex, ie);
2078 done:
2079 free(ondisk_path);
2080 return err;
2083 struct diff_cb_arg {
2084 struct got_fileindex *fileindex;
2085 struct got_worktree *worktree;
2086 struct got_repository *repo;
2087 got_worktree_checkout_cb progress_cb;
2088 void *progress_arg;
2089 got_cancel_cb cancel_cb;
2090 void *cancel_arg;
2093 static const struct got_error *
2094 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2095 struct got_tree_entry *te, const char *parent_path)
2097 struct diff_cb_arg *a = arg;
2099 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2100 return got_error(GOT_ERR_CANCELLED);
2102 return update_blob(a->worktree, a->fileindex, ie, te,
2103 ie->path, a->repo, a->progress_cb, a->progress_arg);
2106 static const struct got_error *
2107 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2109 struct diff_cb_arg *a = arg;
2111 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2112 return got_error(GOT_ERR_CANCELLED);
2114 return delete_blob(a->worktree, a->fileindex, ie,
2115 a->repo, a->progress_cb, a->progress_arg);
2118 static const struct got_error *
2119 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2121 struct diff_cb_arg *a = arg;
2122 const struct got_error *err;
2123 char *path;
2125 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2126 return got_error(GOT_ERR_CANCELLED);
2128 if (got_object_tree_entry_is_submodule(te))
2129 return NULL;
2131 if (asprintf(&path, "%s%s%s", parent_path,
2132 parent_path[0] ? "/" : "", te->name)
2133 == -1)
2134 return got_error_from_errno("asprintf");
2136 if (S_ISDIR(te->mode))
2137 err = add_dir_on_disk(a->worktree, path);
2138 else
2139 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2140 a->repo, a->progress_cb, a->progress_arg);
2142 free(path);
2143 return err;
2146 const struct got_error *
2147 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2149 uint32_t uuid_status;
2151 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2152 if (uuid_status != uuid_s_ok) {
2153 *uuidstr = NULL;
2154 return got_error_uuid(uuid_status, "uuid_to_string");
2157 return NULL;
2160 static const struct got_error *
2161 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2163 const struct got_error *err = NULL;
2164 char *uuidstr = NULL;
2166 *refname = NULL;
2168 err = got_worktree_get_uuid(&uuidstr, worktree);
2169 if (err)
2170 return err;
2172 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2173 err = got_error_from_errno("asprintf");
2174 *refname = NULL;
2176 free(uuidstr);
2177 return err;
2180 const struct got_error *
2181 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2183 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2186 static const struct got_error *
2187 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2189 return get_ref_name(refname, worktree,
2190 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2193 static const struct got_error *
2194 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2196 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2199 static const struct got_error *
2200 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2202 return get_ref_name(refname, worktree,
2203 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2206 static const struct got_error *
2207 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2209 return get_ref_name(refname, worktree,
2210 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2213 static const struct got_error *
2214 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2216 return get_ref_name(refname, worktree,
2217 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2220 static const struct got_error *
2221 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2227 static const struct got_error *
2228 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2234 static const struct got_error *
2235 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2237 return get_ref_name(refname, worktree,
2238 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2241 const struct got_error *
2242 got_worktree_get_histedit_script_path(char **path,
2243 struct got_worktree *worktree)
2245 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2246 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2247 *path = NULL;
2248 return got_error_from_errno("asprintf");
2250 return NULL;
2254 * Prevent Git's garbage collector from deleting our base commit by
2255 * setting a reference to our base commit's ID.
2257 static const struct got_error *
2258 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2260 const struct got_error *err = NULL;
2261 struct got_reference *ref = NULL;
2262 char *refname;
2264 err = got_worktree_get_base_ref_name(&refname, worktree);
2265 if (err)
2266 return err;
2268 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2269 if (err)
2270 goto done;
2272 err = got_ref_write(ref, repo);
2273 done:
2274 free(refname);
2275 if (ref)
2276 got_ref_close(ref);
2277 return err;
2280 static const struct got_error *
2281 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2283 const struct got_error *err = NULL;
2285 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2286 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2287 err = got_error_from_errno("asprintf");
2288 *fileindex_path = NULL;
2290 return err;
2294 static const struct got_error *
2295 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2296 struct got_worktree *worktree)
2298 const struct got_error *err = NULL;
2299 FILE *index = NULL;
2301 *fileindex_path = NULL;
2302 *fileindex = got_fileindex_alloc();
2303 if (*fileindex == NULL)
2304 return got_error_from_errno("got_fileindex_alloc");
2306 err = get_fileindex_path(fileindex_path, worktree);
2307 if (err)
2308 goto done;
2310 index = fopen(*fileindex_path, "rb");
2311 if (index == NULL) {
2312 if (errno != ENOENT)
2313 err = got_error_from_errno2("fopen", *fileindex_path);
2314 } else {
2315 err = got_fileindex_read(*fileindex, index);
2316 if (fclose(index) != 0 && err == NULL)
2317 err = got_error_from_errno("fclose");
2319 done:
2320 if (err) {
2321 free(*fileindex_path);
2322 *fileindex_path = NULL;
2323 got_fileindex_free(*fileindex);
2324 *fileindex = NULL;
2326 return err;
2329 struct bump_base_commit_id_arg {
2330 struct got_object_id *base_commit_id;
2331 const char *path;
2332 size_t path_len;
2333 const char *entry_name;
2334 got_worktree_checkout_cb progress_cb;
2335 void *progress_arg;
2338 /* Bump base commit ID of all files within an updated part of the work tree. */
2339 static const struct got_error *
2340 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2342 const struct got_error *err;
2343 struct bump_base_commit_id_arg *a = arg;
2345 if (a->entry_name) {
2346 if (strcmp(ie->path, a->path) != 0)
2347 return NULL;
2348 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2349 return NULL;
2351 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2352 SHA1_DIGEST_LENGTH) == 0)
2353 return NULL;
2355 if (a->progress_cb) {
2356 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2357 ie->path);
2358 if (err)
2359 return err;
2361 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2362 return NULL;
2365 static const struct got_error *
2366 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2368 const struct got_error *err = NULL;
2369 char *new_fileindex_path = NULL;
2370 FILE *new_index = NULL;
2371 struct timespec timeout;
2373 err = got_opentemp_named(&new_fileindex_path, &new_index,
2374 fileindex_path);
2375 if (err)
2376 goto done;
2378 err = got_fileindex_write(fileindex, new_index);
2379 if (err)
2380 goto done;
2382 if (rename(new_fileindex_path, fileindex_path) != 0) {
2383 err = got_error_from_errno3("rename", new_fileindex_path,
2384 fileindex_path);
2385 unlink(new_fileindex_path);
2389 * Sleep for a short amount of time to ensure that files modified after
2390 * this program exits have a different time stamp from the one which
2391 * was recorded in the file index.
2393 timeout.tv_sec = 0;
2394 timeout.tv_nsec = 1;
2395 nanosleep(&timeout, NULL);
2396 done:
2397 if (new_index)
2398 fclose(new_index);
2399 free(new_fileindex_path);
2400 return err;
2403 static const struct got_error *
2404 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2405 struct got_object_id **tree_id, const char *wt_relpath,
2406 struct got_worktree *worktree, struct got_repository *repo)
2408 const struct got_error *err = NULL;
2409 struct got_object_id *id = NULL;
2410 char *in_repo_path = NULL;
2411 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2413 *entry_type = GOT_OBJ_TYPE_ANY;
2414 *tree_relpath = NULL;
2415 *tree_id = NULL;
2417 if (wt_relpath[0] == '\0') {
2418 /* Check out all files within the work tree. */
2419 *entry_type = GOT_OBJ_TYPE_TREE;
2420 *tree_relpath = strdup("");
2421 if (*tree_relpath == NULL) {
2422 err = got_error_from_errno("strdup");
2423 goto done;
2425 err = got_object_id_by_path(tree_id, repo,
2426 worktree->base_commit_id, worktree->path_prefix);
2427 if (err)
2428 goto done;
2429 return NULL;
2432 /* Check out a subset of files in the work tree. */
2434 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2435 is_root_wt ? "" : "/", wt_relpath) == -1) {
2436 err = got_error_from_errno("asprintf");
2437 goto done;
2440 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2441 in_repo_path);
2442 if (err)
2443 goto done;
2445 free(in_repo_path);
2446 in_repo_path = NULL;
2448 err = got_object_get_type(entry_type, repo, id);
2449 if (err)
2450 goto done;
2452 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2453 /* Check out a single file. */
2454 if (strchr(wt_relpath, '/') == NULL) {
2455 /* Check out a single file in work tree's root dir. */
2456 in_repo_path = strdup(worktree->path_prefix);
2457 if (in_repo_path == NULL) {
2458 err = got_error_from_errno("strdup");
2459 goto done;
2461 *tree_relpath = strdup("");
2462 if (*tree_relpath == NULL) {
2463 err = got_error_from_errno("strdup");
2464 goto done;
2466 } else {
2467 /* Check out a single file in a subdirectory. */
2468 err = got_path_dirname(tree_relpath, wt_relpath);
2469 if (err)
2470 return err;
2471 if (asprintf(&in_repo_path, "%s%s%s",
2472 worktree->path_prefix, is_root_wt ? "" : "/",
2473 *tree_relpath) == -1) {
2474 err = got_error_from_errno("asprintf");
2475 goto done;
2478 err = got_object_id_by_path(tree_id, repo,
2479 worktree->base_commit_id, in_repo_path);
2480 } else {
2481 /* Check out all files within a subdirectory. */
2482 *tree_id = got_object_id_dup(id);
2483 if (*tree_id == NULL) {
2484 err = got_error_from_errno("got_object_id_dup");
2485 goto done;
2487 *tree_relpath = strdup(wt_relpath);
2488 if (*tree_relpath == NULL) {
2489 err = got_error_from_errno("strdup");
2490 goto done;
2493 done:
2494 free(id);
2495 free(in_repo_path);
2496 if (err) {
2497 *entry_type = GOT_OBJ_TYPE_ANY;
2498 free(*tree_relpath);
2499 *tree_relpath = NULL;
2500 free(*tree_id);
2501 *tree_id = NULL;
2503 return err;
2506 static const struct got_error *
2507 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2508 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2509 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2510 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2512 const struct got_error *err = NULL;
2513 struct got_commit_object *commit = NULL;
2514 struct got_tree_object *tree = NULL;
2515 struct got_fileindex_diff_tree_cb diff_cb;
2516 struct diff_cb_arg arg;
2518 err = ref_base_commit(worktree, repo);
2519 if (err) {
2520 if (!(err->code == GOT_ERR_ERRNO &&
2521 (errno == EACCES || errno == EROFS)))
2522 goto done;
2523 err = (*progress_cb)(progress_arg,
2524 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2525 if (err)
2526 return err;
2529 err = got_object_open_as_commit(&commit, repo,
2530 worktree->base_commit_id);
2531 if (err)
2532 goto done;
2534 err = got_object_open_as_tree(&tree, repo, tree_id);
2535 if (err)
2536 goto done;
2538 if (entry_name &&
2539 got_object_tree_find_entry(tree, entry_name) == NULL) {
2540 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2541 goto done;
2544 diff_cb.diff_old_new = diff_old_new;
2545 diff_cb.diff_old = diff_old;
2546 diff_cb.diff_new = diff_new;
2547 arg.fileindex = fileindex;
2548 arg.worktree = worktree;
2549 arg.repo = repo;
2550 arg.progress_cb = progress_cb;
2551 arg.progress_arg = progress_arg;
2552 arg.cancel_cb = cancel_cb;
2553 arg.cancel_arg = cancel_arg;
2554 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2555 entry_name, repo, &diff_cb, &arg);
2556 done:
2557 if (tree)
2558 got_object_tree_close(tree);
2559 if (commit)
2560 got_object_commit_close(commit);
2561 return err;
2564 const struct got_error *
2565 got_worktree_checkout_files(struct got_worktree *worktree,
2566 struct got_pathlist_head *paths, struct got_repository *repo,
2567 got_worktree_checkout_cb progress_cb, void *progress_arg,
2568 got_cancel_cb cancel_cb, void *cancel_arg)
2570 const struct got_error *err = NULL, *sync_err, *unlockerr;
2571 struct got_commit_object *commit = NULL;
2572 struct got_tree_object *tree = NULL;
2573 struct got_fileindex *fileindex = NULL;
2574 char *fileindex_path = NULL;
2575 struct got_pathlist_entry *pe;
2576 struct tree_path_data {
2577 SIMPLEQ_ENTRY(tree_path_data) entry;
2578 struct got_object_id *tree_id;
2579 int entry_type;
2580 char *relpath;
2581 char *entry_name;
2582 } *tpd = NULL;
2583 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2585 SIMPLEQ_INIT(&tree_paths);
2587 err = lock_worktree(worktree, LOCK_EX);
2588 if (err)
2589 return err;
2591 /* Map all specified paths to in-repository trees. */
2592 TAILQ_FOREACH(pe, paths, entry) {
2593 tpd = malloc(sizeof(*tpd));
2594 if (tpd == NULL) {
2595 err = got_error_from_errno("malloc");
2596 goto done;
2599 err = find_tree_entry_for_checkout(&tpd->entry_type,
2600 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2601 if (err) {
2602 free(tpd);
2603 goto done;
2606 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2607 err = got_path_basename(&tpd->entry_name, pe->path);
2608 if (err) {
2609 free(tpd->relpath);
2610 free(tpd->tree_id);
2611 free(tpd);
2612 goto done;
2614 } else
2615 tpd->entry_name = NULL;
2617 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2621 * Read the file index.
2622 * Checking out files is supposed to be an idempotent operation.
2623 * If the on-disk file index is incomplete we will try to complete it.
2625 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2626 if (err)
2627 goto done;
2629 tpd = SIMPLEQ_FIRST(&tree_paths);
2630 TAILQ_FOREACH(pe, paths, entry) {
2631 struct bump_base_commit_id_arg bbc_arg;
2633 err = checkout_files(worktree, fileindex, tpd->relpath,
2634 tpd->tree_id, tpd->entry_name, repo,
2635 progress_cb, progress_arg, cancel_cb, cancel_arg);
2636 if (err)
2637 break;
2639 bbc_arg.base_commit_id = worktree->base_commit_id;
2640 bbc_arg.entry_name = tpd->entry_name;
2641 bbc_arg.path = pe->path;
2642 bbc_arg.path_len = pe->path_len;
2643 bbc_arg.progress_cb = progress_cb;
2644 bbc_arg.progress_arg = progress_arg;
2645 err = got_fileindex_for_each_entry_safe(fileindex,
2646 bump_base_commit_id, &bbc_arg);
2647 if (err)
2648 break;
2650 tpd = SIMPLEQ_NEXT(tpd, entry);
2652 sync_err = sync_fileindex(fileindex, fileindex_path);
2653 if (sync_err && err == NULL)
2654 err = sync_err;
2655 done:
2656 free(fileindex_path);
2657 if (tree)
2658 got_object_tree_close(tree);
2659 if (commit)
2660 got_object_commit_close(commit);
2661 if (fileindex)
2662 got_fileindex_free(fileindex);
2663 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2664 tpd = SIMPLEQ_FIRST(&tree_paths);
2665 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2666 free(tpd->relpath);
2667 free(tpd->tree_id);
2668 free(tpd);
2670 unlockerr = lock_worktree(worktree, LOCK_SH);
2671 if (unlockerr && err == NULL)
2672 err = unlockerr;
2673 return err;
2676 struct merge_file_cb_arg {
2677 struct got_worktree *worktree;
2678 struct got_fileindex *fileindex;
2679 got_worktree_checkout_cb progress_cb;
2680 void *progress_arg;
2681 got_cancel_cb cancel_cb;
2682 void *cancel_arg;
2683 const char *label_orig;
2684 struct got_object_id *commit_id2;
2687 static const struct got_error *
2688 merge_file_cb(void *arg, struct got_blob_object *blob1,
2689 struct got_blob_object *blob2, struct got_object_id *id1,
2690 struct got_object_id *id2, const char *path1, const char *path2,
2691 mode_t mode1, mode_t mode2, struct got_repository *repo)
2693 static const struct got_error *err = NULL;
2694 struct merge_file_cb_arg *a = arg;
2695 struct got_fileindex_entry *ie;
2696 char *ondisk_path = NULL;
2697 struct stat sb;
2698 unsigned char status;
2699 int local_changes_subsumed;
2701 if (blob1 && blob2) {
2702 ie = got_fileindex_entry_get(a->fileindex, path2,
2703 strlen(path2));
2704 if (ie == NULL)
2705 return (*a->progress_cb)(a->progress_arg,
2706 GOT_STATUS_MISSING, path2);
2708 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2709 path2) == -1)
2710 return got_error_from_errno("asprintf");
2712 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2713 repo);
2714 if (err)
2715 goto done;
2717 if (status == GOT_STATUS_DELETE) {
2718 err = (*a->progress_cb)(a->progress_arg,
2719 GOT_STATUS_MERGE, path2);
2720 goto done;
2722 if (status != GOT_STATUS_NO_CHANGE &&
2723 status != GOT_STATUS_MODIFY &&
2724 status != GOT_STATUS_CONFLICT &&
2725 status != GOT_STATUS_ADD) {
2726 err = (*a->progress_cb)(a->progress_arg, status, path2);
2727 goto done;
2730 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2731 char *link_target2;
2732 err = got_object_blob_read_to_str(&link_target2, blob2);
2733 if (err)
2734 goto done;
2735 err = merge_symlink(a->worktree, blob1, ondisk_path,
2736 path2, a->label_orig, link_target2, a->commit_id2,
2737 repo, a->progress_cb, a->progress_arg);
2738 free(link_target2);
2739 } else {
2740 err = merge_blob(&local_changes_subsumed, a->worktree,
2741 blob1, ondisk_path, path2, sb.st_mode,
2742 a->label_orig, blob2, a->commit_id2, repo,
2743 a->progress_cb, a->progress_arg);
2745 } else if (blob1) {
2746 ie = got_fileindex_entry_get(a->fileindex, path1,
2747 strlen(path1));
2748 if (ie == NULL)
2749 return (*a->progress_cb)(a->progress_arg,
2750 GOT_STATUS_MISSING, path1);
2752 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2753 path1) == -1)
2754 return got_error_from_errno("asprintf");
2756 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2757 repo);
2758 if (err)
2759 goto done;
2761 switch (status) {
2762 case GOT_STATUS_NO_CHANGE:
2763 err = (*a->progress_cb)(a->progress_arg,
2764 GOT_STATUS_DELETE, path1);
2765 if (err)
2766 goto done;
2767 err = remove_ondisk_file(a->worktree->root_path, 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_DELETE:
2774 case GOT_STATUS_MISSING:
2775 err = (*a->progress_cb)(a->progress_arg,
2776 GOT_STATUS_DELETE, path1);
2777 if (err)
2778 goto done;
2779 if (ie)
2780 got_fileindex_entry_mark_deleted_from_disk(ie);
2781 break;
2782 case GOT_STATUS_ADD:
2783 case GOT_STATUS_MODIFY:
2784 case GOT_STATUS_CONFLICT:
2785 err = (*a->progress_cb)(a->progress_arg,
2786 GOT_STATUS_CANNOT_DELETE, path1);
2787 if (err)
2788 goto done;
2789 break;
2790 case GOT_STATUS_OBSTRUCTED:
2791 err = (*a->progress_cb)(a->progress_arg, status, path1);
2792 if (err)
2793 goto done;
2794 break;
2795 default:
2796 break;
2798 } else if (blob2) {
2799 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2800 path2) == -1)
2801 return got_error_from_errno("asprintf");
2802 ie = got_fileindex_entry_get(a->fileindex, path2,
2803 strlen(path2));
2804 if (ie) {
2805 err = get_file_status(&status, &sb, ie, ondisk_path,
2806 -1, NULL, repo);
2807 if (err)
2808 goto done;
2809 if (status != GOT_STATUS_NO_CHANGE &&
2810 status != GOT_STATUS_MODIFY &&
2811 status != GOT_STATUS_CONFLICT &&
2812 status != GOT_STATUS_ADD) {
2813 err = (*a->progress_cb)(a->progress_arg,
2814 status, path2);
2815 goto done;
2817 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2818 char *link_target2;
2819 err = got_object_blob_read_to_str(&link_target2,
2820 blob2);
2821 if (err)
2822 goto done;
2823 err = merge_symlink(a->worktree, NULL,
2824 ondisk_path, path2, a->label_orig,
2825 link_target2, a->commit_id2, repo,
2826 a->progress_cb, a->progress_arg);
2827 free(link_target2);
2828 } else if (S_ISREG(sb.st_mode)) {
2829 err = merge_blob(&local_changes_subsumed,
2830 a->worktree, NULL, ondisk_path, path2,
2831 sb.st_mode, a->label_orig, blob2,
2832 a->commit_id2, repo, a->progress_cb,
2833 a->progress_arg);
2834 } else {
2835 err = got_error_path(ondisk_path,
2836 GOT_ERR_FILE_OBSTRUCTED);
2838 if (err)
2839 goto done;
2840 if (status == GOT_STATUS_DELETE) {
2841 err = got_fileindex_entry_update(ie,
2842 ondisk_path, blob2->id.sha1,
2843 a->worktree->base_commit_id->sha1, 0);
2844 if (err)
2845 goto done;
2847 } else {
2848 int is_bad_symlink = 0;
2849 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2850 if (S_ISLNK(mode2)) {
2851 err = install_symlink(&is_bad_symlink,
2852 a->worktree, ondisk_path, path2, blob2, 0,
2853 0, 1, repo, a->progress_cb, a->progress_arg);
2854 } else {
2855 err = install_blob(a->worktree, ondisk_path, path2,
2856 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2857 a->progress_cb, a->progress_arg);
2859 if (err)
2860 goto done;
2861 err = got_fileindex_entry_alloc(&ie, path2);
2862 if (err)
2863 goto done;
2864 err = got_fileindex_entry_update(ie, ondisk_path,
2865 NULL, NULL, 1);
2866 if (err) {
2867 got_fileindex_entry_free(ie);
2868 goto done;
2870 err = got_fileindex_entry_add(a->fileindex, ie);
2871 if (err) {
2872 got_fileindex_entry_free(ie);
2873 goto done;
2875 if (is_bad_symlink) {
2876 got_fileindex_entry_filetype_set(ie,
2877 GOT_FILEIDX_MODE_BAD_SYMLINK);
2881 done:
2882 free(ondisk_path);
2883 return err;
2886 struct check_merge_ok_arg {
2887 struct got_worktree *worktree;
2888 struct got_repository *repo;
2891 static const struct got_error *
2892 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2894 const struct got_error *err = NULL;
2895 struct check_merge_ok_arg *a = arg;
2896 unsigned char status;
2897 struct stat sb;
2898 char *ondisk_path;
2900 /* Reject merges into a work tree with mixed base commits. */
2901 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2902 SHA1_DIGEST_LENGTH))
2903 return got_error(GOT_ERR_MIXED_COMMITS);
2905 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2906 == -1)
2907 return got_error_from_errno("asprintf");
2909 /* Reject merges into a work tree with conflicted files. */
2910 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2911 if (err)
2912 return err;
2913 if (status == GOT_STATUS_CONFLICT)
2914 return got_error(GOT_ERR_CONFLICTS);
2916 return NULL;
2919 static const struct got_error *
2920 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2921 const char *fileindex_path, struct got_object_id *commit_id1,
2922 struct got_object_id *commit_id2, struct got_repository *repo,
2923 got_worktree_checkout_cb progress_cb, void *progress_arg,
2924 got_cancel_cb cancel_cb, void *cancel_arg)
2926 const struct got_error *err = NULL, *sync_err;
2927 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2928 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2929 struct merge_file_cb_arg arg;
2930 char *label_orig = NULL;
2932 if (commit_id1) {
2933 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2934 worktree->path_prefix);
2935 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2936 goto done;
2938 if (tree_id1) {
2939 char *id_str;
2941 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2942 if (err)
2943 goto done;
2945 err = got_object_id_str(&id_str, commit_id1);
2946 if (err)
2947 goto done;
2949 if (asprintf(&label_orig, "%s: commit %s",
2950 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2951 err = got_error_from_errno("asprintf");
2952 free(id_str);
2953 goto done;
2955 free(id_str);
2958 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2959 worktree->path_prefix);
2960 if (err)
2961 goto done;
2963 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2964 if (err)
2965 goto done;
2967 arg.worktree = worktree;
2968 arg.fileindex = fileindex;
2969 arg.progress_cb = progress_cb;
2970 arg.progress_arg = progress_arg;
2971 arg.cancel_cb = cancel_cb;
2972 arg.cancel_arg = cancel_arg;
2973 arg.label_orig = label_orig;
2974 arg.commit_id2 = commit_id2;
2975 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2976 sync_err = sync_fileindex(fileindex, fileindex_path);
2977 if (sync_err && err == NULL)
2978 err = sync_err;
2979 done:
2980 if (tree1)
2981 got_object_tree_close(tree1);
2982 if (tree2)
2983 got_object_tree_close(tree2);
2984 free(label_orig);
2985 return err;
2988 const struct got_error *
2989 got_worktree_merge_files(struct got_worktree *worktree,
2990 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2991 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2992 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2994 const struct got_error *err, *unlockerr;
2995 char *fileindex_path = NULL;
2996 struct got_fileindex *fileindex = NULL;
2997 struct check_merge_ok_arg mok_arg;
2999 err = lock_worktree(worktree, LOCK_EX);
3000 if (err)
3001 return err;
3003 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3004 if (err)
3005 goto done;
3007 mok_arg.worktree = worktree;
3008 mok_arg.repo = repo;
3009 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3010 &mok_arg);
3011 if (err)
3012 goto done;
3014 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3015 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3016 done:
3017 if (fileindex)
3018 got_fileindex_free(fileindex);
3019 free(fileindex_path);
3020 unlockerr = lock_worktree(worktree, LOCK_SH);
3021 if (unlockerr && err == NULL)
3022 err = unlockerr;
3023 return err;
3026 struct diff_dir_cb_arg {
3027 struct got_fileindex *fileindex;
3028 struct got_worktree *worktree;
3029 const char *status_path;
3030 size_t status_path_len;
3031 struct got_repository *repo;
3032 got_worktree_status_cb status_cb;
3033 void *status_arg;
3034 got_cancel_cb cancel_cb;
3035 void *cancel_arg;
3036 /* A pathlist containing per-directory pathlists of ignore patterns. */
3037 struct got_pathlist_head ignores;
3038 int report_unchanged;
3039 int no_ignores;
3042 static const struct got_error *
3043 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3044 int dirfd, const char *de_name,
3045 got_worktree_status_cb status_cb, void *status_arg,
3046 struct got_repository *repo, int report_unchanged)
3048 const struct got_error *err = NULL;
3049 unsigned char status = GOT_STATUS_NO_CHANGE;
3050 unsigned char staged_status = get_staged_status(ie);
3051 struct stat sb;
3052 struct got_object_id blob_id, commit_id, staged_blob_id;
3053 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3054 struct got_object_id *staged_blob_idp = NULL;
3056 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3057 if (err)
3058 return err;
3060 if (status == GOT_STATUS_NO_CHANGE &&
3061 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3062 return NULL;
3064 if (got_fileindex_entry_has_blob(ie)) {
3065 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3066 blob_idp = &blob_id;
3068 if (got_fileindex_entry_has_commit(ie)) {
3069 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3070 commit_idp = &commit_id;
3072 if (staged_status == GOT_STATUS_ADD ||
3073 staged_status == GOT_STATUS_MODIFY) {
3074 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3075 SHA1_DIGEST_LENGTH);
3076 staged_blob_idp = &staged_blob_id;
3079 return (*status_cb)(status_arg, status, staged_status,
3080 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3083 static const struct got_error *
3084 status_old_new(void *arg, struct got_fileindex_entry *ie,
3085 struct dirent *de, const char *parent_path, int dirfd)
3087 const struct got_error *err = NULL;
3088 struct diff_dir_cb_arg *a = arg;
3089 char *abspath;
3091 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3092 return got_error(GOT_ERR_CANCELLED);
3094 if (got_path_cmp(parent_path, a->status_path,
3095 strlen(parent_path), a->status_path_len) != 0 &&
3096 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3097 return NULL;
3099 if (parent_path[0]) {
3100 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3101 parent_path, de->d_name) == -1)
3102 return got_error_from_errno("asprintf");
3103 } else {
3104 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3105 de->d_name) == -1)
3106 return got_error_from_errno("asprintf");
3109 err = report_file_status(ie, abspath, dirfd, de->d_name,
3110 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3111 free(abspath);
3112 return err;
3115 static const struct got_error *
3116 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3118 struct diff_dir_cb_arg *a = arg;
3119 struct got_object_id blob_id, commit_id;
3120 unsigned char status;
3122 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3123 return got_error(GOT_ERR_CANCELLED);
3125 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3126 return NULL;
3128 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3129 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3130 if (got_fileindex_entry_has_file_on_disk(ie))
3131 status = GOT_STATUS_MISSING;
3132 else
3133 status = GOT_STATUS_DELETE;
3134 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3135 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3138 void
3139 free_ignorelist(struct got_pathlist_head *ignorelist)
3141 struct got_pathlist_entry *pe;
3143 TAILQ_FOREACH(pe, ignorelist, entry)
3144 free((char *)pe->path);
3145 got_pathlist_free(ignorelist);
3148 void
3149 free_ignores(struct got_pathlist_head *ignores)
3151 struct got_pathlist_entry *pe;
3153 TAILQ_FOREACH(pe, ignores, entry) {
3154 struct got_pathlist_head *ignorelist = pe->data;
3155 free_ignorelist(ignorelist);
3156 free((char *)pe->path);
3158 got_pathlist_free(ignores);
3161 static const struct got_error *
3162 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3164 const struct got_error *err = NULL;
3165 struct got_pathlist_entry *pe = NULL;
3166 struct got_pathlist_head *ignorelist;
3167 char *line = NULL, *pattern, *dirpath = NULL;
3168 size_t linesize = 0;
3169 ssize_t linelen;
3171 ignorelist = calloc(1, sizeof(*ignorelist));
3172 if (ignorelist == NULL)
3173 return got_error_from_errno("calloc");
3174 TAILQ_INIT(ignorelist);
3176 while ((linelen = getline(&line, &linesize, f)) != -1) {
3177 if (linelen > 0 && line[linelen - 1] == '\n')
3178 line[linelen - 1] = '\0';
3180 /* Git's ignores may contain comments. */
3181 if (line[0] == '#')
3182 continue;
3184 /* Git's negated patterns are not (yet?) supported. */
3185 if (line[0] == '!')
3186 continue;
3188 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3189 line) == -1) {
3190 err = got_error_from_errno("asprintf");
3191 goto done;
3193 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3194 if (err)
3195 goto done;
3197 if (ferror(f)) {
3198 err = got_error_from_errno("getline");
3199 goto done;
3202 dirpath = strdup(path);
3203 if (dirpath == NULL) {
3204 err = got_error_from_errno("strdup");
3205 goto done;
3207 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3208 done:
3209 free(line);
3210 if (err || pe == NULL) {
3211 free(dirpath);
3212 free_ignorelist(ignorelist);
3214 return err;
3217 int
3218 match_ignores(struct got_pathlist_head *ignores, const char *path)
3220 struct got_pathlist_entry *pe;
3222 /* Handle patterns which match in all directories. */
3223 TAILQ_FOREACH(pe, ignores, entry) {
3224 struct got_pathlist_head *ignorelist = pe->data;
3225 struct got_pathlist_entry *pi;
3227 TAILQ_FOREACH(pi, ignorelist, entry) {
3228 const char *p, *pattern = pi->path;
3230 if (strncmp(pattern, "**/", 3) != 0)
3231 continue;
3232 pattern += 3;
3233 p = path;
3234 while (*p) {
3235 if (fnmatch(pattern, p,
3236 FNM_PATHNAME | FNM_LEADING_DIR)) {
3237 /* Retry in next directory. */
3238 while (*p && *p != '/')
3239 p++;
3240 while (*p == '/')
3241 p++;
3242 continue;
3244 return 1;
3250 * The ignores pathlist contains ignore lists from children before
3251 * parents, so we can find the most specific ignorelist by walking
3252 * ignores backwards.
3254 pe = TAILQ_LAST(ignores, got_pathlist_head);
3255 while (pe) {
3256 if (got_path_is_child(path, pe->path, pe->path_len)) {
3257 struct got_pathlist_head *ignorelist = pe->data;
3258 struct got_pathlist_entry *pi;
3259 TAILQ_FOREACH(pi, ignorelist, entry) {
3260 const char *pattern = pi->path;
3261 int flags = FNM_LEADING_DIR;
3262 if (strstr(pattern, "/**/") == NULL)
3263 flags |= FNM_PATHNAME;
3264 if (fnmatch(pattern, path, flags))
3265 continue;
3266 return 1;
3269 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3272 return 0;
3275 static const struct got_error *
3276 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3277 const char *path, int dirfd, const char *ignores_filename)
3279 const struct got_error *err = NULL;
3280 char *ignorespath;
3281 int fd = -1;
3282 FILE *ignoresfile = NULL;
3284 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3285 path[0] ? "/" : "", ignores_filename) == -1)
3286 return got_error_from_errno("asprintf");
3288 if (dirfd != -1) {
3289 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3290 if (fd == -1) {
3291 if (errno != ENOENT && errno != EACCES)
3292 err = got_error_from_errno2("openat",
3293 ignorespath);
3294 } else {
3295 ignoresfile = fdopen(fd, "r");
3296 if (ignoresfile == NULL)
3297 err = got_error_from_errno2("fdopen",
3298 ignorespath);
3299 else {
3300 fd = -1;
3301 err = read_ignores(ignores, path, ignoresfile);
3304 } else {
3305 ignoresfile = fopen(ignorespath, "r");
3306 if (ignoresfile == NULL) {
3307 if (errno != ENOENT && errno != EACCES)
3308 err = got_error_from_errno2("fopen",
3309 ignorespath);
3310 } else
3311 err = read_ignores(ignores, path, ignoresfile);
3314 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3315 err = got_error_from_errno2("fclose", path);
3316 if (fd != -1 && close(fd) == -1 && err == NULL)
3317 err = got_error_from_errno2("close", path);
3318 free(ignorespath);
3319 return err;
3322 static const struct got_error *
3323 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3325 const struct got_error *err = NULL;
3326 struct diff_dir_cb_arg *a = arg;
3327 char *path = NULL;
3329 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3330 return got_error(GOT_ERR_CANCELLED);
3332 if (parent_path[0]) {
3333 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3334 return got_error_from_errno("asprintf");
3335 } else {
3336 path = de->d_name;
3339 if (de->d_type != DT_DIR &&
3340 got_path_is_child(path, a->status_path, a->status_path_len)
3341 && !match_ignores(&a->ignores, path))
3342 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3343 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3344 if (parent_path[0])
3345 free(path);
3346 return err;
3349 static const struct got_error *
3350 status_traverse(void *arg, const char *path, int dirfd)
3352 const struct got_error *err = NULL;
3353 struct diff_dir_cb_arg *a = arg;
3355 if (a->no_ignores)
3356 return NULL;
3358 err = add_ignores(&a->ignores, a->worktree->root_path,
3359 path, dirfd, ".cvsignore");
3360 if (err)
3361 return err;
3363 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3364 dirfd, ".gitignore");
3366 return err;
3369 static const struct got_error *
3370 report_single_file_status(const char *path, const char *ondisk_path,
3371 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3372 void *status_arg, struct got_repository *repo, int report_unchanged)
3374 struct got_fileindex_entry *ie;
3375 struct stat sb;
3377 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3378 if (ie)
3379 return report_file_status(ie, ondisk_path, -1, NULL,
3380 status_cb, status_arg, repo, report_unchanged);
3382 if (lstat(ondisk_path, &sb) == -1) {
3383 if (errno != ENOENT)
3384 return got_error_from_errno2("lstat", ondisk_path);
3385 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3386 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3387 return NULL;
3390 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3391 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3392 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3394 return NULL;
3397 static const struct got_error *
3398 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3399 const char *root_path, const char *path)
3401 const struct got_error *err;
3402 char *parent_path, *next_parent_path = NULL;
3404 err = add_ignores(ignores, root_path, "", -1,
3405 ".cvsignore");
3406 if (err)
3407 return err;
3409 err = add_ignores(ignores, root_path, "", -1,
3410 ".gitignore");
3411 if (err)
3412 return err;
3414 err = got_path_dirname(&parent_path, path);
3415 if (err) {
3416 if (err->code == GOT_ERR_BAD_PATH)
3417 return NULL; /* cannot traverse parent */
3418 return err;
3420 for (;;) {
3421 err = add_ignores(ignores, root_path, parent_path, -1,
3422 ".cvsignore");
3423 if (err)
3424 break;
3425 err = add_ignores(ignores, root_path, parent_path, -1,
3426 ".gitignore");
3427 if (err)
3428 break;
3429 err = got_path_dirname(&next_parent_path, parent_path);
3430 if (err) {
3431 if (err->code == GOT_ERR_BAD_PATH)
3432 err = NULL; /* traversed everything */
3433 break;
3435 free(parent_path);
3436 parent_path = next_parent_path;
3437 next_parent_path = NULL;
3440 free(parent_path);
3441 free(next_parent_path);
3442 return err;
3445 static const struct got_error *
3446 worktree_status(struct got_worktree *worktree, const char *path,
3447 struct got_fileindex *fileindex, struct got_repository *repo,
3448 got_worktree_status_cb status_cb, void *status_arg,
3449 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3450 int report_unchanged)
3452 const struct got_error *err = NULL;
3453 int fd = -1;
3454 struct got_fileindex_diff_dir_cb fdiff_cb;
3455 struct diff_dir_cb_arg arg;
3456 char *ondisk_path = NULL;
3458 TAILQ_INIT(&arg.ignores);
3460 if (asprintf(&ondisk_path, "%s%s%s",
3461 worktree->root_path, path[0] ? "/" : "", path) == -1)
3462 return got_error_from_errno("asprintf");
3464 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3465 if (fd == -1) {
3466 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3467 errno != ELOOP)
3468 err = got_error_from_errno2("open", ondisk_path);
3469 else
3470 err = report_single_file_status(path, ondisk_path,
3471 fileindex, status_cb, status_arg, repo,
3472 report_unchanged);
3473 } else {
3474 fdiff_cb.diff_old_new = status_old_new;
3475 fdiff_cb.diff_old = status_old;
3476 fdiff_cb.diff_new = status_new;
3477 fdiff_cb.diff_traverse = status_traverse;
3478 arg.fileindex = fileindex;
3479 arg.worktree = worktree;
3480 arg.status_path = path;
3481 arg.status_path_len = strlen(path);
3482 arg.repo = repo;
3483 arg.status_cb = status_cb;
3484 arg.status_arg = status_arg;
3485 arg.cancel_cb = cancel_cb;
3486 arg.cancel_arg = cancel_arg;
3487 arg.report_unchanged = report_unchanged;
3488 arg.no_ignores = no_ignores;
3489 if (!no_ignores) {
3490 err = add_ignores_from_parent_paths(&arg.ignores,
3491 worktree->root_path, path);
3492 if (err)
3493 goto done;
3495 err = got_fileindex_diff_dir(fileindex, fd,
3496 worktree->root_path, path, repo, &fdiff_cb, &arg);
3498 done:
3499 free_ignores(&arg.ignores);
3500 if (fd != -1 && close(fd) != 0 && err == NULL)
3501 err = got_error_from_errno("close");
3502 free(ondisk_path);
3503 return err;
3506 const struct got_error *
3507 got_worktree_status(struct got_worktree *worktree,
3508 struct got_pathlist_head *paths, struct got_repository *repo,
3509 got_worktree_status_cb status_cb, void *status_arg,
3510 got_cancel_cb cancel_cb, void *cancel_arg)
3512 const struct got_error *err = NULL;
3513 char *fileindex_path = NULL;
3514 struct got_fileindex *fileindex = NULL;
3515 struct got_pathlist_entry *pe;
3517 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3518 if (err)
3519 return err;
3521 TAILQ_FOREACH(pe, paths, entry) {
3522 err = worktree_status(worktree, pe->path, fileindex, repo,
3523 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3524 if (err)
3525 break;
3527 free(fileindex_path);
3528 got_fileindex_free(fileindex);
3529 return err;
3532 const struct got_error *
3533 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3534 const char *arg)
3536 const struct got_error *err = NULL;
3537 char *resolved = NULL, *cwd = NULL, *path = NULL;
3538 size_t len;
3539 struct stat sb;
3541 *wt_path = NULL;
3543 cwd = getcwd(NULL, 0);
3544 if (cwd == NULL)
3545 return got_error_from_errno("getcwd");
3547 if (lstat(arg, &sb) == -1) {
3548 if (errno != ENOENT) {
3549 err = got_error_from_errno2("lstat", arg);
3550 goto done;
3553 if (S_ISLNK(sb.st_mode)) {
3555 * We cannot use realpath(3) with symlinks since we want to
3556 * operate on the symlink itself.
3557 * But we can make the path absolute, assuming it is relative
3558 * to the current working directory, and then canonicalize it.
3560 char *abspath = NULL;
3561 char canonpath[PATH_MAX];
3562 if (!got_path_is_absolute(arg)) {
3563 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3564 err = got_error_from_errno("asprintf");
3565 goto done;
3569 err = got_canonpath(abspath ? abspath : arg, canonpath,
3570 sizeof(canonpath));
3571 if (err)
3572 goto done;
3573 resolved = strdup(canonpath);
3574 if (resolved == NULL) {
3575 err = got_error_from_errno("strdup");
3576 goto done;
3578 } else {
3579 resolved = realpath(arg, NULL);
3580 if (resolved == NULL) {
3581 if (errno != ENOENT) {
3582 err = got_error_from_errno2("realpath", arg);
3583 goto done;
3585 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3586 err = got_error_from_errno("asprintf");
3587 goto done;
3592 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3593 strlen(got_worktree_get_root_path(worktree)))) {
3594 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3595 goto done;
3598 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3599 err = got_path_skip_common_ancestor(&path,
3600 got_worktree_get_root_path(worktree), resolved);
3601 if (err)
3602 goto done;
3603 } else {
3604 path = strdup("");
3605 if (path == NULL) {
3606 err = got_error_from_errno("strdup");
3607 goto done;
3611 /* XXX status walk can't deal with trailing slash! */
3612 len = strlen(path);
3613 while (len > 0 && path[len - 1] == '/') {
3614 path[len - 1] = '\0';
3615 len--;
3617 done:
3618 free(resolved);
3619 free(cwd);
3620 if (err == NULL)
3621 *wt_path = path;
3622 else
3623 free(path);
3624 return err;
3627 struct schedule_addition_args {
3628 struct got_worktree *worktree;
3629 struct got_fileindex *fileindex;
3630 got_worktree_checkout_cb progress_cb;
3631 void *progress_arg;
3632 struct got_repository *repo;
3635 static const struct got_error *
3636 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3637 const char *relpath, struct got_object_id *blob_id,
3638 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3639 int dirfd, const char *de_name)
3641 struct schedule_addition_args *a = arg;
3642 const struct got_error *err = NULL;
3643 struct got_fileindex_entry *ie;
3644 struct stat sb;
3645 char *ondisk_path;
3647 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3648 relpath) == -1)
3649 return got_error_from_errno("asprintf");
3651 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3652 if (ie) {
3653 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3654 de_name, a->repo);
3655 if (err)
3656 goto done;
3657 /* Re-adding an existing entry is a no-op. */
3658 if (status == GOT_STATUS_ADD)
3659 goto done;
3660 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3661 if (err)
3662 goto done;
3665 if (status != GOT_STATUS_UNVERSIONED) {
3666 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3667 goto done;
3670 err = got_fileindex_entry_alloc(&ie, relpath);
3671 if (err)
3672 goto done;
3673 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3674 if (err) {
3675 got_fileindex_entry_free(ie);
3676 goto done;
3678 err = got_fileindex_entry_add(a->fileindex, ie);
3679 if (err) {
3680 got_fileindex_entry_free(ie);
3681 goto done;
3683 done:
3684 free(ondisk_path);
3685 if (err)
3686 return err;
3687 if (status == GOT_STATUS_ADD)
3688 return NULL;
3689 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3692 const struct got_error *
3693 got_worktree_schedule_add(struct got_worktree *worktree,
3694 struct got_pathlist_head *paths,
3695 got_worktree_checkout_cb progress_cb, void *progress_arg,
3696 struct got_repository *repo, int no_ignores)
3698 struct got_fileindex *fileindex = NULL;
3699 char *fileindex_path = NULL;
3700 const struct got_error *err = NULL, *sync_err, *unlockerr;
3701 struct got_pathlist_entry *pe;
3702 struct schedule_addition_args saa;
3704 err = lock_worktree(worktree, LOCK_EX);
3705 if (err)
3706 return err;
3708 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3709 if (err)
3710 goto done;
3712 saa.worktree = worktree;
3713 saa.fileindex = fileindex;
3714 saa.progress_cb = progress_cb;
3715 saa.progress_arg = progress_arg;
3716 saa.repo = repo;
3718 TAILQ_FOREACH(pe, paths, entry) {
3719 err = worktree_status(worktree, pe->path, fileindex, repo,
3720 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3721 if (err)
3722 break;
3724 sync_err = sync_fileindex(fileindex, fileindex_path);
3725 if (sync_err && err == NULL)
3726 err = sync_err;
3727 done:
3728 free(fileindex_path);
3729 if (fileindex)
3730 got_fileindex_free(fileindex);
3731 unlockerr = lock_worktree(worktree, LOCK_SH);
3732 if (unlockerr && err == NULL)
3733 err = unlockerr;
3734 return err;
3737 struct schedule_deletion_args {
3738 struct got_worktree *worktree;
3739 struct got_fileindex *fileindex;
3740 got_worktree_delete_cb progress_cb;
3741 void *progress_arg;
3742 struct got_repository *repo;
3743 int delete_local_mods;
3744 int keep_on_disk;
3747 static const struct got_error *
3748 schedule_for_deletion(void *arg, unsigned char status,
3749 unsigned char staged_status, const char *relpath,
3750 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3751 struct got_object_id *commit_id, int dirfd, const char *de_name)
3753 struct schedule_deletion_args *a = arg;
3754 const struct got_error *err = NULL;
3755 struct got_fileindex_entry *ie = NULL;
3756 struct stat sb;
3757 char *ondisk_path, *parent = NULL;
3759 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3760 if (ie == NULL)
3761 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3763 staged_status = get_staged_status(ie);
3764 if (staged_status != GOT_STATUS_NO_CHANGE) {
3765 if (staged_status == GOT_STATUS_DELETE)
3766 return NULL;
3767 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3770 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3771 relpath) == -1)
3772 return got_error_from_errno("asprintf");
3774 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3775 a->repo);
3776 if (err)
3777 goto done;
3779 if (status != GOT_STATUS_NO_CHANGE) {
3780 if (status == GOT_STATUS_DELETE)
3781 goto done;
3782 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3783 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3784 goto done;
3786 if (status != GOT_STATUS_MODIFY &&
3787 status != GOT_STATUS_MISSING) {
3788 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3789 goto done;
3793 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3794 if (dirfd != -1) {
3795 if (unlinkat(dirfd, de_name, 0) != 0) {
3796 err = got_error_from_errno2("unlinkat",
3797 ondisk_path);
3798 goto done;
3800 } else if (unlink(ondisk_path) != 0) {
3801 err = got_error_from_errno2("unlink", ondisk_path);
3802 goto done;
3805 parent = dirname(ondisk_path);
3807 if (parent == NULL) {
3808 err = got_error_from_errno2("dirname", ondisk_path);
3809 goto done;
3811 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3812 if (rmdir(parent) == -1) {
3813 if (errno != ENOTEMPTY)
3814 err = got_error_from_errno2("rmdir",
3815 parent);
3816 break;
3818 parent = dirname(parent);
3819 if (parent == NULL) {
3820 err = got_error_from_errno2("dirname", parent);
3821 goto done;
3826 got_fileindex_entry_mark_deleted_from_disk(ie);
3827 done:
3828 free(ondisk_path);
3829 if (err)
3830 return err;
3831 if (status == GOT_STATUS_DELETE)
3832 return NULL;
3833 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3834 staged_status, relpath);
3837 const struct got_error *
3838 got_worktree_schedule_delete(struct got_worktree *worktree,
3839 struct got_pathlist_head *paths, int delete_local_mods,
3840 got_worktree_delete_cb progress_cb, void *progress_arg,
3841 struct got_repository *repo, int keep_on_disk)
3843 struct got_fileindex *fileindex = NULL;
3844 char *fileindex_path = NULL;
3845 const struct got_error *err = NULL, *sync_err, *unlockerr;
3846 struct got_pathlist_entry *pe;
3847 struct schedule_deletion_args sda;
3849 err = lock_worktree(worktree, LOCK_EX);
3850 if (err)
3851 return err;
3853 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3854 if (err)
3855 goto done;
3857 sda.worktree = worktree;
3858 sda.fileindex = fileindex;
3859 sda.progress_cb = progress_cb;
3860 sda.progress_arg = progress_arg;
3861 sda.repo = repo;
3862 sda.delete_local_mods = delete_local_mods;
3863 sda.keep_on_disk = keep_on_disk;
3865 TAILQ_FOREACH(pe, paths, entry) {
3866 err = worktree_status(worktree, pe->path, fileindex, repo,
3867 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3868 if (err)
3869 break;
3871 sync_err = sync_fileindex(fileindex, fileindex_path);
3872 if (sync_err && err == NULL)
3873 err = sync_err;
3874 done:
3875 free(fileindex_path);
3876 if (fileindex)
3877 got_fileindex_free(fileindex);
3878 unlockerr = lock_worktree(worktree, LOCK_SH);
3879 if (unlockerr && err == NULL)
3880 err = unlockerr;
3881 return err;
3884 static const struct got_error *
3885 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3887 const struct got_error *err = NULL;
3888 char *line = NULL;
3889 size_t linesize = 0, n;
3890 ssize_t linelen;
3892 linelen = getline(&line, &linesize, infile);
3893 if (linelen == -1) {
3894 if (ferror(infile)) {
3895 err = got_error_from_errno("getline");
3896 goto done;
3898 return NULL;
3900 if (outfile) {
3901 n = fwrite(line, 1, linelen, outfile);
3902 if (n != linelen) {
3903 err = got_ferror(outfile, GOT_ERR_IO);
3904 goto done;
3907 if (rejectfile) {
3908 n = fwrite(line, 1, linelen, rejectfile);
3909 if (n != linelen)
3910 err = got_ferror(outfile, GOT_ERR_IO);
3912 done:
3913 free(line);
3914 return err;
3917 static const struct got_error *
3918 skip_one_line(FILE *f)
3920 char *line = NULL;
3921 size_t linesize = 0;
3922 ssize_t linelen;
3924 linelen = getline(&line, &linesize, f);
3925 if (linelen == -1) {
3926 if (ferror(f))
3927 return got_error_from_errno("getline");
3928 return NULL;
3930 free(line);
3931 return NULL;
3934 static const struct got_error *
3935 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3936 int start_old, int end_old, int start_new, int end_new,
3937 FILE *outfile, FILE *rejectfile)
3939 const struct got_error *err;
3941 /* Copy old file's lines leading up to patch. */
3942 while (!feof(f1) && *line_cur1 < start_old) {
3943 err = copy_one_line(f1, outfile, NULL);
3944 if (err)
3945 return err;
3946 (*line_cur1)++;
3948 /* Skip new file's lines leading up to patch. */
3949 while (!feof(f2) && *line_cur2 < start_new) {
3950 if (rejectfile)
3951 err = copy_one_line(f2, NULL, rejectfile);
3952 else
3953 err = skip_one_line(f2);
3954 if (err)
3955 return err;
3956 (*line_cur2)++;
3958 /* Copy patched lines. */
3959 while (!feof(f2) && *line_cur2 <= end_new) {
3960 err = copy_one_line(f2, outfile, NULL);
3961 if (err)
3962 return err;
3963 (*line_cur2)++;
3965 /* Skip over old file's replaced lines. */
3966 while (!feof(f1) && *line_cur1 <= end_old) {
3967 if (rejectfile)
3968 err = copy_one_line(f1, NULL, rejectfile);
3969 else
3970 err = skip_one_line(f1);
3971 if (err)
3972 return err;
3973 (*line_cur1)++;
3976 return NULL;
3979 static const struct got_error *
3980 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3981 FILE *outfile, FILE *rejectfile)
3983 const struct got_error *err;
3985 if (outfile) {
3986 /* Copy old file's lines until EOF. */
3987 while (!feof(f1)) {
3988 err = copy_one_line(f1, outfile, NULL);
3989 if (err)
3990 return err;
3991 (*line_cur1)++;
3994 if (rejectfile) {
3995 /* Copy new file's lines until EOF. */
3996 while (!feof(f2)) {
3997 err = copy_one_line(f2, NULL, rejectfile);
3998 if (err)
3999 return err;
4000 (*line_cur2)++;
4004 return NULL;
4007 static const struct got_error *
4008 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4009 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4010 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4011 int *line_cur2, FILE *outfile, FILE *rejectfile,
4012 got_worktree_patch_cb patch_cb, void *patch_arg)
4014 const struct got_error *err = NULL;
4015 int start_old = change->cv.a;
4016 int end_old = change->cv.b;
4017 int start_new = change->cv.c;
4018 int end_new = change->cv.d;
4019 long pos1, pos2;
4020 FILE *hunkfile;
4022 *choice = GOT_PATCH_CHOICE_NONE;
4024 hunkfile = got_opentemp();
4025 if (hunkfile == NULL)
4026 return got_error_from_errno("got_opentemp");
4028 pos1 = ftell(f1);
4029 pos2 = ftell(f2);
4031 /* XXX TODO needs error checking */
4032 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4034 if (fseek(f1, pos1, SEEK_SET) == -1) {
4035 err = got_ferror(f1, GOT_ERR_IO);
4036 goto done;
4038 if (fseek(f2, pos2, SEEK_SET) == -1) {
4039 err = got_ferror(f1, GOT_ERR_IO);
4040 goto done;
4042 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4043 err = got_ferror(hunkfile, GOT_ERR_IO);
4044 goto done;
4047 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4048 hunkfile, n, nchanges);
4049 if (err)
4050 goto done;
4052 switch (*choice) {
4053 case GOT_PATCH_CHOICE_YES:
4054 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4055 end_old, start_new, end_new, outfile, rejectfile);
4056 break;
4057 case GOT_PATCH_CHOICE_NO:
4058 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4059 end_old, start_new, end_new, rejectfile, outfile);
4060 break;
4061 case GOT_PATCH_CHOICE_QUIT:
4062 break;
4063 default:
4064 err = got_error(GOT_ERR_PATCH_CHOICE);
4065 break;
4067 done:
4068 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4069 err = got_error_from_errno("fclose");
4070 return err;
4073 struct revert_file_args {
4074 struct got_worktree *worktree;
4075 struct got_fileindex *fileindex;
4076 got_worktree_checkout_cb progress_cb;
4077 void *progress_arg;
4078 got_worktree_patch_cb patch_cb;
4079 void *patch_arg;
4080 struct got_repository *repo;
4083 static const struct got_error *
4084 create_patched_content(char **path_outfile, int reverse_patch,
4085 struct got_object_id *blob_id, const char *path2,
4086 int dirfd2, const char *de_name2,
4087 const char *relpath, struct got_repository *repo,
4088 got_worktree_patch_cb patch_cb, void *patch_arg)
4090 const struct got_error *err;
4091 struct got_blob_object *blob = NULL;
4092 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4093 int fd2 = -1;
4094 char link_target[PATH_MAX];
4095 ssize_t link_len = 0;
4096 char *path1 = NULL, *id_str = NULL;
4097 struct stat sb1, sb2;
4098 struct got_diff_changes *changes = NULL;
4099 struct got_diff_state *ds = NULL;
4100 struct got_diff_args *args = NULL;
4101 struct got_diff_change *change;
4102 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4103 int n = 0;
4105 *path_outfile = NULL;
4107 err = got_object_id_str(&id_str, blob_id);
4108 if (err)
4109 return err;
4111 if (dirfd2 != -1) {
4112 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4113 if (fd2 == -1) {
4114 if (errno != ELOOP) {
4115 err = got_error_from_errno2("openat", path2);
4116 goto done;
4118 link_len = readlinkat(dirfd2, de_name2,
4119 link_target, sizeof(link_target));
4120 if (link_len == -1)
4121 return got_error_from_errno2("readlinkat", path2);
4122 sb2.st_mode = S_IFLNK;
4123 sb2.st_size = link_len;
4125 } else {
4126 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4127 if (fd2 == -1) {
4128 if (errno != ELOOP) {
4129 err = got_error_from_errno2("open", path2);
4130 goto done;
4132 link_len = readlink(path2, link_target,
4133 sizeof(link_target));
4134 if (link_len == -1)
4135 return got_error_from_errno2("readlink", path2);
4136 sb2.st_mode = S_IFLNK;
4137 sb2.st_size = link_len;
4140 if (fd2 != -1) {
4141 if (fstat(fd2, &sb2) == -1) {
4142 err = got_error_from_errno2("fstat", path2);
4143 goto done;
4146 f2 = fdopen(fd2, "r");
4147 if (f2 == NULL) {
4148 err = got_error_from_errno2("fdopen", path2);
4149 goto done;
4151 fd2 = -1;
4152 } else {
4153 size_t n;
4154 f2 = got_opentemp();
4155 if (f2 == NULL) {
4156 err = got_error_from_errno2("got_opentemp", path2);
4157 goto done;
4159 n = fwrite(link_target, 1, link_len, f2);
4160 if (n != link_len) {
4161 err = got_ferror(f2, GOT_ERR_IO);
4162 goto done;
4164 if (fflush(f2) == EOF) {
4165 err = got_error_from_errno("fflush");
4166 goto done;
4168 rewind(f2);
4171 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4172 if (err)
4173 goto done;
4175 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4176 if (err)
4177 goto done;
4179 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4180 if (err)
4181 goto done;
4183 if (stat(path1, &sb1) == -1) {
4184 err = got_error_from_errno2("stat", path1);
4185 goto done;
4188 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4189 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4190 if (err)
4191 goto done;
4193 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4194 if (err)
4195 goto done;
4197 if (fseek(f1, 0L, SEEK_SET) == -1)
4198 return got_ferror(f1, GOT_ERR_IO);
4199 if (fseek(f2, 0L, SEEK_SET) == -1)
4200 return got_ferror(f2, GOT_ERR_IO);
4201 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4202 int choice;
4203 err = apply_or_reject_change(&choice, change, ++n,
4204 changes->nchanges, ds, args, diff_flags, relpath,
4205 f1, f2, &line_cur1, &line_cur2,
4206 reverse_patch ? NULL : outfile,
4207 reverse_patch ? outfile : NULL,
4208 patch_cb, patch_arg);
4209 if (err)
4210 goto done;
4211 if (choice == GOT_PATCH_CHOICE_YES)
4212 have_content = 1;
4213 else if (choice == GOT_PATCH_CHOICE_QUIT)
4214 break;
4216 if (have_content) {
4217 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4218 reverse_patch ? NULL : outfile,
4219 reverse_patch ? outfile : NULL);
4220 if (err)
4221 goto done;
4223 if (!S_ISLNK(sb2.st_mode)) {
4224 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4225 err = got_error_from_errno2("chmod", path2);
4226 goto done;
4230 done:
4231 free(id_str);
4232 if (blob)
4233 got_object_blob_close(blob);
4234 if (f1 && fclose(f1) == EOF && err == NULL)
4235 err = got_error_from_errno2("fclose", path1);
4236 if (f2 && fclose(f2) == EOF && err == NULL)
4237 err = got_error_from_errno2("fclose", path2);
4238 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4239 err = got_error_from_errno2("close", path2);
4240 if (outfile && fclose(outfile) == EOF && err == NULL)
4241 err = got_error_from_errno2("fclose", *path_outfile);
4242 if (path1 && unlink(path1) == -1 && err == NULL)
4243 err = got_error_from_errno2("unlink", path1);
4244 if (err || !have_content) {
4245 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4246 err = got_error_from_errno2("unlink", *path_outfile);
4247 free(*path_outfile);
4248 *path_outfile = NULL;
4250 free(args);
4251 if (ds) {
4252 got_diff_state_free(ds);
4253 free(ds);
4255 if (changes)
4256 got_diff_free_changes(changes);
4257 free(path1);
4258 return err;
4261 static const struct got_error *
4262 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4263 const char *relpath, struct got_object_id *blob_id,
4264 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4265 int dirfd, const char *de_name)
4267 struct revert_file_args *a = arg;
4268 const struct got_error *err = NULL;
4269 char *parent_path = NULL;
4270 struct got_fileindex_entry *ie;
4271 struct got_tree_object *tree = NULL;
4272 struct got_object_id *tree_id = NULL;
4273 const struct got_tree_entry *te = NULL;
4274 char *tree_path = NULL, *te_name;
4275 char *ondisk_path = NULL, *path_content = NULL;
4276 struct got_blob_object *blob = NULL;
4278 /* Reverting a staged deletion is a no-op. */
4279 if (status == GOT_STATUS_DELETE &&
4280 staged_status != GOT_STATUS_NO_CHANGE)
4281 return NULL;
4283 if (status == GOT_STATUS_UNVERSIONED)
4284 return (*a->progress_cb)(a->progress_arg,
4285 GOT_STATUS_UNVERSIONED, relpath);
4287 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4288 if (ie == NULL)
4289 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4291 /* Construct in-repository path of tree which contains this blob. */
4292 err = got_path_dirname(&parent_path, ie->path);
4293 if (err) {
4294 if (err->code != GOT_ERR_BAD_PATH)
4295 goto done;
4296 parent_path = strdup("/");
4297 if (parent_path == NULL) {
4298 err = got_error_from_errno("strdup");
4299 goto done;
4302 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4303 tree_path = strdup(parent_path);
4304 if (tree_path == NULL) {
4305 err = got_error_from_errno("strdup");
4306 goto done;
4308 } else {
4309 if (got_path_is_root_dir(parent_path)) {
4310 tree_path = strdup(a->worktree->path_prefix);
4311 if (tree_path == NULL) {
4312 err = got_error_from_errno("strdup");
4313 goto done;
4315 } else {
4316 if (asprintf(&tree_path, "%s/%s",
4317 a->worktree->path_prefix, parent_path) == -1) {
4318 err = got_error_from_errno("asprintf");
4319 goto done;
4324 err = got_object_id_by_path(&tree_id, a->repo,
4325 a->worktree->base_commit_id, tree_path);
4326 if (err) {
4327 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4328 (status == GOT_STATUS_ADD ||
4329 staged_status == GOT_STATUS_ADD)))
4330 goto done;
4331 } else {
4332 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4333 if (err)
4334 goto done;
4336 te_name = basename(ie->path);
4337 if (te_name == NULL) {
4338 err = got_error_from_errno2("basename", ie->path);
4339 goto done;
4342 te = got_object_tree_find_entry(tree, te_name);
4343 if (te == NULL && status != GOT_STATUS_ADD &&
4344 staged_status != GOT_STATUS_ADD) {
4345 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4346 goto done;
4350 switch (status) {
4351 case GOT_STATUS_ADD:
4352 if (a->patch_cb) {
4353 int choice = GOT_PATCH_CHOICE_NONE;
4354 err = (*a->patch_cb)(&choice, a->patch_arg,
4355 status, ie->path, NULL, 1, 1);
4356 if (err)
4357 goto done;
4358 if (choice != GOT_PATCH_CHOICE_YES)
4359 break;
4361 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4362 ie->path);
4363 if (err)
4364 goto done;
4365 got_fileindex_entry_remove(a->fileindex, ie);
4366 break;
4367 case GOT_STATUS_DELETE:
4368 if (a->patch_cb) {
4369 int choice = GOT_PATCH_CHOICE_NONE;
4370 err = (*a->patch_cb)(&choice, a->patch_arg,
4371 status, ie->path, NULL, 1, 1);
4372 if (err)
4373 goto done;
4374 if (choice != GOT_PATCH_CHOICE_YES)
4375 break;
4377 /* fall through */
4378 case GOT_STATUS_MODIFY:
4379 case GOT_STATUS_MODE_CHANGE:
4380 case GOT_STATUS_CONFLICT:
4381 case GOT_STATUS_MISSING: {
4382 struct got_object_id id;
4383 if (staged_status == GOT_STATUS_ADD ||
4384 staged_status == GOT_STATUS_MODIFY) {
4385 memcpy(id.sha1, ie->staged_blob_sha1,
4386 SHA1_DIGEST_LENGTH);
4387 } else
4388 memcpy(id.sha1, ie->blob_sha1,
4389 SHA1_DIGEST_LENGTH);
4390 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4391 if (err)
4392 goto done;
4394 if (asprintf(&ondisk_path, "%s/%s",
4395 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4396 err = got_error_from_errno("asprintf");
4397 goto done;
4400 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4401 status == GOT_STATUS_CONFLICT)) {
4402 int is_bad_symlink = 0;
4403 err = create_patched_content(&path_content, 1, &id,
4404 ondisk_path, dirfd, de_name, ie->path, a->repo,
4405 a->patch_cb, a->patch_arg);
4406 if (err || path_content == NULL)
4407 break;
4408 if (te && S_ISLNK(te->mode)) {
4409 if (unlink(path_content) == -1) {
4410 err = got_error_from_errno2("unlink",
4411 path_content);
4412 break;
4414 err = install_symlink(&is_bad_symlink,
4415 a->worktree, ondisk_path, ie->path,
4416 blob, 0, 1, 0, a->repo,
4417 a->progress_cb, a->progress_arg);
4418 } else {
4419 if (rename(path_content, ondisk_path) == -1) {
4420 err = got_error_from_errno3("rename",
4421 path_content, ondisk_path);
4422 goto done;
4425 } else {
4426 int is_bad_symlink = 0;
4427 if (te && S_ISLNK(te->mode)) {
4428 err = install_symlink(&is_bad_symlink,
4429 a->worktree, ondisk_path, ie->path,
4430 blob, 0, 1, 0, a->repo,
4431 a->progress_cb, a->progress_arg);
4432 } else {
4433 err = install_blob(a->worktree, ondisk_path,
4434 ie->path,
4435 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4436 got_fileindex_perms_to_st(ie), blob,
4437 0, 1, 0, 0, a->repo,
4438 a->progress_cb, a->progress_arg);
4440 if (err)
4441 goto done;
4442 if (status == GOT_STATUS_DELETE ||
4443 status == GOT_STATUS_MODE_CHANGE) {
4444 err = got_fileindex_entry_update(ie,
4445 ondisk_path, blob->id.sha1,
4446 a->worktree->base_commit_id->sha1, 1);
4447 if (err)
4448 goto done;
4450 if (is_bad_symlink) {
4451 got_fileindex_entry_filetype_set(ie,
4452 GOT_FILEIDX_MODE_BAD_SYMLINK);
4455 break;
4457 default:
4458 break;
4460 done:
4461 free(ondisk_path);
4462 free(path_content);
4463 free(parent_path);
4464 free(tree_path);
4465 if (blob)
4466 got_object_blob_close(blob);
4467 if (tree)
4468 got_object_tree_close(tree);
4469 free(tree_id);
4470 return err;
4473 const struct got_error *
4474 got_worktree_revert(struct got_worktree *worktree,
4475 struct got_pathlist_head *paths,
4476 got_worktree_checkout_cb progress_cb, void *progress_arg,
4477 got_worktree_patch_cb patch_cb, void *patch_arg,
4478 struct got_repository *repo)
4480 struct got_fileindex *fileindex = NULL;
4481 char *fileindex_path = NULL;
4482 const struct got_error *err = NULL, *unlockerr = NULL;
4483 const struct got_error *sync_err = NULL;
4484 struct got_pathlist_entry *pe;
4485 struct revert_file_args rfa;
4487 err = lock_worktree(worktree, LOCK_EX);
4488 if (err)
4489 return err;
4491 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4492 if (err)
4493 goto done;
4495 rfa.worktree = worktree;
4496 rfa.fileindex = fileindex;
4497 rfa.progress_cb = progress_cb;
4498 rfa.progress_arg = progress_arg;
4499 rfa.patch_cb = patch_cb;
4500 rfa.patch_arg = patch_arg;
4501 rfa.repo = repo;
4502 TAILQ_FOREACH(pe, paths, entry) {
4503 err = worktree_status(worktree, pe->path, fileindex, repo,
4504 revert_file, &rfa, NULL, NULL, 0, 0);
4505 if (err)
4506 break;
4508 sync_err = sync_fileindex(fileindex, fileindex_path);
4509 if (sync_err && err == NULL)
4510 err = sync_err;
4511 done:
4512 free(fileindex_path);
4513 if (fileindex)
4514 got_fileindex_free(fileindex);
4515 unlockerr = lock_worktree(worktree, LOCK_SH);
4516 if (unlockerr && err == NULL)
4517 err = unlockerr;
4518 return err;
4521 static void
4522 free_commitable(struct got_commitable *ct)
4524 free(ct->path);
4525 free(ct->in_repo_path);
4526 free(ct->ondisk_path);
4527 free(ct->blob_id);
4528 free(ct->base_blob_id);
4529 free(ct->staged_blob_id);
4530 free(ct->base_commit_id);
4531 free(ct);
4534 struct collect_commitables_arg {
4535 struct got_pathlist_head *commitable_paths;
4536 struct got_repository *repo;
4537 struct got_worktree *worktree;
4538 struct got_fileindex *fileindex;
4539 int have_staged_files;
4540 int allow_bad_symlinks;
4543 static const struct got_error *
4544 collect_commitables(void *arg, unsigned char status,
4545 unsigned char staged_status, const char *relpath,
4546 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4547 struct got_object_id *commit_id, int dirfd, const char *de_name)
4549 struct collect_commitables_arg *a = arg;
4550 const struct got_error *err = NULL;
4551 struct got_commitable *ct = NULL;
4552 struct got_pathlist_entry *new = NULL;
4553 char *parent_path = NULL, *path = NULL;
4554 struct stat sb;
4556 if (a->have_staged_files) {
4557 if (staged_status != GOT_STATUS_MODIFY &&
4558 staged_status != GOT_STATUS_ADD &&
4559 staged_status != GOT_STATUS_DELETE)
4560 return NULL;
4561 } else {
4562 if (status == GOT_STATUS_CONFLICT)
4563 return got_error(GOT_ERR_COMMIT_CONFLICT);
4565 if (status != GOT_STATUS_MODIFY &&
4566 status != GOT_STATUS_MODE_CHANGE &&
4567 status != GOT_STATUS_ADD &&
4568 status != GOT_STATUS_DELETE)
4569 return NULL;
4572 if (asprintf(&path, "/%s", relpath) == -1) {
4573 err = got_error_from_errno("asprintf");
4574 goto done;
4576 if (strcmp(path, "/") == 0) {
4577 parent_path = strdup("");
4578 if (parent_path == NULL)
4579 return got_error_from_errno("strdup");
4580 } else {
4581 err = got_path_dirname(&parent_path, path);
4582 if (err)
4583 return err;
4586 ct = calloc(1, sizeof(*ct));
4587 if (ct == NULL) {
4588 err = got_error_from_errno("calloc");
4589 goto done;
4592 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4593 relpath) == -1) {
4594 err = got_error_from_errno("asprintf");
4595 goto done;
4598 if (staged_status == GOT_STATUS_ADD ||
4599 staged_status == GOT_STATUS_MODIFY) {
4600 struct got_fileindex_entry *ie;
4601 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4602 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4603 case GOT_FILEIDX_MODE_REGULAR_FILE:
4604 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4605 ct->mode = S_IFREG;
4606 break;
4607 case GOT_FILEIDX_MODE_SYMLINK:
4608 ct->mode = S_IFLNK;
4609 break;
4610 default:
4611 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4612 goto done;
4614 ct->mode |= got_fileindex_entry_perms_get(ie);
4615 } else if (status != GOT_STATUS_DELETE &&
4616 staged_status != GOT_STATUS_DELETE) {
4617 if (dirfd != -1) {
4618 if (fstatat(dirfd, de_name, &sb,
4619 AT_SYMLINK_NOFOLLOW) == -1) {
4620 err = got_error_from_errno2("fstatat",
4621 ct->ondisk_path);
4622 goto done;
4624 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4625 err = got_error_from_errno2("lstat", ct->ondisk_path);
4626 goto done;
4628 ct->mode = sb.st_mode;
4631 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4632 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4633 relpath) == -1) {
4634 err = got_error_from_errno("asprintf");
4635 goto done;
4638 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4639 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4640 int is_bad_symlink;
4641 char target_path[PATH_MAX];
4642 ssize_t target_len;
4643 target_len = readlink(ct->ondisk_path, target_path,
4644 sizeof(target_path));
4645 if (target_len == -1) {
4646 err = got_error_from_errno2("readlink",
4647 ct->ondisk_path);
4648 goto done;
4650 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4651 target_len, ct->ondisk_path, a->worktree->root_path);
4652 if (err)
4653 goto done;
4654 if (is_bad_symlink) {
4655 err = got_error_path(ct->ondisk_path,
4656 GOT_ERR_BAD_SYMLINK);
4657 goto done;
4662 ct->status = status;
4663 ct->staged_status = staged_status;
4664 ct->blob_id = NULL; /* will be filled in when blob gets created */
4665 if (ct->status != GOT_STATUS_ADD &&
4666 ct->staged_status != GOT_STATUS_ADD) {
4667 ct->base_blob_id = got_object_id_dup(blob_id);
4668 if (ct->base_blob_id == NULL) {
4669 err = got_error_from_errno("got_object_id_dup");
4670 goto done;
4672 ct->base_commit_id = got_object_id_dup(commit_id);
4673 if (ct->base_commit_id == NULL) {
4674 err = got_error_from_errno("got_object_id_dup");
4675 goto done;
4678 if (ct->staged_status == GOT_STATUS_ADD ||
4679 ct->staged_status == GOT_STATUS_MODIFY) {
4680 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4681 if (ct->staged_blob_id == NULL) {
4682 err = got_error_from_errno("got_object_id_dup");
4683 goto done;
4686 ct->path = strdup(path);
4687 if (ct->path == NULL) {
4688 err = got_error_from_errno("strdup");
4689 goto done;
4691 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4692 done:
4693 if (ct && (err || new == NULL))
4694 free_commitable(ct);
4695 free(parent_path);
4696 free(path);
4697 return err;
4700 static const struct got_error *write_tree(struct got_object_id **, int *,
4701 struct got_tree_object *, const char *, struct got_pathlist_head *,
4702 got_worktree_status_cb status_cb, void *status_arg,
4703 struct got_repository *);
4705 static const struct got_error *
4706 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4707 struct got_tree_entry *te, const char *parent_path,
4708 struct got_pathlist_head *commitable_paths,
4709 got_worktree_status_cb status_cb, void *status_arg,
4710 struct got_repository *repo)
4712 const struct got_error *err = NULL;
4713 struct got_tree_object *subtree;
4714 char *subpath;
4716 if (asprintf(&subpath, "%s%s%s", parent_path,
4717 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4718 return got_error_from_errno("asprintf");
4720 err = got_object_open_as_tree(&subtree, repo, &te->id);
4721 if (err)
4722 return err;
4724 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4725 commitable_paths, status_cb, status_arg, repo);
4726 got_object_tree_close(subtree);
4727 free(subpath);
4728 return err;
4731 static const struct got_error *
4732 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4734 const struct got_error *err = NULL;
4735 char *ct_parent_path = NULL;
4737 *match = 0;
4739 if (strchr(ct->in_repo_path, '/') == NULL) {
4740 *match = got_path_is_root_dir(path);
4741 return NULL;
4744 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4745 if (err)
4746 return err;
4747 *match = (strcmp(path, ct_parent_path) == 0);
4748 free(ct_parent_path);
4749 return err;
4752 static mode_t
4753 get_ct_file_mode(struct got_commitable *ct)
4755 if (S_ISLNK(ct->mode))
4756 return S_IFLNK;
4758 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4761 static const struct got_error *
4762 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4763 struct got_tree_entry *te, struct got_commitable *ct)
4765 const struct got_error *err = NULL;
4767 *new_te = NULL;
4769 err = got_object_tree_entry_dup(new_te, te);
4770 if (err)
4771 goto done;
4773 (*new_te)->mode = get_ct_file_mode(ct);
4775 if (ct->staged_status == GOT_STATUS_MODIFY)
4776 memcpy(&(*new_te)->id, ct->staged_blob_id,
4777 sizeof((*new_te)->id));
4778 else
4779 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4780 done:
4781 if (err && *new_te) {
4782 free(*new_te);
4783 *new_te = NULL;
4785 return err;
4788 static const struct got_error *
4789 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4790 struct got_commitable *ct)
4792 const struct got_error *err = NULL;
4793 char *ct_name;
4795 *new_te = NULL;
4797 *new_te = calloc(1, sizeof(**new_te));
4798 if (*new_te == NULL)
4799 return got_error_from_errno("calloc");
4801 ct_name = basename(ct->path);
4802 if (ct_name == NULL) {
4803 err = got_error_from_errno2("basename", ct->path);
4804 goto done;
4806 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4807 sizeof((*new_te)->name)) {
4808 err = got_error(GOT_ERR_NO_SPACE);
4809 goto done;
4812 (*new_te)->mode = get_ct_file_mode(ct);
4814 if (ct->staged_status == GOT_STATUS_ADD)
4815 memcpy(&(*new_te)->id, ct->staged_blob_id,
4816 sizeof((*new_te)->id));
4817 else
4818 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4819 done:
4820 if (err && *new_te) {
4821 free(*new_te);
4822 *new_te = NULL;
4824 return err;
4827 static const struct got_error *
4828 insert_tree_entry(struct got_tree_entry *new_te,
4829 struct got_pathlist_head *paths)
4831 const struct got_error *err = NULL;
4832 struct got_pathlist_entry *new_pe;
4834 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4835 if (err)
4836 return err;
4837 if (new_pe == NULL)
4838 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4839 return NULL;
4842 static const struct got_error *
4843 report_ct_status(struct got_commitable *ct,
4844 got_worktree_status_cb status_cb, void *status_arg)
4846 const char *ct_path = ct->path;
4847 unsigned char status;
4849 while (ct_path[0] == '/')
4850 ct_path++;
4852 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4853 status = ct->staged_status;
4854 else
4855 status = ct->status;
4857 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4858 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4861 static const struct got_error *
4862 match_modified_subtree(int *modified, struct got_tree_entry *te,
4863 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4865 const struct got_error *err = NULL;
4866 struct got_pathlist_entry *pe;
4867 char *te_path;
4869 *modified = 0;
4871 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4872 got_path_is_root_dir(base_tree_path) ? "" : "/",
4873 te->name) == -1)
4874 return got_error_from_errno("asprintf");
4876 TAILQ_FOREACH(pe, commitable_paths, entry) {
4877 struct got_commitable *ct = pe->data;
4878 *modified = got_path_is_child(ct->in_repo_path, te_path,
4879 strlen(te_path));
4880 if (*modified)
4881 break;
4884 free(te_path);
4885 return err;
4888 static const struct got_error *
4889 match_deleted_or_modified_ct(struct got_commitable **ctp,
4890 struct got_tree_entry *te, const char *base_tree_path,
4891 struct got_pathlist_head *commitable_paths)
4893 const struct got_error *err = NULL;
4894 struct got_pathlist_entry *pe;
4896 *ctp = NULL;
4898 TAILQ_FOREACH(pe, commitable_paths, entry) {
4899 struct got_commitable *ct = pe->data;
4900 char *ct_name = NULL;
4901 int path_matches;
4903 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4904 if (ct->status != GOT_STATUS_MODIFY &&
4905 ct->status != GOT_STATUS_MODE_CHANGE &&
4906 ct->status != GOT_STATUS_DELETE)
4907 continue;
4908 } else {
4909 if (ct->staged_status != GOT_STATUS_MODIFY &&
4910 ct->staged_status != GOT_STATUS_DELETE)
4911 continue;
4914 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4915 continue;
4917 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4918 if (err)
4919 return err;
4920 if (!path_matches)
4921 continue;
4923 ct_name = basename(pe->path);
4924 if (ct_name == NULL)
4925 return got_error_from_errno2("basename", pe->path);
4927 if (strcmp(te->name, ct_name) != 0)
4928 continue;
4930 *ctp = ct;
4931 break;
4934 return err;
4937 static const struct got_error *
4938 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4939 const char *child_path, const char *path_base_tree,
4940 struct got_pathlist_head *commitable_paths,
4941 got_worktree_status_cb status_cb, void *status_arg,
4942 struct got_repository *repo)
4944 const struct got_error *err = NULL;
4945 struct got_tree_entry *new_te;
4946 char *subtree_path;
4947 struct got_object_id *id = NULL;
4948 int nentries;
4950 *new_tep = NULL;
4952 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4953 got_path_is_root_dir(path_base_tree) ? "" : "/",
4954 child_path) == -1)
4955 return got_error_from_errno("asprintf");
4957 new_te = calloc(1, sizeof(*new_te));
4958 if (new_te == NULL)
4959 return got_error_from_errno("calloc");
4960 new_te->mode = S_IFDIR;
4962 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4963 sizeof(new_te->name)) {
4964 err = got_error(GOT_ERR_NO_SPACE);
4965 goto done;
4967 err = write_tree(&id, &nentries, NULL, subtree_path,
4968 commitable_paths, status_cb, status_arg, repo);
4969 if (err) {
4970 free(new_te);
4971 goto done;
4973 memcpy(&new_te->id, id, sizeof(new_te->id));
4974 done:
4975 free(id);
4976 free(subtree_path);
4977 if (err == NULL)
4978 *new_tep = new_te;
4979 return err;
4982 static const struct got_error *
4983 write_tree(struct got_object_id **new_tree_id, int *nentries,
4984 struct got_tree_object *base_tree, const char *path_base_tree,
4985 struct got_pathlist_head *commitable_paths,
4986 got_worktree_status_cb status_cb, void *status_arg,
4987 struct got_repository *repo)
4989 const struct got_error *err = NULL;
4990 struct got_pathlist_head paths;
4991 struct got_tree_entry *te, *new_te = NULL;
4992 struct got_pathlist_entry *pe;
4994 TAILQ_INIT(&paths);
4995 *nentries = 0;
4997 /* Insert, and recurse into, newly added entries first. */
4998 TAILQ_FOREACH(pe, commitable_paths, entry) {
4999 struct got_commitable *ct = pe->data;
5000 char *child_path = NULL, *slash;
5002 if ((ct->status != GOT_STATUS_ADD &&
5003 ct->staged_status != GOT_STATUS_ADD) ||
5004 (ct->flags & GOT_COMMITABLE_ADDED))
5005 continue;
5007 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5008 strlen(path_base_tree)))
5009 continue;
5011 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5012 ct->in_repo_path);
5013 if (err)
5014 goto done;
5016 slash = strchr(child_path, '/');
5017 if (slash == NULL) {
5018 err = alloc_added_blob_tree_entry(&new_te, ct);
5019 if (err)
5020 goto done;
5021 err = report_ct_status(ct, status_cb, status_arg);
5022 if (err)
5023 goto done;
5024 ct->flags |= GOT_COMMITABLE_ADDED;
5025 err = insert_tree_entry(new_te, &paths);
5026 if (err)
5027 goto done;
5028 (*nentries)++;
5029 } else {
5030 *slash = '\0'; /* trim trailing path components */
5031 if (base_tree == NULL ||
5032 got_object_tree_find_entry(base_tree, child_path)
5033 == NULL) {
5034 err = make_subtree_for_added_blob(&new_te,
5035 child_path, path_base_tree,
5036 commitable_paths, status_cb, status_arg,
5037 repo);
5038 if (err)
5039 goto done;
5040 err = insert_tree_entry(new_te, &paths);
5041 if (err)
5042 goto done;
5043 (*nentries)++;
5048 if (base_tree) {
5049 int i, nbase_entries;
5050 /* Handle modified and deleted entries. */
5051 nbase_entries = got_object_tree_get_nentries(base_tree);
5052 for (i = 0; i < nbase_entries; i++) {
5053 struct got_commitable *ct = NULL;
5055 te = got_object_tree_get_entry(base_tree, i);
5056 if (got_object_tree_entry_is_submodule(te)) {
5057 /* Entry is a submodule; just copy it. */
5058 err = got_object_tree_entry_dup(&new_te, te);
5059 if (err)
5060 goto done;
5061 err = insert_tree_entry(new_te, &paths);
5062 if (err)
5063 goto done;
5064 (*nentries)++;
5065 continue;
5068 if (S_ISDIR(te->mode)) {
5069 int modified;
5070 err = got_object_tree_entry_dup(&new_te, te);
5071 if (err)
5072 goto done;
5073 err = match_modified_subtree(&modified, te,
5074 path_base_tree, commitable_paths);
5075 if (err)
5076 goto done;
5077 /* Avoid recursion into unmodified subtrees. */
5078 if (modified) {
5079 struct got_object_id *new_id;
5080 int nsubentries;
5081 err = write_subtree(&new_id,
5082 &nsubentries, te,
5083 path_base_tree, commitable_paths,
5084 status_cb, status_arg, repo);
5085 if (err)
5086 goto done;
5087 if (nsubentries == 0) {
5088 /* All entries were deleted. */
5089 free(new_id);
5090 continue;
5092 memcpy(&new_te->id, new_id,
5093 sizeof(new_te->id));
5094 free(new_id);
5096 err = insert_tree_entry(new_te, &paths);
5097 if (err)
5098 goto done;
5099 (*nentries)++;
5100 continue;
5103 err = match_deleted_or_modified_ct(&ct, te,
5104 path_base_tree, commitable_paths);
5105 if (err)
5106 goto done;
5107 if (ct) {
5108 /* NB: Deleted entries get dropped here. */
5109 if (ct->status == GOT_STATUS_MODIFY ||
5110 ct->status == GOT_STATUS_MODE_CHANGE ||
5111 ct->staged_status == GOT_STATUS_MODIFY) {
5112 err = alloc_modified_blob_tree_entry(
5113 &new_te, te, ct);
5114 if (err)
5115 goto done;
5116 err = insert_tree_entry(new_te, &paths);
5117 if (err)
5118 goto done;
5119 (*nentries)++;
5121 err = report_ct_status(ct, status_cb,
5122 status_arg);
5123 if (err)
5124 goto done;
5125 } else {
5126 /* Entry is unchanged; just copy it. */
5127 err = got_object_tree_entry_dup(&new_te, te);
5128 if (err)
5129 goto done;
5130 err = insert_tree_entry(new_te, &paths);
5131 if (err)
5132 goto done;
5133 (*nentries)++;
5138 /* Write new list of entries; deleted entries have been dropped. */
5139 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5140 done:
5141 got_pathlist_free(&paths);
5142 return err;
5145 static const struct got_error *
5146 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5147 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5148 int have_staged_files)
5150 const struct got_error *err = NULL;
5151 struct got_pathlist_entry *pe;
5153 TAILQ_FOREACH(pe, commitable_paths, entry) {
5154 struct got_fileindex_entry *ie;
5155 struct got_commitable *ct = pe->data;
5157 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5158 if (ie) {
5159 if (ct->status == GOT_STATUS_DELETE ||
5160 ct->staged_status == GOT_STATUS_DELETE) {
5161 got_fileindex_entry_remove(fileindex, ie);
5162 } else if (ct->staged_status == GOT_STATUS_ADD ||
5163 ct->staged_status == GOT_STATUS_MODIFY) {
5164 got_fileindex_entry_stage_set(ie,
5165 GOT_FILEIDX_STAGE_NONE);
5166 err = got_fileindex_entry_update(ie,
5167 ct->ondisk_path, ct->staged_blob_id->sha1,
5168 new_base_commit_id->sha1,
5169 !have_staged_files);
5170 } else
5171 err = got_fileindex_entry_update(ie,
5172 ct->ondisk_path, ct->blob_id->sha1,
5173 new_base_commit_id->sha1,
5174 !have_staged_files);
5175 } else {
5176 err = got_fileindex_entry_alloc(&ie, pe->path);
5177 if (err)
5178 break;
5179 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5180 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5181 if (err) {
5182 got_fileindex_entry_free(ie);
5183 break;
5185 err = got_fileindex_entry_add(fileindex, ie);
5186 if (err) {
5187 got_fileindex_entry_free(ie);
5188 break;
5192 return err;
5196 static const struct got_error *
5197 check_out_of_date(const char *in_repo_path, unsigned char status,
5198 unsigned char staged_status, struct got_object_id *base_blob_id,
5199 struct got_object_id *base_commit_id,
5200 struct got_object_id *head_commit_id, struct got_repository *repo,
5201 int ood_errcode)
5203 const struct got_error *err = NULL;
5204 struct got_object_id *id = NULL;
5206 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5207 /* Trivial case: base commit == head commit */
5208 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5209 return NULL;
5211 * Ensure file content which local changes were based
5212 * on matches file content in the branch head.
5214 err = got_object_id_by_path(&id, repo, head_commit_id,
5215 in_repo_path);
5216 if (err) {
5217 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5218 err = got_error(ood_errcode);
5219 goto done;
5220 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5221 err = got_error(ood_errcode);
5222 } else {
5223 /* Require that added files don't exist in the branch head. */
5224 err = got_object_id_by_path(&id, repo, head_commit_id,
5225 in_repo_path);
5226 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5227 goto done;
5228 err = id ? got_error(ood_errcode) : NULL;
5230 done:
5231 free(id);
5232 return err;
5235 const struct got_error *
5236 commit_worktree(struct got_object_id **new_commit_id,
5237 struct got_pathlist_head *commitable_paths,
5238 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5239 const char *author, const char *committer,
5240 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5241 got_worktree_status_cb status_cb, void *status_arg,
5242 struct got_repository *repo)
5244 const struct got_error *err = NULL, *unlockerr = NULL;
5245 struct got_pathlist_entry *pe;
5246 const char *head_ref_name = NULL;
5247 struct got_commit_object *head_commit = NULL;
5248 struct got_reference *head_ref2 = NULL;
5249 struct got_object_id *head_commit_id2 = NULL;
5250 struct got_tree_object *head_tree = NULL;
5251 struct got_object_id *new_tree_id = NULL;
5252 int nentries;
5253 struct got_object_id_queue parent_ids;
5254 struct got_object_qid *pid = NULL;
5255 char *logmsg = NULL;
5257 *new_commit_id = NULL;
5259 SIMPLEQ_INIT(&parent_ids);
5261 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5262 if (err)
5263 goto done;
5265 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5266 if (err)
5267 goto done;
5269 if (commit_msg_cb != NULL) {
5270 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5271 if (err)
5272 goto done;
5275 if (logmsg == NULL || strlen(logmsg) == 0) {
5276 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5277 goto done;
5280 /* Create blobs from added and modified files and record their IDs. */
5281 TAILQ_FOREACH(pe, commitable_paths, entry) {
5282 struct got_commitable *ct = pe->data;
5283 char *ondisk_path;
5285 /* Blobs for staged files already exist. */
5286 if (ct->staged_status == GOT_STATUS_ADD ||
5287 ct->staged_status == GOT_STATUS_MODIFY)
5288 continue;
5290 if (ct->status != GOT_STATUS_ADD &&
5291 ct->status != GOT_STATUS_MODIFY &&
5292 ct->status != GOT_STATUS_MODE_CHANGE)
5293 continue;
5295 if (asprintf(&ondisk_path, "%s/%s",
5296 worktree->root_path, pe->path) == -1) {
5297 err = got_error_from_errno("asprintf");
5298 goto done;
5300 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5301 free(ondisk_path);
5302 if (err)
5303 goto done;
5306 /* Recursively write new tree objects. */
5307 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5308 commitable_paths, status_cb, status_arg, repo);
5309 if (err)
5310 goto done;
5312 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5313 if (err)
5314 goto done;
5315 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5316 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5317 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5318 got_object_qid_free(pid);
5319 if (logmsg != NULL)
5320 free(logmsg);
5321 if (err)
5322 goto done;
5324 /* Check if a concurrent commit to our branch has occurred. */
5325 head_ref_name = got_worktree_get_head_ref_name(worktree);
5326 if (head_ref_name == NULL) {
5327 err = got_error_from_errno("got_worktree_get_head_ref_name");
5328 goto done;
5330 /* Lock the reference here to prevent concurrent modification. */
5331 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5332 if (err)
5333 goto done;
5334 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5335 if (err)
5336 goto done;
5337 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5338 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5339 goto done;
5341 /* Update branch head in repository. */
5342 err = got_ref_change_ref(head_ref2, *new_commit_id);
5343 if (err)
5344 goto done;
5345 err = got_ref_write(head_ref2, repo);
5346 if (err)
5347 goto done;
5349 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5350 if (err)
5351 goto done;
5353 err = ref_base_commit(worktree, repo);
5354 if (err)
5355 goto done;
5356 done:
5357 if (head_tree)
5358 got_object_tree_close(head_tree);
5359 if (head_commit)
5360 got_object_commit_close(head_commit);
5361 free(head_commit_id2);
5362 if (head_ref2) {
5363 unlockerr = got_ref_unlock(head_ref2);
5364 if (unlockerr && err == NULL)
5365 err = unlockerr;
5366 got_ref_close(head_ref2);
5368 return err;
5371 static const struct got_error *
5372 check_path_is_commitable(const char *path,
5373 struct got_pathlist_head *commitable_paths)
5375 struct got_pathlist_entry *cpe = NULL;
5376 size_t path_len = strlen(path);
5378 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5379 struct got_commitable *ct = cpe->data;
5380 const char *ct_path = ct->path;
5382 while (ct_path[0] == '/')
5383 ct_path++;
5385 if (strcmp(path, ct_path) == 0 ||
5386 got_path_is_child(ct_path, path, path_len))
5387 break;
5390 if (cpe == NULL)
5391 return got_error_path(path, GOT_ERR_BAD_PATH);
5393 return NULL;
5396 static const struct got_error *
5397 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5399 int *have_staged_files = arg;
5401 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5402 *have_staged_files = 1;
5403 return got_error(GOT_ERR_CANCELLED);
5406 return NULL;
5409 static const struct got_error *
5410 check_non_staged_files(struct got_fileindex *fileindex,
5411 struct got_pathlist_head *paths)
5413 struct got_pathlist_entry *pe;
5414 struct got_fileindex_entry *ie;
5416 TAILQ_FOREACH(pe, paths, entry) {
5417 if (pe->path[0] == '\0')
5418 continue;
5419 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5420 if (ie == NULL)
5421 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5422 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5423 return got_error_path(pe->path,
5424 GOT_ERR_FILE_NOT_STAGED);
5427 return NULL;
5430 const struct got_error *
5431 got_worktree_commit(struct got_object_id **new_commit_id,
5432 struct got_worktree *worktree, struct got_pathlist_head *paths,
5433 const char *author, const char *committer, int allow_bad_symlinks,
5434 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5435 got_worktree_status_cb status_cb, void *status_arg,
5436 struct got_repository *repo)
5438 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5439 struct got_fileindex *fileindex = NULL;
5440 char *fileindex_path = NULL;
5441 struct got_pathlist_head commitable_paths;
5442 struct collect_commitables_arg cc_arg;
5443 struct got_pathlist_entry *pe;
5444 struct got_reference *head_ref = NULL;
5445 struct got_object_id *head_commit_id = NULL;
5446 int have_staged_files = 0;
5448 *new_commit_id = NULL;
5450 TAILQ_INIT(&commitable_paths);
5452 err = lock_worktree(worktree, LOCK_EX);
5453 if (err)
5454 goto done;
5456 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5457 if (err)
5458 goto done;
5460 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5461 if (err)
5462 goto done;
5464 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5465 if (err)
5466 goto done;
5468 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5469 &have_staged_files);
5470 if (err && err->code != GOT_ERR_CANCELLED)
5471 goto done;
5472 if (have_staged_files) {
5473 err = check_non_staged_files(fileindex, paths);
5474 if (err)
5475 goto done;
5478 cc_arg.commitable_paths = &commitable_paths;
5479 cc_arg.worktree = worktree;
5480 cc_arg.fileindex = fileindex;
5481 cc_arg.repo = repo;
5482 cc_arg.have_staged_files = have_staged_files;
5483 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5484 TAILQ_FOREACH(pe, paths, entry) {
5485 err = worktree_status(worktree, pe->path, fileindex, repo,
5486 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5487 if (err)
5488 goto done;
5491 if (TAILQ_EMPTY(&commitable_paths)) {
5492 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5493 goto done;
5496 TAILQ_FOREACH(pe, paths, entry) {
5497 err = check_path_is_commitable(pe->path, &commitable_paths);
5498 if (err)
5499 goto done;
5502 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5503 struct got_commitable *ct = pe->data;
5504 const char *ct_path = ct->in_repo_path;
5506 while (ct_path[0] == '/')
5507 ct_path++;
5508 err = check_out_of_date(ct_path, ct->status,
5509 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5510 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5511 if (err)
5512 goto done;
5516 err = commit_worktree(new_commit_id, &commitable_paths,
5517 head_commit_id, worktree, author, committer,
5518 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5519 if (err)
5520 goto done;
5522 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5523 fileindex, have_staged_files);
5524 sync_err = sync_fileindex(fileindex, fileindex_path);
5525 if (sync_err && err == NULL)
5526 err = sync_err;
5527 done:
5528 if (fileindex)
5529 got_fileindex_free(fileindex);
5530 free(fileindex_path);
5531 unlockerr = lock_worktree(worktree, LOCK_SH);
5532 if (unlockerr && err == NULL)
5533 err = unlockerr;
5534 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5535 struct got_commitable *ct = pe->data;
5536 free_commitable(ct);
5538 got_pathlist_free(&commitable_paths);
5539 return err;
5542 const char *
5543 got_commitable_get_path(struct got_commitable *ct)
5545 return ct->path;
5548 unsigned int
5549 got_commitable_get_status(struct got_commitable *ct)
5551 return ct->status;
5554 struct check_rebase_ok_arg {
5555 struct got_worktree *worktree;
5556 struct got_repository *repo;
5559 static const struct got_error *
5560 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5562 const struct got_error *err = NULL;
5563 struct check_rebase_ok_arg *a = arg;
5564 unsigned char status;
5565 struct stat sb;
5566 char *ondisk_path;
5568 /* Reject rebase of a work tree with mixed base commits. */
5569 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5570 SHA1_DIGEST_LENGTH))
5571 return got_error(GOT_ERR_MIXED_COMMITS);
5573 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5574 == -1)
5575 return got_error_from_errno("asprintf");
5577 /* Reject rebase of a work tree with modified or staged files. */
5578 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5579 free(ondisk_path);
5580 if (err)
5581 return err;
5583 if (status != GOT_STATUS_NO_CHANGE)
5584 return got_error(GOT_ERR_MODIFIED);
5585 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5586 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5588 return NULL;
5591 const struct got_error *
5592 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5593 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5594 struct got_worktree *worktree, struct got_reference *branch,
5595 struct got_repository *repo)
5597 const struct got_error *err = NULL;
5598 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5599 char *branch_ref_name = NULL;
5600 char *fileindex_path = NULL;
5601 struct check_rebase_ok_arg ok_arg;
5602 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5603 struct got_object_id *wt_branch_tip = NULL;
5605 *new_base_branch_ref = NULL;
5606 *tmp_branch = NULL;
5607 *fileindex = NULL;
5609 err = lock_worktree(worktree, LOCK_EX);
5610 if (err)
5611 return err;
5613 err = open_fileindex(fileindex, &fileindex_path, worktree);
5614 if (err)
5615 goto done;
5617 ok_arg.worktree = worktree;
5618 ok_arg.repo = repo;
5619 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5620 &ok_arg);
5621 if (err)
5622 goto done;
5624 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5625 if (err)
5626 goto done;
5628 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5629 if (err)
5630 goto done;
5632 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5633 if (err)
5634 goto done;
5636 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5637 0);
5638 if (err)
5639 goto done;
5641 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5642 if (err)
5643 goto done;
5644 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5645 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5646 goto done;
5649 err = got_ref_alloc_symref(new_base_branch_ref,
5650 new_base_branch_ref_name, wt_branch);
5651 if (err)
5652 goto done;
5653 err = got_ref_write(*new_base_branch_ref, repo);
5654 if (err)
5655 goto done;
5657 /* TODO Lock original branch's ref while rebasing? */
5659 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5660 if (err)
5661 goto done;
5663 err = got_ref_write(branch_ref, repo);
5664 if (err)
5665 goto done;
5667 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5668 worktree->base_commit_id);
5669 if (err)
5670 goto done;
5671 err = got_ref_write(*tmp_branch, repo);
5672 if (err)
5673 goto done;
5675 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5676 if (err)
5677 goto done;
5678 done:
5679 free(fileindex_path);
5680 free(tmp_branch_name);
5681 free(new_base_branch_ref_name);
5682 free(branch_ref_name);
5683 if (branch_ref)
5684 got_ref_close(branch_ref);
5685 if (wt_branch)
5686 got_ref_close(wt_branch);
5687 free(wt_branch_tip);
5688 if (err) {
5689 if (*new_base_branch_ref) {
5690 got_ref_close(*new_base_branch_ref);
5691 *new_base_branch_ref = NULL;
5693 if (*tmp_branch) {
5694 got_ref_close(*tmp_branch);
5695 *tmp_branch = NULL;
5697 if (*fileindex) {
5698 got_fileindex_free(*fileindex);
5699 *fileindex = NULL;
5701 lock_worktree(worktree, LOCK_SH);
5703 return err;
5706 const struct got_error *
5707 got_worktree_rebase_continue(struct got_object_id **commit_id,
5708 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5709 struct got_reference **branch, struct got_fileindex **fileindex,
5710 struct got_worktree *worktree, struct got_repository *repo)
5712 const struct got_error *err;
5713 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5714 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5715 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5716 char *fileindex_path = NULL;
5717 int have_staged_files = 0;
5719 *commit_id = NULL;
5720 *new_base_branch = NULL;
5721 *tmp_branch = NULL;
5722 *branch = NULL;
5723 *fileindex = NULL;
5725 err = lock_worktree(worktree, LOCK_EX);
5726 if (err)
5727 return err;
5729 err = open_fileindex(fileindex, &fileindex_path, worktree);
5730 if (err)
5731 goto done;
5733 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5734 &have_staged_files);
5735 if (err && err->code != GOT_ERR_CANCELLED)
5736 goto done;
5737 if (have_staged_files) {
5738 err = got_error(GOT_ERR_STAGED_PATHS);
5739 goto done;
5742 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5743 if (err)
5744 goto done;
5746 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5747 if (err)
5748 goto done;
5750 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5751 if (err)
5752 goto done;
5754 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5755 if (err)
5756 goto done;
5758 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5759 if (err)
5760 goto done;
5762 err = got_ref_open(branch, repo,
5763 got_ref_get_symref_target(branch_ref), 0);
5764 if (err)
5765 goto done;
5767 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5768 if (err)
5769 goto done;
5771 err = got_ref_resolve(commit_id, repo, commit_ref);
5772 if (err)
5773 goto done;
5775 err = got_ref_open(new_base_branch, repo,
5776 new_base_branch_ref_name, 0);
5777 if (err)
5778 goto done;
5780 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5781 if (err)
5782 goto done;
5783 done:
5784 free(commit_ref_name);
5785 free(branch_ref_name);
5786 free(fileindex_path);
5787 if (commit_ref)
5788 got_ref_close(commit_ref);
5789 if (branch_ref)
5790 got_ref_close(branch_ref);
5791 if (err) {
5792 free(*commit_id);
5793 *commit_id = NULL;
5794 if (*tmp_branch) {
5795 got_ref_close(*tmp_branch);
5796 *tmp_branch = NULL;
5798 if (*new_base_branch) {
5799 got_ref_close(*new_base_branch);
5800 *new_base_branch = NULL;
5802 if (*branch) {
5803 got_ref_close(*branch);
5804 *branch = NULL;
5806 if (*fileindex) {
5807 got_fileindex_free(*fileindex);
5808 *fileindex = NULL;
5810 lock_worktree(worktree, LOCK_SH);
5812 return err;
5815 const struct got_error *
5816 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5818 const struct got_error *err;
5819 char *tmp_branch_name = NULL;
5821 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5822 if (err)
5823 return err;
5825 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5826 free(tmp_branch_name);
5827 return NULL;
5830 static const struct got_error *
5831 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5832 char **logmsg, void *arg)
5834 *logmsg = arg;
5835 return NULL;
5838 static const struct got_error *
5839 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5840 const char *path, struct got_object_id *blob_id,
5841 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5842 int dirfd, const char *de_name)
5844 return NULL;
5847 struct collect_merged_paths_arg {
5848 got_worktree_checkout_cb progress_cb;
5849 void *progress_arg;
5850 struct got_pathlist_head *merged_paths;
5853 static const struct got_error *
5854 collect_merged_paths(void *arg, unsigned char status, const char *path)
5856 const struct got_error *err;
5857 struct collect_merged_paths_arg *a = arg;
5858 char *p;
5859 struct got_pathlist_entry *new;
5861 err = (*a->progress_cb)(a->progress_arg, status, path);
5862 if (err)
5863 return err;
5865 if (status != GOT_STATUS_MERGE &&
5866 status != GOT_STATUS_ADD &&
5867 status != GOT_STATUS_DELETE &&
5868 status != GOT_STATUS_CONFLICT)
5869 return NULL;
5871 p = strdup(path);
5872 if (p == NULL)
5873 return got_error_from_errno("strdup");
5875 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5876 if (err || new == NULL)
5877 free(p);
5878 return err;
5881 void
5882 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5884 struct got_pathlist_entry *pe;
5886 TAILQ_FOREACH(pe, merged_paths, entry)
5887 free((char *)pe->path);
5889 got_pathlist_free(merged_paths);
5892 static const struct got_error *
5893 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5894 int is_rebase, struct got_repository *repo)
5896 const struct got_error *err;
5897 struct got_reference *commit_ref = NULL;
5899 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5900 if (err) {
5901 if (err->code != GOT_ERR_NOT_REF)
5902 goto done;
5903 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5904 if (err)
5905 goto done;
5906 err = got_ref_write(commit_ref, repo);
5907 if (err)
5908 goto done;
5909 } else if (is_rebase) {
5910 struct got_object_id *stored_id;
5911 int cmp;
5913 err = got_ref_resolve(&stored_id, repo, commit_ref);
5914 if (err)
5915 goto done;
5916 cmp = got_object_id_cmp(commit_id, stored_id);
5917 free(stored_id);
5918 if (cmp != 0) {
5919 err = got_error(GOT_ERR_REBASE_COMMITID);
5920 goto done;
5923 done:
5924 if (commit_ref)
5925 got_ref_close(commit_ref);
5926 return err;
5929 static const struct got_error *
5930 rebase_merge_files(struct got_pathlist_head *merged_paths,
5931 const char *commit_ref_name, struct got_worktree *worktree,
5932 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5933 struct got_object_id *commit_id, struct got_repository *repo,
5934 got_worktree_checkout_cb progress_cb, void *progress_arg,
5935 got_cancel_cb cancel_cb, void *cancel_arg)
5937 const struct got_error *err;
5938 struct got_reference *commit_ref = NULL;
5939 struct collect_merged_paths_arg cmp_arg;
5940 char *fileindex_path;
5942 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5944 err = get_fileindex_path(&fileindex_path, worktree);
5945 if (err)
5946 return err;
5948 cmp_arg.progress_cb = progress_cb;
5949 cmp_arg.progress_arg = progress_arg;
5950 cmp_arg.merged_paths = merged_paths;
5951 err = merge_files(worktree, fileindex, fileindex_path,
5952 parent_commit_id, commit_id, repo, collect_merged_paths,
5953 &cmp_arg, cancel_cb, cancel_arg);
5954 if (commit_ref)
5955 got_ref_close(commit_ref);
5956 return err;
5959 const struct got_error *
5960 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5961 struct got_worktree *worktree, struct got_fileindex *fileindex,
5962 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5963 struct got_repository *repo,
5964 got_worktree_checkout_cb progress_cb, void *progress_arg,
5965 got_cancel_cb cancel_cb, void *cancel_arg)
5967 const struct got_error *err;
5968 char *commit_ref_name;
5970 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5971 if (err)
5972 return err;
5974 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5975 if (err)
5976 goto done;
5978 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5979 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5980 progress_arg, cancel_cb, cancel_arg);
5981 done:
5982 free(commit_ref_name);
5983 return err;
5986 const struct got_error *
5987 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5988 struct got_worktree *worktree, struct got_fileindex *fileindex,
5989 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5990 struct got_repository *repo,
5991 got_worktree_checkout_cb progress_cb, void *progress_arg,
5992 got_cancel_cb cancel_cb, void *cancel_arg)
5994 const struct got_error *err;
5995 char *commit_ref_name;
5997 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5998 if (err)
5999 return err;
6001 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6002 if (err)
6003 goto done;
6005 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6006 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6007 progress_arg, cancel_cb, cancel_arg);
6008 done:
6009 free(commit_ref_name);
6010 return err;
6013 static const struct got_error *
6014 rebase_commit(struct got_object_id **new_commit_id,
6015 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6016 struct got_worktree *worktree, struct got_fileindex *fileindex,
6017 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6018 const char *new_logmsg, struct got_repository *repo)
6020 const struct got_error *err, *sync_err;
6021 struct got_pathlist_head commitable_paths;
6022 struct collect_commitables_arg cc_arg;
6023 char *fileindex_path = NULL;
6024 struct got_reference *head_ref = NULL;
6025 struct got_object_id *head_commit_id = NULL;
6026 char *logmsg = NULL;
6028 TAILQ_INIT(&commitable_paths);
6029 *new_commit_id = NULL;
6031 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6033 err = get_fileindex_path(&fileindex_path, worktree);
6034 if (err)
6035 return err;
6037 cc_arg.commitable_paths = &commitable_paths;
6038 cc_arg.worktree = worktree;
6039 cc_arg.repo = repo;
6040 cc_arg.have_staged_files = 0;
6042 * If possible get the status of individual files directly to
6043 * avoid crawling the entire work tree once per rebased commit.
6044 * TODO: Ideally, merged_paths would contain a list of commitables
6045 * we could use so we could skip worktree_status() entirely.
6047 if (merged_paths) {
6048 struct got_pathlist_entry *pe;
6049 TAILQ_FOREACH(pe, merged_paths, entry) {
6050 err = worktree_status(worktree, pe->path, fileindex,
6051 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6052 0);
6053 if (err)
6054 goto done;
6056 } else {
6057 err = worktree_status(worktree, "", fileindex, repo,
6058 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6059 if (err)
6060 goto done;
6063 if (TAILQ_EMPTY(&commitable_paths)) {
6064 /* No-op change; commit will be elided. */
6065 err = got_ref_delete(commit_ref, repo);
6066 if (err)
6067 goto done;
6068 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6069 goto done;
6072 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6073 if (err)
6074 goto done;
6076 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6077 if (err)
6078 goto done;
6080 if (new_logmsg) {
6081 logmsg = strdup(new_logmsg);
6082 if (logmsg == NULL) {
6083 err = got_error_from_errno("strdup");
6084 goto done;
6086 } else {
6087 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6088 if (err)
6089 goto done;
6092 /* NB: commit_worktree will call free(logmsg) */
6093 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6094 worktree, got_object_commit_get_author(orig_commit),
6095 got_object_commit_get_committer(orig_commit),
6096 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6097 if (err)
6098 goto done;
6100 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6101 if (err)
6102 goto done;
6104 err = got_ref_delete(commit_ref, repo);
6105 if (err)
6106 goto done;
6108 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6109 fileindex, 0);
6110 sync_err = sync_fileindex(fileindex, fileindex_path);
6111 if (sync_err && err == NULL)
6112 err = sync_err;
6113 done:
6114 free(fileindex_path);
6115 free(head_commit_id);
6116 if (head_ref)
6117 got_ref_close(head_ref);
6118 if (err) {
6119 free(*new_commit_id);
6120 *new_commit_id = NULL;
6122 return err;
6125 const struct got_error *
6126 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6127 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6128 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6129 struct got_commit_object *orig_commit,
6130 struct got_object_id *orig_commit_id, struct got_repository *repo)
6132 const struct got_error *err;
6133 char *commit_ref_name;
6134 struct got_reference *commit_ref = NULL;
6135 struct got_object_id *commit_id = NULL;
6137 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6138 if (err)
6139 return err;
6141 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6142 if (err)
6143 goto done;
6144 err = got_ref_resolve(&commit_id, repo, commit_ref);
6145 if (err)
6146 goto done;
6147 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6148 err = got_error(GOT_ERR_REBASE_COMMITID);
6149 goto done;
6152 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6153 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6154 done:
6155 if (commit_ref)
6156 got_ref_close(commit_ref);
6157 free(commit_ref_name);
6158 free(commit_id);
6159 return err;
6162 const struct got_error *
6163 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6164 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6165 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6166 struct got_commit_object *orig_commit,
6167 struct got_object_id *orig_commit_id, const char *new_logmsg,
6168 struct got_repository *repo)
6170 const struct got_error *err;
6171 char *commit_ref_name;
6172 struct got_reference *commit_ref = NULL;
6174 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6175 if (err)
6176 return err;
6178 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6179 if (err)
6180 goto done;
6182 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6183 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6184 done:
6185 if (commit_ref)
6186 got_ref_close(commit_ref);
6187 free(commit_ref_name);
6188 return err;
6191 const struct got_error *
6192 got_worktree_rebase_postpone(struct got_worktree *worktree,
6193 struct got_fileindex *fileindex)
6195 if (fileindex)
6196 got_fileindex_free(fileindex);
6197 return lock_worktree(worktree, LOCK_SH);
6200 static const struct got_error *
6201 delete_ref(const char *name, struct got_repository *repo)
6203 const struct got_error *err;
6204 struct got_reference *ref;
6206 err = got_ref_open(&ref, repo, name, 0);
6207 if (err) {
6208 if (err->code == GOT_ERR_NOT_REF)
6209 return NULL;
6210 return err;
6213 err = got_ref_delete(ref, repo);
6214 got_ref_close(ref);
6215 return err;
6218 static const struct got_error *
6219 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6221 const struct got_error *err;
6222 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6223 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6225 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6226 if (err)
6227 goto done;
6228 err = delete_ref(tmp_branch_name, repo);
6229 if (err)
6230 goto done;
6232 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6233 if (err)
6234 goto done;
6235 err = delete_ref(new_base_branch_ref_name, repo);
6236 if (err)
6237 goto done;
6239 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6240 if (err)
6241 goto done;
6242 err = delete_ref(branch_ref_name, repo);
6243 if (err)
6244 goto done;
6246 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6247 if (err)
6248 goto done;
6249 err = delete_ref(commit_ref_name, repo);
6250 if (err)
6251 goto done;
6253 done:
6254 free(tmp_branch_name);
6255 free(new_base_branch_ref_name);
6256 free(branch_ref_name);
6257 free(commit_ref_name);
6258 return err;
6261 const struct got_error *
6262 got_worktree_rebase_complete(struct got_worktree *worktree,
6263 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6264 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6265 struct got_repository *repo)
6267 const struct got_error *err, *unlockerr;
6268 struct got_object_id *new_head_commit_id = NULL;
6270 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6271 if (err)
6272 return err;
6274 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6275 if (err)
6276 goto done;
6278 err = got_ref_write(rebased_branch, repo);
6279 if (err)
6280 goto done;
6282 err = got_worktree_set_head_ref(worktree, rebased_branch);
6283 if (err)
6284 goto done;
6286 err = delete_rebase_refs(worktree, repo);
6287 done:
6288 if (fileindex)
6289 got_fileindex_free(fileindex);
6290 free(new_head_commit_id);
6291 unlockerr = lock_worktree(worktree, LOCK_SH);
6292 if (unlockerr && err == NULL)
6293 err = unlockerr;
6294 return err;
6297 const struct got_error *
6298 got_worktree_rebase_abort(struct got_worktree *worktree,
6299 struct got_fileindex *fileindex, struct got_repository *repo,
6300 struct got_reference *new_base_branch,
6301 got_worktree_checkout_cb progress_cb, void *progress_arg)
6303 const struct got_error *err, *unlockerr, *sync_err;
6304 struct got_reference *resolved = NULL;
6305 struct got_object_id *commit_id = NULL;
6306 char *fileindex_path = NULL;
6307 struct revert_file_args rfa;
6308 struct got_object_id *tree_id = NULL;
6310 err = lock_worktree(worktree, LOCK_EX);
6311 if (err)
6312 return err;
6314 err = got_ref_open(&resolved, repo,
6315 got_ref_get_symref_target(new_base_branch), 0);
6316 if (err)
6317 goto done;
6319 err = got_worktree_set_head_ref(worktree, resolved);
6320 if (err)
6321 goto done;
6324 * XXX commits to the base branch could have happened while
6325 * we were busy rebasing; should we store the original commit ID
6326 * when rebase begins and read it back here?
6328 err = got_ref_resolve(&commit_id, repo, resolved);
6329 if (err)
6330 goto done;
6332 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6333 if (err)
6334 goto done;
6336 err = got_object_id_by_path(&tree_id, repo,
6337 worktree->base_commit_id, worktree->path_prefix);
6338 if (err)
6339 goto done;
6341 err = delete_rebase_refs(worktree, repo);
6342 if (err)
6343 goto done;
6345 err = get_fileindex_path(&fileindex_path, worktree);
6346 if (err)
6347 goto done;
6349 rfa.worktree = worktree;
6350 rfa.fileindex = fileindex;
6351 rfa.progress_cb = progress_cb;
6352 rfa.progress_arg = progress_arg;
6353 rfa.patch_cb = NULL;
6354 rfa.patch_arg = NULL;
6355 rfa.repo = repo;
6356 err = worktree_status(worktree, "", fileindex, repo,
6357 revert_file, &rfa, NULL, NULL, 0, 0);
6358 if (err)
6359 goto sync;
6361 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6362 repo, progress_cb, progress_arg, NULL, NULL);
6363 sync:
6364 sync_err = sync_fileindex(fileindex, fileindex_path);
6365 if (sync_err && err == NULL)
6366 err = sync_err;
6367 done:
6368 got_ref_close(resolved);
6369 free(tree_id);
6370 free(commit_id);
6371 if (fileindex)
6372 got_fileindex_free(fileindex);
6373 free(fileindex_path);
6375 unlockerr = lock_worktree(worktree, LOCK_SH);
6376 if (unlockerr && err == NULL)
6377 err = unlockerr;
6378 return err;
6381 const struct got_error *
6382 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6383 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6384 struct got_fileindex **fileindex, struct got_worktree *worktree,
6385 struct got_repository *repo)
6387 const struct got_error *err = NULL;
6388 char *tmp_branch_name = NULL;
6389 char *branch_ref_name = NULL;
6390 char *base_commit_ref_name = NULL;
6391 char *fileindex_path = NULL;
6392 struct check_rebase_ok_arg ok_arg;
6393 struct got_reference *wt_branch = NULL;
6394 struct got_reference *base_commit_ref = NULL;
6396 *tmp_branch = NULL;
6397 *branch_ref = NULL;
6398 *base_commit_id = NULL;
6399 *fileindex = NULL;
6401 err = lock_worktree(worktree, LOCK_EX);
6402 if (err)
6403 return err;
6405 err = open_fileindex(fileindex, &fileindex_path, worktree);
6406 if (err)
6407 goto done;
6409 ok_arg.worktree = worktree;
6410 ok_arg.repo = repo;
6411 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6412 &ok_arg);
6413 if (err)
6414 goto done;
6416 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6417 if (err)
6418 goto done;
6420 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6421 if (err)
6422 goto done;
6424 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6425 worktree);
6426 if (err)
6427 goto done;
6429 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6430 0);
6431 if (err)
6432 goto done;
6434 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6435 if (err)
6436 goto done;
6438 err = got_ref_write(*branch_ref, repo);
6439 if (err)
6440 goto done;
6442 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6443 worktree->base_commit_id);
6444 if (err)
6445 goto done;
6446 err = got_ref_write(base_commit_ref, repo);
6447 if (err)
6448 goto done;
6449 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6450 if (*base_commit_id == NULL) {
6451 err = got_error_from_errno("got_object_id_dup");
6452 goto done;
6455 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6456 worktree->base_commit_id);
6457 if (err)
6458 goto done;
6459 err = got_ref_write(*tmp_branch, repo);
6460 if (err)
6461 goto done;
6463 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6464 if (err)
6465 goto done;
6466 done:
6467 free(fileindex_path);
6468 free(tmp_branch_name);
6469 free(branch_ref_name);
6470 free(base_commit_ref_name);
6471 if (wt_branch)
6472 got_ref_close(wt_branch);
6473 if (err) {
6474 if (*branch_ref) {
6475 got_ref_close(*branch_ref);
6476 *branch_ref = NULL;
6478 if (*tmp_branch) {
6479 got_ref_close(*tmp_branch);
6480 *tmp_branch = NULL;
6482 free(*base_commit_id);
6483 if (*fileindex) {
6484 got_fileindex_free(*fileindex);
6485 *fileindex = NULL;
6487 lock_worktree(worktree, LOCK_SH);
6489 return err;
6492 const struct got_error *
6493 got_worktree_histedit_postpone(struct got_worktree *worktree,
6494 struct got_fileindex *fileindex)
6496 if (fileindex)
6497 got_fileindex_free(fileindex);
6498 return lock_worktree(worktree, LOCK_SH);
6501 const struct got_error *
6502 got_worktree_histedit_in_progress(int *in_progress,
6503 struct got_worktree *worktree)
6505 const struct got_error *err;
6506 char *tmp_branch_name = NULL;
6508 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6509 if (err)
6510 return err;
6512 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6513 free(tmp_branch_name);
6514 return NULL;
6517 const struct got_error *
6518 got_worktree_histedit_continue(struct got_object_id **commit_id,
6519 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6520 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6521 struct got_worktree *worktree, struct got_repository *repo)
6523 const struct got_error *err;
6524 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6525 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6526 struct got_reference *commit_ref = NULL;
6527 struct got_reference *base_commit_ref = NULL;
6528 char *fileindex_path = NULL;
6529 int have_staged_files = 0;
6531 *commit_id = NULL;
6532 *tmp_branch = NULL;
6533 *base_commit_id = NULL;
6534 *fileindex = NULL;
6536 err = lock_worktree(worktree, LOCK_EX);
6537 if (err)
6538 return err;
6540 err = open_fileindex(fileindex, &fileindex_path, worktree);
6541 if (err)
6542 goto done;
6544 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6545 &have_staged_files);
6546 if (err && err->code != GOT_ERR_CANCELLED)
6547 goto done;
6548 if (have_staged_files) {
6549 err = got_error(GOT_ERR_STAGED_PATHS);
6550 goto done;
6553 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6554 if (err)
6555 goto done;
6557 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6558 if (err)
6559 goto done;
6561 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6562 if (err)
6563 goto done;
6565 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6566 worktree);
6567 if (err)
6568 goto done;
6570 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6571 if (err)
6572 goto done;
6574 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6575 if (err)
6576 goto done;
6577 err = got_ref_resolve(commit_id, repo, commit_ref);
6578 if (err)
6579 goto done;
6581 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6582 if (err)
6583 goto done;
6584 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6585 if (err)
6586 goto done;
6588 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6589 if (err)
6590 goto done;
6591 done:
6592 free(commit_ref_name);
6593 free(branch_ref_name);
6594 free(fileindex_path);
6595 if (commit_ref)
6596 got_ref_close(commit_ref);
6597 if (base_commit_ref)
6598 got_ref_close(base_commit_ref);
6599 if (err) {
6600 free(*commit_id);
6601 *commit_id = NULL;
6602 free(*base_commit_id);
6603 *base_commit_id = NULL;
6604 if (*tmp_branch) {
6605 got_ref_close(*tmp_branch);
6606 *tmp_branch = NULL;
6608 if (*fileindex) {
6609 got_fileindex_free(*fileindex);
6610 *fileindex = NULL;
6612 lock_worktree(worktree, LOCK_EX);
6614 return err;
6617 static const struct got_error *
6618 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6620 const struct got_error *err;
6621 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6622 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6624 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6625 if (err)
6626 goto done;
6627 err = delete_ref(tmp_branch_name, repo);
6628 if (err)
6629 goto done;
6631 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6632 worktree);
6633 if (err)
6634 goto done;
6635 err = delete_ref(base_commit_ref_name, repo);
6636 if (err)
6637 goto done;
6639 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6640 if (err)
6641 goto done;
6642 err = delete_ref(branch_ref_name, repo);
6643 if (err)
6644 goto done;
6646 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6647 if (err)
6648 goto done;
6649 err = delete_ref(commit_ref_name, repo);
6650 if (err)
6651 goto done;
6652 done:
6653 free(tmp_branch_name);
6654 free(base_commit_ref_name);
6655 free(branch_ref_name);
6656 free(commit_ref_name);
6657 return err;
6660 const struct got_error *
6661 got_worktree_histedit_abort(struct got_worktree *worktree,
6662 struct got_fileindex *fileindex, struct got_repository *repo,
6663 struct got_reference *branch, struct got_object_id *base_commit_id,
6664 got_worktree_checkout_cb progress_cb, void *progress_arg)
6666 const struct got_error *err, *unlockerr, *sync_err;
6667 struct got_reference *resolved = NULL;
6668 char *fileindex_path = NULL;
6669 struct got_object_id *tree_id = NULL;
6670 struct revert_file_args rfa;
6672 err = lock_worktree(worktree, LOCK_EX);
6673 if (err)
6674 return err;
6676 err = got_ref_open(&resolved, repo,
6677 got_ref_get_symref_target(branch), 0);
6678 if (err)
6679 goto done;
6681 err = got_worktree_set_head_ref(worktree, resolved);
6682 if (err)
6683 goto done;
6685 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6686 if (err)
6687 goto done;
6689 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6690 worktree->path_prefix);
6691 if (err)
6692 goto done;
6694 err = delete_histedit_refs(worktree, repo);
6695 if (err)
6696 goto done;
6698 err = get_fileindex_path(&fileindex_path, worktree);
6699 if (err)
6700 goto done;
6702 rfa.worktree = worktree;
6703 rfa.fileindex = fileindex;
6704 rfa.progress_cb = progress_cb;
6705 rfa.progress_arg = progress_arg;
6706 rfa.patch_cb = NULL;
6707 rfa.patch_arg = NULL;
6708 rfa.repo = repo;
6709 err = worktree_status(worktree, "", fileindex, repo,
6710 revert_file, &rfa, NULL, NULL, 0, 0);
6711 if (err)
6712 goto sync;
6714 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6715 repo, progress_cb, progress_arg, NULL, NULL);
6716 sync:
6717 sync_err = sync_fileindex(fileindex, fileindex_path);
6718 if (sync_err && err == NULL)
6719 err = sync_err;
6720 done:
6721 got_ref_close(resolved);
6722 free(tree_id);
6723 free(fileindex_path);
6725 unlockerr = lock_worktree(worktree, LOCK_SH);
6726 if (unlockerr && err == NULL)
6727 err = unlockerr;
6728 return err;
6731 const struct got_error *
6732 got_worktree_histedit_complete(struct got_worktree *worktree,
6733 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6734 struct got_reference *edited_branch, struct got_repository *repo)
6736 const struct got_error *err, *unlockerr;
6737 struct got_object_id *new_head_commit_id = NULL;
6738 struct got_reference *resolved = NULL;
6740 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6741 if (err)
6742 return err;
6744 err = got_ref_open(&resolved, repo,
6745 got_ref_get_symref_target(edited_branch), 0);
6746 if (err)
6747 goto done;
6749 err = got_ref_change_ref(resolved, new_head_commit_id);
6750 if (err)
6751 goto done;
6753 err = got_ref_write(resolved, repo);
6754 if (err)
6755 goto done;
6757 err = got_worktree_set_head_ref(worktree, resolved);
6758 if (err)
6759 goto done;
6761 err = delete_histedit_refs(worktree, repo);
6762 done:
6763 if (fileindex)
6764 got_fileindex_free(fileindex);
6765 free(new_head_commit_id);
6766 unlockerr = lock_worktree(worktree, LOCK_SH);
6767 if (unlockerr && err == NULL)
6768 err = unlockerr;
6769 return err;
6772 const struct got_error *
6773 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6774 struct got_object_id *commit_id, struct got_repository *repo)
6776 const struct got_error *err;
6777 char *commit_ref_name;
6779 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6780 if (err)
6781 return err;
6783 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6784 if (err)
6785 goto done;
6787 err = delete_ref(commit_ref_name, repo);
6788 done:
6789 free(commit_ref_name);
6790 return err;
6793 const struct got_error *
6794 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6795 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6796 struct got_worktree *worktree, const char *refname,
6797 struct got_repository *repo)
6799 const struct got_error *err = NULL;
6800 char *fileindex_path = NULL;
6801 struct check_rebase_ok_arg ok_arg;
6803 *fileindex = NULL;
6804 *branch_ref = NULL;
6805 *base_branch_ref = NULL;
6807 err = lock_worktree(worktree, LOCK_EX);
6808 if (err)
6809 return err;
6811 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6812 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6813 "cannot integrate a branch into itself; "
6814 "update -b or different branch name required");
6815 goto done;
6818 err = open_fileindex(fileindex, &fileindex_path, worktree);
6819 if (err)
6820 goto done;
6822 /* Preconditions are the same as for rebase. */
6823 ok_arg.worktree = worktree;
6824 ok_arg.repo = repo;
6825 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6826 &ok_arg);
6827 if (err)
6828 goto done;
6830 err = got_ref_open(branch_ref, repo, refname, 1);
6831 if (err)
6832 goto done;
6834 err = got_ref_open(base_branch_ref, repo,
6835 got_worktree_get_head_ref_name(worktree), 1);
6836 done:
6837 if (err) {
6838 if (*branch_ref) {
6839 got_ref_close(*branch_ref);
6840 *branch_ref = NULL;
6842 if (*base_branch_ref) {
6843 got_ref_close(*base_branch_ref);
6844 *base_branch_ref = NULL;
6846 if (*fileindex) {
6847 got_fileindex_free(*fileindex);
6848 *fileindex = NULL;
6850 lock_worktree(worktree, LOCK_SH);
6852 return err;
6855 const struct got_error *
6856 got_worktree_integrate_continue(struct got_worktree *worktree,
6857 struct got_fileindex *fileindex, struct got_repository *repo,
6858 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6859 got_worktree_checkout_cb progress_cb, void *progress_arg,
6860 got_cancel_cb cancel_cb, void *cancel_arg)
6862 const struct got_error *err = NULL, *sync_err, *unlockerr;
6863 char *fileindex_path = NULL;
6864 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6866 err = get_fileindex_path(&fileindex_path, worktree);
6867 if (err)
6868 goto done;
6870 err = got_ref_resolve(&commit_id, repo, branch_ref);
6871 if (err)
6872 goto done;
6874 err = got_object_id_by_path(&tree_id, repo, commit_id,
6875 worktree->path_prefix);
6876 if (err)
6877 goto done;
6879 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6880 if (err)
6881 goto done;
6883 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6884 progress_cb, progress_arg, cancel_cb, cancel_arg);
6885 if (err)
6886 goto sync;
6888 err = got_ref_change_ref(base_branch_ref, commit_id);
6889 if (err)
6890 goto sync;
6892 err = got_ref_write(base_branch_ref, repo);
6893 sync:
6894 sync_err = sync_fileindex(fileindex, fileindex_path);
6895 if (sync_err && err == NULL)
6896 err = sync_err;
6898 done:
6899 unlockerr = got_ref_unlock(branch_ref);
6900 if (unlockerr && err == NULL)
6901 err = unlockerr;
6902 got_ref_close(branch_ref);
6904 unlockerr = got_ref_unlock(base_branch_ref);
6905 if (unlockerr && err == NULL)
6906 err = unlockerr;
6907 got_ref_close(base_branch_ref);
6909 got_fileindex_free(fileindex);
6910 free(fileindex_path);
6911 free(tree_id);
6913 unlockerr = lock_worktree(worktree, LOCK_SH);
6914 if (unlockerr && err == NULL)
6915 err = unlockerr;
6916 return err;
6919 const struct got_error *
6920 got_worktree_integrate_abort(struct got_worktree *worktree,
6921 struct got_fileindex *fileindex, struct got_repository *repo,
6922 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6924 const struct got_error *err = NULL, *unlockerr = NULL;
6926 got_fileindex_free(fileindex);
6928 err = lock_worktree(worktree, LOCK_SH);
6930 unlockerr = got_ref_unlock(branch_ref);
6931 if (unlockerr && err == NULL)
6932 err = unlockerr;
6933 got_ref_close(branch_ref);
6935 unlockerr = got_ref_unlock(base_branch_ref);
6936 if (unlockerr && err == NULL)
6937 err = unlockerr;
6938 got_ref_close(base_branch_ref);
6940 return err;
6943 struct check_stage_ok_arg {
6944 struct got_object_id *head_commit_id;
6945 struct got_worktree *worktree;
6946 struct got_fileindex *fileindex;
6947 struct got_repository *repo;
6948 int have_changes;
6951 const struct got_error *
6952 check_stage_ok(void *arg, unsigned char status,
6953 unsigned char staged_status, const char *relpath,
6954 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6955 struct got_object_id *commit_id, int dirfd, const char *de_name)
6957 struct check_stage_ok_arg *a = arg;
6958 const struct got_error *err = NULL;
6959 struct got_fileindex_entry *ie;
6960 struct got_object_id base_commit_id;
6961 struct got_object_id *base_commit_idp = NULL;
6962 char *in_repo_path = NULL, *p;
6964 if (status == GOT_STATUS_UNVERSIONED ||
6965 status == GOT_STATUS_NO_CHANGE)
6966 return NULL;
6967 if (status == GOT_STATUS_NONEXISTENT)
6968 return got_error_set_errno(ENOENT, relpath);
6970 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6971 if (ie == NULL)
6972 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6974 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6975 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6976 relpath) == -1)
6977 return got_error_from_errno("asprintf");
6979 if (got_fileindex_entry_has_commit(ie)) {
6980 memcpy(base_commit_id.sha1, ie->commit_sha1,
6981 SHA1_DIGEST_LENGTH);
6982 base_commit_idp = &base_commit_id;
6985 if (status == GOT_STATUS_CONFLICT) {
6986 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6987 goto done;
6988 } else if (status != GOT_STATUS_ADD &&
6989 status != GOT_STATUS_MODIFY &&
6990 status != GOT_STATUS_DELETE) {
6991 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6992 goto done;
6995 a->have_changes = 1;
6997 p = in_repo_path;
6998 while (p[0] == '/')
6999 p++;
7000 err = check_out_of_date(p, status, staged_status,
7001 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7002 GOT_ERR_STAGE_OUT_OF_DATE);
7003 done:
7004 free(in_repo_path);
7005 return err;
7008 struct stage_path_arg {
7009 struct got_worktree *worktree;
7010 struct got_fileindex *fileindex;
7011 struct got_repository *repo;
7012 got_worktree_status_cb status_cb;
7013 void *status_arg;
7014 got_worktree_patch_cb patch_cb;
7015 void *patch_arg;
7016 int staged_something;
7017 int allow_bad_symlinks;
7020 static const struct got_error *
7021 stage_path(void *arg, unsigned char status,
7022 unsigned char staged_status, const char *relpath,
7023 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7024 struct got_object_id *commit_id, int dirfd, const char *de_name)
7026 struct stage_path_arg *a = arg;
7027 const struct got_error *err = NULL;
7028 struct got_fileindex_entry *ie;
7029 char *ondisk_path = NULL, *path_content = NULL;
7030 uint32_t stage;
7031 struct got_object_id *new_staged_blob_id = NULL;
7032 struct stat sb;
7034 if (status == GOT_STATUS_UNVERSIONED)
7035 return NULL;
7037 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7038 if (ie == NULL)
7039 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7041 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7042 relpath)== -1)
7043 return got_error_from_errno("asprintf");
7045 switch (status) {
7046 case GOT_STATUS_ADD:
7047 case GOT_STATUS_MODIFY:
7048 /* XXX could sb.st_mode be passed in by our caller? */
7049 if (lstat(ondisk_path, &sb) == -1) {
7050 err = got_error_from_errno2("lstat", ondisk_path);
7051 break;
7053 if (a->patch_cb) {
7054 if (status == GOT_STATUS_ADD) {
7055 int choice = GOT_PATCH_CHOICE_NONE;
7056 err = (*a->patch_cb)(&choice, a->patch_arg,
7057 status, ie->path, NULL, 1, 1);
7058 if (err)
7059 break;
7060 if (choice != GOT_PATCH_CHOICE_YES)
7061 break;
7062 } else {
7063 err = create_patched_content(&path_content, 0,
7064 staged_blob_id ? staged_blob_id : blob_id,
7065 ondisk_path, dirfd, de_name, ie->path,
7066 a->repo, a->patch_cb, a->patch_arg);
7067 if (err || path_content == NULL)
7068 break;
7071 err = got_object_blob_create(&new_staged_blob_id,
7072 path_content ? path_content : ondisk_path, a->repo);
7073 if (err)
7074 break;
7075 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7076 SHA1_DIGEST_LENGTH);
7077 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7078 stage = GOT_FILEIDX_STAGE_ADD;
7079 else
7080 stage = GOT_FILEIDX_STAGE_MODIFY;
7081 got_fileindex_entry_stage_set(ie, stage);
7082 if (S_ISLNK(sb.st_mode)) {
7083 int is_bad_symlink = 0;
7084 if (!a->allow_bad_symlinks) {
7085 char target_path[PATH_MAX];
7086 ssize_t target_len;
7087 target_len = readlink(ondisk_path, target_path,
7088 sizeof(target_path));
7089 if (target_len == -1) {
7090 err = got_error_from_errno2("readlink",
7091 ondisk_path);
7092 break;
7094 err = is_bad_symlink_target(&is_bad_symlink,
7095 target_path, target_len, ondisk_path,
7096 a->worktree->root_path);
7097 if (err)
7098 break;
7099 if (is_bad_symlink) {
7100 err = got_error_path(ondisk_path,
7101 GOT_ERR_BAD_SYMLINK);
7102 break;
7105 if (is_bad_symlink)
7106 got_fileindex_entry_staged_filetype_set(ie,
7107 GOT_FILEIDX_MODE_BAD_SYMLINK);
7108 else
7109 got_fileindex_entry_staged_filetype_set(ie,
7110 GOT_FILEIDX_MODE_SYMLINK);
7111 } else {
7112 got_fileindex_entry_staged_filetype_set(ie,
7113 GOT_FILEIDX_MODE_REGULAR_FILE);
7115 a->staged_something = 1;
7116 if (a->status_cb == NULL)
7117 break;
7118 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7119 get_staged_status(ie), relpath, blob_id,
7120 new_staged_blob_id, NULL, dirfd, de_name);
7121 break;
7122 case GOT_STATUS_DELETE:
7123 if (staged_status == GOT_STATUS_DELETE)
7124 break;
7125 if (a->patch_cb) {
7126 int choice = GOT_PATCH_CHOICE_NONE;
7127 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7128 ie->path, NULL, 1, 1);
7129 if (err)
7130 break;
7131 if (choice == GOT_PATCH_CHOICE_NO)
7132 break;
7133 if (choice != GOT_PATCH_CHOICE_YES) {
7134 err = got_error(GOT_ERR_PATCH_CHOICE);
7135 break;
7138 stage = GOT_FILEIDX_STAGE_DELETE;
7139 got_fileindex_entry_stage_set(ie, stage);
7140 a->staged_something = 1;
7141 if (a->status_cb == NULL)
7142 break;
7143 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7144 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7145 de_name);
7146 break;
7147 case GOT_STATUS_NO_CHANGE:
7148 break;
7149 case GOT_STATUS_CONFLICT:
7150 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7151 break;
7152 case GOT_STATUS_NONEXISTENT:
7153 err = got_error_set_errno(ENOENT, relpath);
7154 break;
7155 default:
7156 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7157 break;
7160 if (path_content && unlink(path_content) == -1 && err == NULL)
7161 err = got_error_from_errno2("unlink", path_content);
7162 free(path_content);
7163 free(ondisk_path);
7164 free(new_staged_blob_id);
7165 return err;
7168 const struct got_error *
7169 got_worktree_stage(struct got_worktree *worktree,
7170 struct got_pathlist_head *paths,
7171 got_worktree_status_cb status_cb, void *status_arg,
7172 got_worktree_patch_cb patch_cb, void *patch_arg,
7173 int allow_bad_symlinks, struct got_repository *repo)
7175 const struct got_error *err = NULL, *sync_err, *unlockerr;
7176 struct got_pathlist_entry *pe;
7177 struct got_fileindex *fileindex = NULL;
7178 char *fileindex_path = NULL;
7179 struct got_reference *head_ref = NULL;
7180 struct got_object_id *head_commit_id = NULL;
7181 struct check_stage_ok_arg oka;
7182 struct stage_path_arg spa;
7184 err = lock_worktree(worktree, LOCK_EX);
7185 if (err)
7186 return err;
7188 err = got_ref_open(&head_ref, repo,
7189 got_worktree_get_head_ref_name(worktree), 0);
7190 if (err)
7191 goto done;
7192 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7193 if (err)
7194 goto done;
7195 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7196 if (err)
7197 goto done;
7199 /* Check pre-conditions before staging anything. */
7200 oka.head_commit_id = head_commit_id;
7201 oka.worktree = worktree;
7202 oka.fileindex = fileindex;
7203 oka.repo = repo;
7204 oka.have_changes = 0;
7205 TAILQ_FOREACH(pe, paths, entry) {
7206 err = worktree_status(worktree, pe->path, fileindex, repo,
7207 check_stage_ok, &oka, NULL, NULL, 0, 0);
7208 if (err)
7209 goto done;
7211 if (!oka.have_changes) {
7212 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7213 goto done;
7216 spa.worktree = worktree;
7217 spa.fileindex = fileindex;
7218 spa.repo = repo;
7219 spa.patch_cb = patch_cb;
7220 spa.patch_arg = patch_arg;
7221 spa.status_cb = status_cb;
7222 spa.status_arg = status_arg;
7223 spa.staged_something = 0;
7224 spa.allow_bad_symlinks = allow_bad_symlinks;
7225 TAILQ_FOREACH(pe, paths, entry) {
7226 err = worktree_status(worktree, pe->path, fileindex, repo,
7227 stage_path, &spa, NULL, NULL, 0, 0);
7228 if (err)
7229 goto done;
7231 if (!spa.staged_something) {
7232 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7233 goto done;
7236 sync_err = sync_fileindex(fileindex, fileindex_path);
7237 if (sync_err && err == NULL)
7238 err = sync_err;
7239 done:
7240 if (head_ref)
7241 got_ref_close(head_ref);
7242 free(head_commit_id);
7243 free(fileindex_path);
7244 if (fileindex)
7245 got_fileindex_free(fileindex);
7246 unlockerr = lock_worktree(worktree, LOCK_SH);
7247 if (unlockerr && err == NULL)
7248 err = unlockerr;
7249 return err;
7252 struct unstage_path_arg {
7253 struct got_worktree *worktree;
7254 struct got_fileindex *fileindex;
7255 struct got_repository *repo;
7256 got_worktree_checkout_cb progress_cb;
7257 void *progress_arg;
7258 got_worktree_patch_cb patch_cb;
7259 void *patch_arg;
7262 static const struct got_error *
7263 create_unstaged_content(char **path_unstaged_content,
7264 char **path_new_staged_content, struct got_object_id *blob_id,
7265 struct got_object_id *staged_blob_id, const char *relpath,
7266 struct got_repository *repo,
7267 got_worktree_patch_cb patch_cb, void *patch_arg)
7269 const struct got_error *err;
7270 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7271 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7272 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7273 struct stat sb1, sb2;
7274 struct got_diff_changes *changes = NULL;
7275 struct got_diff_state *ds = NULL;
7276 struct got_diff_args *args = NULL;
7277 struct got_diff_change *change;
7278 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7279 int have_content = 0, have_rejected_content = 0;
7281 *path_unstaged_content = NULL;
7282 *path_new_staged_content = NULL;
7284 err = got_object_id_str(&label1, blob_id);
7285 if (err)
7286 return err;
7287 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7288 if (err)
7289 goto done;
7291 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7292 if (err)
7293 goto done;
7295 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7296 if (err)
7297 goto done;
7299 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7300 if (err)
7301 goto done;
7303 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7304 if (err)
7305 goto done;
7307 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7308 if (err)
7309 goto done;
7311 if (stat(path1, &sb1) == -1) {
7312 err = got_error_from_errno2("stat", path1);
7313 goto done;
7316 if (stat(path2, &sb2) == -1) {
7317 err = got_error_from_errno2("stat", path2);
7318 goto done;
7321 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7322 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7323 if (err)
7324 goto done;
7326 err = got_opentemp_named(path_unstaged_content, &outfile,
7327 "got-unstaged-content");
7328 if (err)
7329 goto done;
7330 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7331 "got-new-staged-content");
7332 if (err)
7333 goto done;
7335 if (fseek(f1, 0L, SEEK_SET) == -1) {
7336 err = got_ferror(f1, GOT_ERR_IO);
7337 goto done;
7339 if (fseek(f2, 0L, SEEK_SET) == -1) {
7340 err = got_ferror(f2, GOT_ERR_IO);
7341 goto done;
7343 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7344 int choice;
7345 err = apply_or_reject_change(&choice, change, ++n,
7346 changes->nchanges, ds, args, diff_flags, relpath,
7347 f1, f2, &line_cur1, &line_cur2,
7348 outfile, rejectfile, patch_cb, patch_arg);
7349 if (err)
7350 goto done;
7351 if (choice == GOT_PATCH_CHOICE_YES)
7352 have_content = 1;
7353 else
7354 have_rejected_content = 1;
7355 if (choice == GOT_PATCH_CHOICE_QUIT)
7356 break;
7358 if (have_content || have_rejected_content)
7359 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7360 outfile, rejectfile);
7361 done:
7362 free(label1);
7363 if (blob)
7364 got_object_blob_close(blob);
7365 if (staged_blob)
7366 got_object_blob_close(staged_blob);
7367 if (f1 && fclose(f1) == EOF && err == NULL)
7368 err = got_error_from_errno2("fclose", path1);
7369 if (f2 && fclose(f2) == EOF && err == NULL)
7370 err = got_error_from_errno2("fclose", path2);
7371 if (outfile && fclose(outfile) == EOF && err == NULL)
7372 err = got_error_from_errno2("fclose", *path_unstaged_content);
7373 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7374 err = got_error_from_errno2("fclose", *path_new_staged_content);
7375 if (path1 && unlink(path1) == -1 && err == NULL)
7376 err = got_error_from_errno2("unlink", path1);
7377 if (path2 && unlink(path2) == -1 && err == NULL)
7378 err = got_error_from_errno2("unlink", path2);
7379 if (err || !have_content) {
7380 if (*path_unstaged_content &&
7381 unlink(*path_unstaged_content) == -1 && err == NULL)
7382 err = got_error_from_errno2("unlink",
7383 *path_unstaged_content);
7384 free(*path_unstaged_content);
7385 *path_unstaged_content = NULL;
7387 if (err || !have_content || !have_rejected_content) {
7388 if (*path_new_staged_content &&
7389 unlink(*path_new_staged_content) == -1 && err == NULL)
7390 err = got_error_from_errno2("unlink",
7391 *path_new_staged_content);
7392 free(*path_new_staged_content);
7393 *path_new_staged_content = NULL;
7395 free(args);
7396 if (ds) {
7397 got_diff_state_free(ds);
7398 free(ds);
7400 if (changes)
7401 got_diff_free_changes(changes);
7402 free(path1);
7403 free(path2);
7404 return err;
7407 static const struct got_error *
7408 unstage_hunks(struct got_object_id *staged_blob_id,
7409 struct got_blob_object *blob_base,
7410 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7411 const char *ondisk_path, const char *label_orig,
7412 struct got_worktree *worktree, struct got_repository *repo,
7413 got_worktree_patch_cb patch_cb, void *patch_arg,
7414 got_worktree_checkout_cb progress_cb, void *progress_arg)
7416 const struct got_error *err = NULL;
7417 char *path_unstaged_content = NULL;
7418 char *path_new_staged_content = NULL;
7419 struct got_object_id *new_staged_blob_id = NULL;
7420 FILE *f = NULL;
7421 struct stat sb;
7423 err = create_unstaged_content(&path_unstaged_content,
7424 &path_new_staged_content, blob_id, staged_blob_id,
7425 ie->path, repo, patch_cb, patch_arg);
7426 if (err)
7427 return err;
7429 if (path_unstaged_content == NULL)
7430 return NULL;
7432 if (path_new_staged_content) {
7433 err = got_object_blob_create(&new_staged_blob_id,
7434 path_new_staged_content, repo);
7435 if (err)
7436 goto done;
7439 f = fopen(path_unstaged_content, "r");
7440 if (f == NULL) {
7441 err = got_error_from_errno2("fopen",
7442 path_unstaged_content);
7443 goto done;
7445 if (fstat(fileno(f), &sb) == -1) {
7446 err = got_error_from_errno2("fstat", path_unstaged_content);
7447 goto done;
7449 if (got_fileindex_entry_staged_filetype_get(ie) ==
7450 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7451 char link_target[PATH_MAX];
7452 size_t r;
7453 r = fread(link_target, 1, sizeof(link_target), f);
7454 if (r == 0 && ferror(f)) {
7455 err = got_error_from_errno("fread");
7456 goto done;
7458 if (r >= sizeof(link_target)) { /* should not happen */
7459 err = got_error(GOT_ERR_NO_SPACE);
7460 goto done;
7462 link_target[r] = '\0';
7463 err = merge_symlink(worktree, blob_base,
7464 ondisk_path, ie->path, label_orig, link_target,
7465 worktree->base_commit_id, repo, progress_cb,
7466 progress_arg);
7467 } else {
7468 int local_changes_subsumed;
7469 err = merge_file(&local_changes_subsumed, worktree,
7470 blob_base, ondisk_path, ie->path,
7471 got_fileindex_perms_to_st(ie),
7472 path_unstaged_content, label_orig, "unstaged",
7473 repo, progress_cb, progress_arg);
7475 if (err)
7476 goto done;
7478 if (new_staged_blob_id) {
7479 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7480 SHA1_DIGEST_LENGTH);
7481 } else
7482 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7483 done:
7484 free(new_staged_blob_id);
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 if (f && fclose(f) != 0 && err == NULL)
7492 err = got_error_from_errno2("fclose", path_unstaged_content);
7493 free(path_unstaged_content);
7494 free(path_new_staged_content);
7495 return err;
7498 static const struct got_error *
7499 unstage_path(void *arg, unsigned char status,
7500 unsigned char staged_status, const char *relpath,
7501 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7502 struct got_object_id *commit_id, int dirfd, const char *de_name)
7504 const struct got_error *err = NULL;
7505 struct unstage_path_arg *a = arg;
7506 struct got_fileindex_entry *ie;
7507 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7508 char *ondisk_path = NULL;
7509 char *id_str = NULL, *label_orig = NULL;
7510 int local_changes_subsumed;
7511 struct stat sb;
7513 if (staged_status != GOT_STATUS_ADD &&
7514 staged_status != GOT_STATUS_MODIFY &&
7515 staged_status != GOT_STATUS_DELETE)
7516 return NULL;
7518 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7519 if (ie == NULL)
7520 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7522 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7523 == -1)
7524 return got_error_from_errno("asprintf");
7526 err = got_object_id_str(&id_str,
7527 commit_id ? commit_id : a->worktree->base_commit_id);
7528 if (err)
7529 goto done;
7530 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7531 id_str) == -1) {
7532 err = got_error_from_errno("asprintf");
7533 goto done;
7536 switch (staged_status) {
7537 case GOT_STATUS_MODIFY:
7538 err = got_object_open_as_blob(&blob_base, a->repo,
7539 blob_id, 8192);
7540 if (err)
7541 break;
7542 /* fall through */
7543 case GOT_STATUS_ADD:
7544 if (a->patch_cb) {
7545 if (staged_status == GOT_STATUS_ADD) {
7546 int choice = GOT_PATCH_CHOICE_NONE;
7547 err = (*a->patch_cb)(&choice, a->patch_arg,
7548 staged_status, ie->path, NULL, 1, 1);
7549 if (err)
7550 break;
7551 if (choice != GOT_PATCH_CHOICE_YES)
7552 break;
7553 } else {
7554 err = unstage_hunks(staged_blob_id,
7555 blob_base, blob_id, ie, ondisk_path,
7556 label_orig, a->worktree, a->repo,
7557 a->patch_cb, a->patch_arg,
7558 a->progress_cb, a->progress_arg);
7559 break; /* Done with this file. */
7562 err = got_object_open_as_blob(&blob_staged, a->repo,
7563 staged_blob_id, 8192);
7564 if (err)
7565 break;
7566 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7567 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7568 case GOT_FILEIDX_MODE_REGULAR_FILE:
7569 err = merge_blob(&local_changes_subsumed, a->worktree,
7570 blob_base, ondisk_path, relpath,
7571 got_fileindex_perms_to_st(ie), label_orig,
7572 blob_staged, commit_id ? commit_id :
7573 a->worktree->base_commit_id, a->repo,
7574 a->progress_cb, a->progress_arg);
7575 break;
7576 case GOT_FILEIDX_MODE_SYMLINK:
7577 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7578 char *staged_target;
7579 err = got_object_blob_read_to_str(
7580 &staged_target, blob_staged);
7581 if (err)
7582 goto done;
7583 err = merge_symlink(a->worktree, blob_base,
7584 ondisk_path, relpath, label_orig,
7585 staged_target, commit_id ? commit_id :
7586 a->worktree->base_commit_id,
7587 a->repo, a->progress_cb, a->progress_arg);
7588 free(staged_target);
7589 } else {
7590 err = merge_blob(&local_changes_subsumed,
7591 a->worktree, blob_base, ondisk_path,
7592 relpath, got_fileindex_perms_to_st(ie),
7593 label_orig, blob_staged,
7594 commit_id ? commit_id :
7595 a->worktree->base_commit_id, a->repo,
7596 a->progress_cb, a->progress_arg);
7598 break;
7599 default:
7600 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7601 break;
7603 if (err == NULL)
7604 got_fileindex_entry_stage_set(ie,
7605 GOT_FILEIDX_STAGE_NONE);
7606 break;
7607 case GOT_STATUS_DELETE:
7608 if (a->patch_cb) {
7609 int choice = GOT_PATCH_CHOICE_NONE;
7610 err = (*a->patch_cb)(&choice, a->patch_arg,
7611 staged_status, ie->path, NULL, 1, 1);
7612 if (err)
7613 break;
7614 if (choice == GOT_PATCH_CHOICE_NO)
7615 break;
7616 if (choice != GOT_PATCH_CHOICE_YES) {
7617 err = got_error(GOT_ERR_PATCH_CHOICE);
7618 break;
7621 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7622 err = get_file_status(&status, &sb, ie, ondisk_path,
7623 dirfd, de_name, a->repo);
7624 if (err)
7625 break;
7626 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7627 break;
7629 done:
7630 free(ondisk_path);
7631 if (blob_base)
7632 got_object_blob_close(blob_base);
7633 if (blob_staged)
7634 got_object_blob_close(blob_staged);
7635 free(id_str);
7636 free(label_orig);
7637 return err;
7640 const struct got_error *
7641 got_worktree_unstage(struct got_worktree *worktree,
7642 struct got_pathlist_head *paths,
7643 got_worktree_checkout_cb progress_cb, void *progress_arg,
7644 got_worktree_patch_cb patch_cb, void *patch_arg,
7645 struct got_repository *repo)
7647 const struct got_error *err = NULL, *sync_err, *unlockerr;
7648 struct got_pathlist_entry *pe;
7649 struct got_fileindex *fileindex = NULL;
7650 char *fileindex_path = NULL;
7651 struct unstage_path_arg upa;
7653 err = lock_worktree(worktree, LOCK_EX);
7654 if (err)
7655 return err;
7657 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7658 if (err)
7659 goto done;
7661 upa.worktree = worktree;
7662 upa.fileindex = fileindex;
7663 upa.repo = repo;
7664 upa.progress_cb = progress_cb;
7665 upa.progress_arg = progress_arg;
7666 upa.patch_cb = patch_cb;
7667 upa.patch_arg = patch_arg;
7668 TAILQ_FOREACH(pe, paths, entry) {
7669 err = worktree_status(worktree, pe->path, fileindex, repo,
7670 unstage_path, &upa, NULL, NULL, 0, 0);
7671 if (err)
7672 goto done;
7675 sync_err = sync_fileindex(fileindex, fileindex_path);
7676 if (sync_err && err == NULL)
7677 err = sync_err;
7678 done:
7679 free(fileindex_path);
7680 if (fileindex)
7681 got_fileindex_free(fileindex);
7682 unlockerr = lock_worktree(worktree, LOCK_SH);
7683 if (unlockerr && err == NULL)
7684 err = unlockerr;
7685 return err;
7688 struct report_file_info_arg {
7689 struct got_worktree *worktree;
7690 got_worktree_path_info_cb info_cb;
7691 void *info_arg;
7692 struct got_pathlist_head *paths;
7693 got_cancel_cb cancel_cb;
7694 void *cancel_arg;
7697 static const struct got_error *
7698 report_file_info(void *arg, struct got_fileindex_entry *ie)
7700 struct report_file_info_arg *a = arg;
7701 struct got_pathlist_entry *pe;
7702 struct got_object_id blob_id, staged_blob_id, commit_id;
7703 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7704 struct got_object_id *commit_idp = NULL;
7705 int stage;
7707 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7708 return got_error(GOT_ERR_CANCELLED);
7710 TAILQ_FOREACH(pe, a->paths, entry) {
7711 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7712 got_path_is_child(ie->path, pe->path, pe->path_len))
7713 break;
7715 if (pe == NULL) /* not found */
7716 return NULL;
7718 if (got_fileindex_entry_has_blob(ie)) {
7719 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7720 blob_idp = &blob_id;
7722 stage = got_fileindex_entry_stage_get(ie);
7723 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7724 stage == GOT_FILEIDX_STAGE_ADD) {
7725 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7726 SHA1_DIGEST_LENGTH);
7727 staged_blob_idp = &staged_blob_id;
7730 if (got_fileindex_entry_has_commit(ie)) {
7731 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7732 commit_idp = &commit_id;
7735 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7736 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7739 const struct got_error *
7740 got_worktree_path_info(struct got_worktree *worktree,
7741 struct got_pathlist_head *paths,
7742 got_worktree_path_info_cb info_cb, void *info_arg,
7743 got_cancel_cb cancel_cb, void *cancel_arg)
7746 const struct got_error *err = NULL, *unlockerr;
7747 struct got_fileindex *fileindex = NULL;
7748 char *fileindex_path = NULL;
7749 struct report_file_info_arg arg;
7751 err = lock_worktree(worktree, LOCK_SH);
7752 if (err)
7753 return err;
7755 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7756 if (err)
7757 goto done;
7759 arg.worktree = worktree;
7760 arg.info_cb = info_cb;
7761 arg.info_arg = info_arg;
7762 arg.paths = paths;
7763 arg.cancel_cb = cancel_cb;
7764 arg.cancel_arg = cancel_arg;
7765 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7766 &arg);
7767 done:
7768 free(fileindex_path);
7769 if (fileindex)
7770 got_fileindex_free(fileindex);
7771 unlockerr = lock_worktree(worktree, LOCK_UN);
7772 if (unlockerr && err == NULL)
7773 err = unlockerr;
7774 return err;