Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
745 char *symlink_path = NULL;
746 FILE *symlinkf = NULL;
748 *local_changes_subsumed = 0;
750 parent = dirname(ondisk_path);
751 if (parent == NULL)
752 return got_error_from_errno2("dirname", ondisk_path);
754 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 return got_error_from_errno("asprintf");
757 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
758 if (err)
759 goto done;
761 free(base_path);
762 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 err = got_error_from_errno("asprintf");
764 base_path = NULL;
765 goto done;
768 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
769 if (err)
770 goto done;
771 if (blob_orig) {
772 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
773 blob_orig);
774 if (err)
775 goto done;
776 } else {
777 /*
778 * If the file has no blob, this is an "add vs add" conflict,
779 * and we simply use an empty ancestor file to make both files
780 * appear in the merged result in their entirety.
781 */
784 /*
785 * In order the run a 3-way merge with a symlink we copy the symlink's
786 * target path into a temporary file and use that file with diff3.
787 */
788 if (S_ISLNK(st_mode)) {
789 char target_path[PATH_MAX];
790 ssize_t target_len;
791 size_t n;
793 free(base_path);
794 if (asprintf(&base_path, "%s/got-symlink-merge",
795 parent) == -1) {
796 err = got_error_from_errno("asprintf");
797 base_path = NULL;
798 goto done;
800 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
801 if (err)
802 goto done;
803 target_len = readlink(ondisk_path, target_path,
804 sizeof(target_path));
805 if (target_len == -1) {
806 err = got_error_from_errno2("readlink", ondisk_path);
807 goto done;
809 n = fwrite(target_path, 1, target_len, symlinkf);
810 if (n != target_len) {
811 err = got_ferror(symlinkf, GOT_ERR_IO);
812 goto done;
814 if (fflush(symlinkf) == EOF) {
815 err = got_error_from_errno2("fflush", symlink_path);
816 goto done;
820 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 label_deriv, label_orig, NULL);
823 if (err)
824 goto done;
826 err = (*progress_cb)(progress_arg,
827 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
828 if (err)
829 goto done;
831 if (fsync(merged_fd) != 0) {
832 err = got_error_from_errno("fsync");
833 goto done;
836 /* Check if a clean merge has subsumed all local changes. */
837 if (overlapcnt == 0) {
838 err = check_files_equal(local_changes_subsumed, deriv_path,
839 merged_path);
840 if (err)
841 goto done;
844 if (fchmod(merged_fd, st_mode) != 0) {
845 err = got_error_from_errno2("fchmod", merged_path);
846 goto done;
849 if (rename(merged_path, ondisk_path) != 0) {
850 err = got_error_from_errno3("rename", merged_path,
851 ondisk_path);
852 goto done;
854 done:
855 if (err) {
856 if (merged_path)
857 unlink(merged_path);
859 if (symlink_path) {
860 if (unlink(symlink_path) == -1 && err == NULL)
861 err = got_error_from_errno2("unlink", symlink_path);
863 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 err = got_error_from_errno2("fclose", symlink_path);
865 free(symlink_path);
866 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 err = got_error_from_errno("close");
868 if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 err = got_error_from_errno("fclose");
870 free(merged_path);
871 free(base_path);
872 if (blob_orig_path) {
873 unlink(blob_orig_path);
874 free(blob_orig_path);
876 return err;
879 static const struct got_error *
880 update_symlink(const char *ondisk_path, const char *target_path,
881 size_t target_len)
883 /* This is not atomic but matches what 'ln -sf' does. */
884 if (unlink(ondisk_path) == -1)
885 return got_error_from_errno2("unlink", ondisk_path);
886 if (symlink(target_path, ondisk_path) == -1)
887 return got_error_from_errno3("symlink", target_path,
888 ondisk_path);
889 return NULL;
892 /*
893 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 * in the work tree with a file that contains conflict markers and the
895 * conflicting target paths of the original version, a "derived version"
896 * of a symlink from an incoming change, and a local version of the symlink.
898 * The original versions's target path can be NULL if it is not available,
899 * such as if both derived versions added a new symlink at the same path.
901 * The incoming derived symlink target is NULL in case the incoming change
902 * has deleted this symlink.
903 */
904 static const struct got_error *
905 install_symlink_conflict(const char *deriv_target,
906 struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 const char *label_orig, const char *local_target, const char *ondisk_path)
909 const struct got_error *err;
910 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
911 FILE *f = NULL;
913 err = got_object_id_str(&id_str, deriv_base_commit_id);
914 if (err)
915 return got_error_from_errno("asprintf");
917 if (asprintf(&label_deriv, "%s: commit %s",
918 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 err = got_error_from_errno("asprintf");
920 goto done;
923 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
924 if (err)
925 goto done;
927 if (fprintf(f, "%s: Could not install symbolic link because of merge "
928 "conflict.\nln(1) may be used to fix the situation. If this is "
929 "intended to be a\nregular file instead then its expected "
930 "contents may be filled in.\nThe following conflicting symlink "
931 "target paths were found:\n"
932 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
933 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
934 deriv_target ? deriv_target : "(symlink was deleted)",
935 orig_target ? label_orig : "",
936 orig_target ? "\n" : "",
937 orig_target ? orig_target : "",
938 orig_target ? "\n" : "",
939 GOT_DIFF_CONFLICT_MARKER_SEP,
940 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
941 err = got_error_from_errno2("fprintf", path);
942 goto done;
945 if (unlink(ondisk_path) == -1) {
946 err = got_error_from_errno2("unlink", ondisk_path);
947 goto done;
949 if (rename(path, ondisk_path) == -1) {
950 err = got_error_from_errno3("rename", path, ondisk_path);
951 goto done;
953 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
954 err = got_error_from_errno2("chmod", ondisk_path);
955 goto done;
957 done:
958 if (f != NULL && fclose(f) == EOF && err == NULL)
959 err = got_error_from_errno2("fclose", path);
960 free(path);
961 free(id_str);
962 free(label_deriv);
963 return err;
966 /* forward declaration */
967 static const struct got_error *
968 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
969 const char *, const char *, uint16_t, const char *,
970 struct got_blob_object *, struct got_object_id *,
971 struct got_repository *, got_worktree_checkout_cb, void *);
973 /*
974 * Merge a symlink into the work tree, where blob_orig acts as the common
975 * ancestor, deriv_target is the link target of the first derived version,
976 * and the symlink on disk acts as the second derived version.
977 * Assume that contents of both blobs represent symlinks.
978 */
979 static const struct got_error *
980 merge_symlink(struct got_worktree *worktree,
981 struct got_blob_object *blob_orig, const char *ondisk_path,
982 const char *path, const char *label_orig, const char *deriv_target,
983 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
984 got_worktree_checkout_cb progress_cb, void *progress_arg)
986 const struct got_error *err = NULL;
987 char *ancestor_target = NULL;
988 struct stat sb;
989 ssize_t ondisk_len, deriv_len;
990 char ondisk_target[PATH_MAX];
991 int have_local_change = 0;
992 int have_incoming_change = 0;
994 if (lstat(ondisk_path, &sb) == -1)
995 return got_error_from_errno2("lstat", ondisk_path);
997 ondisk_len = readlink(ondisk_path, ondisk_target,
998 sizeof(ondisk_target));
999 if (ondisk_len == -1) {
1000 err = got_error_from_errno2("readlink",
1001 ondisk_path);
1002 goto done;
1004 ondisk_target[ondisk_len] = '\0';
1006 if (blob_orig) {
1007 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1008 if (err)
1009 goto done;
1012 if (ancestor_target == NULL ||
1013 (ondisk_len != strlen(ancestor_target) ||
1014 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1015 have_local_change = 1;
1017 deriv_len = strlen(deriv_target);
1018 if (ancestor_target == NULL ||
1019 (deriv_len != strlen(ancestor_target) ||
1020 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1021 have_incoming_change = 1;
1023 if (!have_local_change && !have_incoming_change) {
1024 if (ancestor_target) {
1025 /* Both sides made the same change. */
1026 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1027 path);
1028 } else if (deriv_len == ondisk_len &&
1029 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1030 /* Both sides added the same symlink. */
1031 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1032 path);
1033 } else {
1034 /* Both sides added symlinks which don't match. */
1035 err = install_symlink_conflict(deriv_target,
1036 deriv_base_commit_id, ancestor_target,
1037 label_orig, ondisk_target, ondisk_path);
1038 if (err)
1039 goto done;
1040 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1041 path);
1043 } else if (!have_local_change && have_incoming_change) {
1044 /* Apply the incoming change. */
1045 err = update_symlink(ondisk_path, deriv_target,
1046 strlen(deriv_target));
1047 if (err)
1048 goto done;
1049 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1050 } else if (have_local_change && have_incoming_change) {
1051 if (deriv_len == ondisk_len &&
1052 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1053 /* Both sides made the same change. */
1054 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1055 path);
1056 } else {
1057 err = install_symlink_conflict(deriv_target,
1058 deriv_base_commit_id, ancestor_target, label_orig,
1059 ondisk_target, ondisk_path);
1060 if (err)
1061 goto done;
1062 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1063 path);
1067 done:
1068 free(ancestor_target);
1069 return err;
1073 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1074 * blob_deriv acts as the first derived version, and the file on disk
1075 * acts as the second derived version.
1077 static const struct got_error *
1078 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1079 struct got_blob_object *blob_orig, const char *ondisk_path,
1080 const char *path, uint16_t st_mode, const char *label_orig,
1081 struct got_blob_object *blob_deriv,
1082 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1083 got_worktree_checkout_cb progress_cb, void *progress_arg)
1085 const struct got_error *err = NULL;
1086 FILE *f_deriv = NULL;
1087 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1088 char *label_deriv = NULL, *parent;
1090 *local_changes_subsumed = 0;
1092 parent = dirname(ondisk_path);
1093 if (parent == NULL)
1094 return got_error_from_errno2("dirname", ondisk_path);
1096 free(base_path);
1097 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1098 err = got_error_from_errno("asprintf");
1099 base_path = NULL;
1100 goto done;
1103 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1104 if (err)
1105 goto done;
1106 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1107 blob_deriv);
1108 if (err)
1109 goto done;
1111 err = got_object_id_str(&id_str, deriv_base_commit_id);
1112 if (err)
1113 goto done;
1114 if (asprintf(&label_deriv, "%s: commit %s",
1115 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1116 err = got_error_from_errno("asprintf");
1117 goto done;
1120 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1121 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1122 label_deriv, repo, progress_cb, progress_arg);
1123 done:
1124 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1125 err = got_error_from_errno("fclose");
1126 free(base_path);
1127 if (blob_deriv_path) {
1128 unlink(blob_deriv_path);
1129 free(blob_deriv_path);
1131 free(id_str);
1132 free(label_deriv);
1133 return err;
1136 static const struct got_error *
1137 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1138 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1139 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1141 const struct got_error *err = NULL;
1142 struct got_fileindex_entry *new_ie;
1144 *new_iep = NULL;
1146 err = got_fileindex_entry_alloc(&new_ie, path);
1147 if (err)
1148 return err;
1150 err = got_fileindex_entry_update(new_ie, ondisk_path,
1151 blob_id->sha1, base_commit_id->sha1, 1);
1152 if (err)
1153 goto done;
1155 err = got_fileindex_entry_add(fileindex, new_ie);
1156 done:
1157 if (err)
1158 got_fileindex_entry_free(new_ie);
1159 else
1160 *new_iep = new_ie;
1161 return err;
1164 static mode_t
1165 get_ondisk_perms(int executable, mode_t st_mode)
1167 mode_t xbits = S_IXUSR;
1169 if (executable) {
1170 /* Map read bits to execute bits. */
1171 if (st_mode & S_IRGRP)
1172 xbits |= S_IXGRP;
1173 if (st_mode & S_IROTH)
1174 xbits |= S_IXOTH;
1175 return st_mode | xbits;
1178 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1181 /* forward declaration */
1182 static const struct got_error *
1183 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1184 const char *path, mode_t te_mode, mode_t st_mode,
1185 struct got_blob_object *blob, int restoring_missing_file,
1186 int reverting_versioned_file, int installing_bad_symlink,
1187 int path_is_unversioned, struct got_repository *repo,
1188 got_worktree_checkout_cb progress_cb, void *progress_arg);
1191 * This function assumes that the provided symlink target points at a
1192 * safe location in the work tree!
1194 static const struct got_error *
1195 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1196 size_t target_len)
1198 const struct got_error *err = NULL;
1199 ssize_t elen;
1200 char etarget[PATH_MAX];
1201 int fd;
1204 * "Bad" symlinks (those pointing outside the work tree or into the
1205 * .got directory) are installed in the work tree as a regular file
1206 * which contains the bad symlink target path.
1207 * The new symlink target has already been checked for safety by our
1208 * caller. If we can successfully open a regular file then we simply
1209 * replace this file with a symlink below.
1211 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1212 if (fd == -1) {
1213 if (errno != ELOOP)
1214 return got_error_from_errno2("open", ondisk_path);
1216 /* We are updating an existing on-disk symlink. */
1217 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1218 if (elen == -1)
1219 return got_error_from_errno2("readlink", ondisk_path);
1221 if (elen == target_len &&
1222 memcmp(etarget, target_path, target_len) == 0)
1223 return NULL; /* nothing to do */
1226 err = update_symlink(ondisk_path, target_path, target_len);
1227 if (fd != -1 && close(fd) == -1 && err == NULL)
1228 err = got_error_from_errno2("close", ondisk_path);
1229 return err;
1232 static const struct got_error *
1233 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1234 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1236 const struct got_error *err = NULL;
1237 char canonpath[PATH_MAX];
1238 char *path_got = NULL;
1240 *is_bad_symlink = 0;
1242 if (target_len >= sizeof(canonpath)) {
1243 *is_bad_symlink = 1;
1244 return NULL;
1248 * We do not use realpath(3) to resolve the symlink's target
1249 * path because we don't want to resolve symlinks recursively.
1250 * Instead we make the path absolute and then canonicalize it.
1251 * Relative symlink target lookup should begin at the directory
1252 * in which the blob object is being installed.
1254 if (!got_path_is_absolute(target_path)) {
1255 char *abspath;
1256 char *parent = dirname(ondisk_path);
1257 if (parent == NULL)
1258 return got_error_from_errno2("dirname", ondisk_path);
1259 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1260 return got_error_from_errno("asprintf");
1261 if (strlen(abspath) >= sizeof(canonpath)) {
1262 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1263 free(abspath);
1264 return err;
1266 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1267 free(abspath);
1268 if (err)
1269 return err;
1270 } else {
1271 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1272 if (err)
1273 return err;
1276 /* Only allow symlinks pointing at paths within the work tree. */
1277 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1278 *is_bad_symlink = 1;
1279 return NULL;
1282 /* Do not allow symlinks pointing into the .got directory. */
1283 if (asprintf(&path_got, "%s/%s", wtroot_path,
1284 GOT_WORKTREE_GOT_DIR) == -1)
1285 return got_error_from_errno("asprintf");
1286 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1287 *is_bad_symlink = 1;
1289 free(path_got);
1290 return NULL;
1293 static const struct got_error *
1294 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1295 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1296 int restoring_missing_file, int reverting_versioned_file,
1297 int path_is_unversioned, struct got_repository *repo,
1298 got_worktree_checkout_cb progress_cb, void *progress_arg)
1300 const struct got_error *err = NULL;
1301 char target_path[PATH_MAX];
1302 size_t len, target_len = 0;
1303 char *path_got = NULL;
1304 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1305 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1307 *is_bad_symlink = 0;
1310 * Blob object content specifies the target path of the link.
1311 * If a symbolic link cannot be installed we instead create
1312 * a regular file which contains the link target path stored
1313 * in the blob object.
1315 do {
1316 err = got_object_blob_read_block(&len, blob);
1317 if (len + target_len >= sizeof(target_path)) {
1318 /* Path too long; install as a regular file. */
1319 *is_bad_symlink = 1;
1320 got_object_blob_rewind(blob);
1321 return install_blob(worktree, ondisk_path, path,
1322 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1323 restoring_missing_file, reverting_versioned_file,
1324 1, path_is_unversioned, repo, progress_cb,
1325 progress_arg);
1327 if (len > 0) {
1328 /* Skip blob object header first time around. */
1329 memcpy(target_path + target_len, buf + hdrlen,
1330 len - hdrlen);
1331 target_len += len - hdrlen;
1332 hdrlen = 0;
1334 } while (len != 0);
1335 target_path[target_len] = '\0';
1337 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1338 ondisk_path, worktree->root_path);
1339 if (err)
1340 return err;
1342 if (*is_bad_symlink) {
1343 /* install as a regular file */
1344 *is_bad_symlink = 1;
1345 got_object_blob_rewind(blob);
1346 err = install_blob(worktree, ondisk_path, path,
1347 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1348 restoring_missing_file, reverting_versioned_file, 1,
1349 path_is_unversioned, repo, progress_cb, progress_arg);
1350 goto done;
1353 if (symlink(target_path, ondisk_path) == -1) {
1354 if (errno == EEXIST) {
1355 if (path_is_unversioned) {
1356 err = (*progress_cb)(progress_arg,
1357 GOT_STATUS_UNVERSIONED, path);
1358 goto done;
1360 err = replace_existing_symlink(ondisk_path,
1361 target_path, target_len);
1362 if (err)
1363 goto done;
1364 if (progress_cb) {
1365 err = (*progress_cb)(progress_arg,
1366 reverting_versioned_file ?
1367 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1368 path);
1370 goto done; /* Nothing else to do. */
1373 if (errno == ENOENT) {
1374 char *parent = dirname(ondisk_path);
1375 if (parent == NULL) {
1376 err = got_error_from_errno2("dirname",
1377 ondisk_path);
1378 goto done;
1380 err = add_dir_on_disk(worktree, parent);
1381 if (err)
1382 goto done;
1384 * Retry, and fall through to error handling
1385 * below if this second attempt fails.
1387 if (symlink(target_path, ondisk_path) != -1) {
1388 err = NULL; /* success */
1389 goto done;
1393 /* Handle errors from first or second creation attempt. */
1394 if (errno == ENAMETOOLONG) {
1395 /* bad target path; install as a regular file */
1396 *is_bad_symlink = 1;
1397 got_object_blob_rewind(blob);
1398 err = install_blob(worktree, ondisk_path, path,
1399 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1400 restoring_missing_file, reverting_versioned_file, 1,
1401 path_is_unversioned, repo,
1402 progress_cb, progress_arg);
1403 } else if (errno == ENOTDIR) {
1404 err = got_error_path(ondisk_path,
1405 GOT_ERR_FILE_OBSTRUCTED);
1406 } else {
1407 err = got_error_from_errno3("symlink",
1408 target_path, ondisk_path);
1410 } else if (progress_cb)
1411 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1412 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1413 done:
1414 free(path_got);
1415 return err;
1418 static const struct got_error *
1419 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1420 const char *path, mode_t te_mode, mode_t st_mode,
1421 struct got_blob_object *blob, int restoring_missing_file,
1422 int reverting_versioned_file, int installing_bad_symlink,
1423 int path_is_unversioned, struct got_repository *repo,
1424 got_worktree_checkout_cb progress_cb, void *progress_arg)
1426 const struct got_error *err = NULL;
1427 int fd = -1;
1428 size_t len, hdrlen;
1429 int update = 0;
1430 char *tmppath = NULL;
1432 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1433 GOT_DEFAULT_FILE_MODE);
1434 if (fd == -1) {
1435 if (errno == ENOENT) {
1436 char *parent = dirname(path);
1437 if (parent == NULL)
1438 return got_error_from_errno2("dirname", path);
1439 err = add_dir_on_disk(worktree, parent);
1440 if (err)
1441 return err;
1442 fd = open(ondisk_path,
1443 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1444 GOT_DEFAULT_FILE_MODE);
1445 if (fd == -1)
1446 return got_error_from_errno2("open",
1447 ondisk_path);
1448 } else if (errno == EEXIST) {
1449 if (path_is_unversioned) {
1450 err = (*progress_cb)(progress_arg,
1451 GOT_STATUS_UNVERSIONED, path);
1452 goto done;
1454 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1455 /* TODO file is obstructed; do something */
1456 err = got_error_path(ondisk_path,
1457 GOT_ERR_FILE_OBSTRUCTED);
1458 goto done;
1459 } else {
1460 err = got_opentemp_named_fd(&tmppath, &fd,
1461 ondisk_path);
1462 if (err)
1463 goto done;
1464 update = 1;
1466 } else
1467 return got_error_from_errno2("open", ondisk_path);
1470 if (progress_cb) {
1471 if (restoring_missing_file)
1472 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1473 path);
1474 else if (reverting_versioned_file)
1475 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1476 path);
1477 else
1478 err = (*progress_cb)(progress_arg,
1479 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1480 if (err)
1481 goto done;
1484 hdrlen = got_object_blob_get_hdrlen(blob);
1485 do {
1486 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1487 err = got_object_blob_read_block(&len, blob);
1488 if (err)
1489 break;
1490 if (len > 0) {
1491 /* Skip blob object header first time around. */
1492 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1493 if (outlen == -1) {
1494 err = got_error_from_errno("write");
1495 goto done;
1496 } else if (outlen != len - hdrlen) {
1497 err = got_error(GOT_ERR_IO);
1498 goto done;
1500 hdrlen = 0;
1502 } while (len != 0);
1504 if (fsync(fd) != 0) {
1505 err = got_error_from_errno("fsync");
1506 goto done;
1509 if (update) {
1510 if (rename(tmppath, ondisk_path) != 0) {
1511 err = got_error_from_errno3("rename", tmppath,
1512 ondisk_path);
1513 unlink(tmppath);
1514 goto done;
1518 if (chmod(ondisk_path,
1519 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1520 err = got_error_from_errno2("chmod", ondisk_path);
1521 goto done;
1524 done:
1525 if (fd != -1 && close(fd) != 0 && err == NULL)
1526 err = got_error_from_errno("close");
1527 free(tmppath);
1528 return err;
1531 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1532 static const struct got_error *
1533 get_modified_file_content_status(unsigned char *status, FILE *f)
1535 const struct got_error *err = NULL;
1536 const char *markers[3] = {
1537 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1538 GOT_DIFF_CONFLICT_MARKER_SEP,
1539 GOT_DIFF_CONFLICT_MARKER_END
1541 int i = 0;
1542 char *line;
1543 size_t len;
1544 const char delim[3] = {'\0', '\0', '\0'};
1546 while (*status == GOT_STATUS_MODIFY) {
1547 line = fparseln(f, &len, NULL, delim, 0);
1548 if (line == NULL) {
1549 if (feof(f))
1550 break;
1551 err = got_ferror(f, GOT_ERR_IO);
1552 break;
1555 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1556 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1557 == 0)
1558 *status = GOT_STATUS_CONFLICT;
1559 else
1560 i++;
1564 return err;
1567 static int
1568 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1570 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1571 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1574 static int
1575 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1577 return !(ie->ctime_sec == sb->st_ctime &&
1578 ie->ctime_nsec == sb->st_ctimensec &&
1579 ie->mtime_sec == sb->st_mtime &&
1580 ie->mtime_nsec == sb->st_mtimensec &&
1581 ie->size == (sb->st_size & 0xffffffff) &&
1582 !xbit_differs(ie, sb->st_mode));
1585 static unsigned char
1586 get_staged_status(struct got_fileindex_entry *ie)
1588 switch (got_fileindex_entry_stage_get(ie)) {
1589 case GOT_FILEIDX_STAGE_ADD:
1590 return GOT_STATUS_ADD;
1591 case GOT_FILEIDX_STAGE_DELETE:
1592 return GOT_STATUS_DELETE;
1593 case GOT_FILEIDX_STAGE_MODIFY:
1594 return GOT_STATUS_MODIFY;
1595 default:
1596 return GOT_STATUS_NO_CHANGE;
1600 static const struct got_error *
1601 get_symlink_modification_status(unsigned char *status,
1602 struct got_fileindex_entry *ie, const char *abspath,
1603 int dirfd, const char *de_name, struct got_blob_object *blob)
1605 const struct got_error *err = NULL;
1606 char target_path[PATH_MAX];
1607 char etarget[PATH_MAX];
1608 ssize_t elen;
1609 size_t len, target_len = 0;
1610 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1611 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1613 *status = GOT_STATUS_NO_CHANGE;
1615 /* Blob object content specifies the target path of the link. */
1616 do {
1617 err = got_object_blob_read_block(&len, blob);
1618 if (err)
1619 return err;
1620 if (len + target_len >= sizeof(target_path)) {
1622 * Should not happen. The blob contents were OK
1623 * when this symlink was installed.
1625 return got_error(GOT_ERR_NO_SPACE);
1627 if (len > 0) {
1628 /* Skip blob object header first time around. */
1629 memcpy(target_path + target_len, buf + hdrlen,
1630 len - hdrlen);
1631 target_len += len - hdrlen;
1632 hdrlen = 0;
1634 } while (len != 0);
1635 target_path[target_len] = '\0';
1637 if (dirfd != -1) {
1638 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1639 if (elen == -1)
1640 return got_error_from_errno2("readlinkat", abspath);
1641 } else {
1642 elen = readlink(abspath, etarget, sizeof(etarget));
1643 if (elen == -1)
1644 return got_error_from_errno2("readlink", abspath);
1647 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1648 *status = GOT_STATUS_MODIFY;
1650 return NULL;
1653 static const struct got_error *
1654 get_file_status(unsigned char *status, struct stat *sb,
1655 struct got_fileindex_entry *ie, const char *abspath,
1656 int dirfd, const char *de_name, struct got_repository *repo)
1658 const struct got_error *err = NULL;
1659 struct got_object_id id;
1660 size_t hdrlen;
1661 int fd = -1;
1662 FILE *f = NULL;
1663 uint8_t fbuf[8192];
1664 struct got_blob_object *blob = NULL;
1665 size_t flen, blen;
1666 unsigned char staged_status = get_staged_status(ie);
1668 *status = GOT_STATUS_NO_CHANGE;
1671 * Whenever the caller provides a directory descriptor and a
1672 * directory entry name for the file, use them! This prevents
1673 * race conditions if filesystem paths change beneath our feet.
1675 if (dirfd != -1) {
1676 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1677 if (errno == ENOENT) {
1678 if (got_fileindex_entry_has_file_on_disk(ie))
1679 *status = GOT_STATUS_MISSING;
1680 else
1681 *status = GOT_STATUS_DELETE;
1682 goto done;
1684 err = got_error_from_errno2("fstatat", abspath);
1685 goto done;
1687 } else {
1688 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1689 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1690 return got_error_from_errno2("open", abspath);
1691 else if (fd == -1 && errno == ELOOP) {
1692 if (lstat(abspath, sb) == -1)
1693 return got_error_from_errno2("lstat", abspath);
1694 } else if (fd == -1 || fstat(fd, sb) == -1) {
1695 if (errno == ENOENT) {
1696 if (got_fileindex_entry_has_file_on_disk(ie))
1697 *status = GOT_STATUS_MISSING;
1698 else
1699 *status = GOT_STATUS_DELETE;
1700 goto done;
1702 err = got_error_from_errno2("fstat", abspath);
1703 goto done;
1707 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1708 *status = GOT_STATUS_OBSTRUCTED;
1709 goto done;
1712 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1713 *status = GOT_STATUS_DELETE;
1714 goto done;
1715 } else if (!got_fileindex_entry_has_blob(ie) &&
1716 staged_status != GOT_STATUS_ADD) {
1717 *status = GOT_STATUS_ADD;
1718 goto done;
1721 if (!stat_info_differs(ie, sb))
1722 goto done;
1724 if (S_ISLNK(sb->st_mode) &&
1725 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1726 *status = GOT_STATUS_MODIFY;
1727 goto done;
1730 if (staged_status == GOT_STATUS_MODIFY ||
1731 staged_status == GOT_STATUS_ADD)
1732 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1733 else
1734 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1736 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1737 if (err)
1738 goto done;
1740 if (S_ISLNK(sb->st_mode)) {
1741 err = get_symlink_modification_status(status, ie,
1742 abspath, dirfd, de_name, blob);
1743 goto done;
1746 if (dirfd != -1) {
1747 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1748 if (fd == -1) {
1749 err = got_error_from_errno2("openat", abspath);
1750 goto done;
1754 f = fdopen(fd, "r");
1755 if (f == NULL) {
1756 err = got_error_from_errno2("fdopen", abspath);
1757 goto done;
1759 fd = -1;
1760 hdrlen = got_object_blob_get_hdrlen(blob);
1761 for (;;) {
1762 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1763 err = got_object_blob_read_block(&blen, blob);
1764 if (err)
1765 goto done;
1766 /* Skip length of blob object header first time around. */
1767 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1768 if (flen == 0 && ferror(f)) {
1769 err = got_error_from_errno("fread");
1770 goto done;
1772 if (blen == 0) {
1773 if (flen != 0)
1774 *status = GOT_STATUS_MODIFY;
1775 break;
1776 } else if (flen == 0) {
1777 if (blen != 0)
1778 *status = GOT_STATUS_MODIFY;
1779 break;
1780 } else if (blen - hdrlen == flen) {
1781 /* Skip blob object header first time around. */
1782 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1783 *status = GOT_STATUS_MODIFY;
1784 break;
1786 } else {
1787 *status = GOT_STATUS_MODIFY;
1788 break;
1790 hdrlen = 0;
1793 if (*status == GOT_STATUS_MODIFY) {
1794 rewind(f);
1795 err = get_modified_file_content_status(status, f);
1796 } else if (xbit_differs(ie, sb->st_mode))
1797 *status = GOT_STATUS_MODE_CHANGE;
1798 done:
1799 if (blob)
1800 got_object_blob_close(blob);
1801 if (f != NULL && fclose(f) == EOF && err == NULL)
1802 err = got_error_from_errno2("fclose", abspath);
1803 if (fd != -1 && close(fd) == -1 && err == NULL)
1804 err = got_error_from_errno2("close", abspath);
1805 return err;
1809 * Update timestamps in the file index if a file is unmodified and
1810 * we had to run a full content comparison to find out.
1812 static const struct got_error *
1813 sync_timestamps(char *ondisk_path, unsigned char status,
1814 struct got_fileindex_entry *ie, struct stat *sb)
1816 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1817 return got_fileindex_entry_update(ie, ondisk_path,
1818 ie->blob_sha1, ie->commit_sha1, 1);
1820 return NULL;
1823 static const struct got_error *
1824 update_blob(struct got_worktree *worktree,
1825 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1826 struct got_tree_entry *te, const char *path,
1827 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1828 void *progress_arg)
1830 const struct got_error *err = NULL;
1831 struct got_blob_object *blob = NULL;
1832 char *ondisk_path;
1833 unsigned char status = GOT_STATUS_NO_CHANGE;
1834 struct stat sb;
1836 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1837 return got_error_from_errno("asprintf");
1839 if (ie) {
1840 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1841 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1842 goto done;
1844 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1845 repo);
1846 if (err)
1847 goto done;
1848 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1849 sb.st_mode = got_fileindex_perms_to_st(ie);
1850 } else {
1851 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1852 status = GOT_STATUS_UNVERSIONED;
1855 if (status == GOT_STATUS_OBSTRUCTED) {
1856 err = (*progress_cb)(progress_arg, status, path);
1857 goto done;
1859 if (status == GOT_STATUS_CONFLICT) {
1860 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1861 path);
1862 goto done;
1865 if (ie && status != GOT_STATUS_MISSING &&
1866 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1867 if (got_fileindex_entry_has_commit(ie) &&
1868 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1869 SHA1_DIGEST_LENGTH) == 0) {
1870 err = sync_timestamps(ondisk_path, status, ie, &sb);
1871 if (err)
1872 goto done;
1873 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1874 path);
1875 goto done;
1877 if (got_fileindex_entry_has_blob(ie) &&
1878 memcmp(ie->blob_sha1, te->id.sha1,
1879 SHA1_DIGEST_LENGTH) == 0) {
1880 err = sync_timestamps(ondisk_path, status, ie, &sb);
1881 goto done;
1885 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1886 if (err)
1887 goto done;
1889 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1890 int update_timestamps;
1891 struct got_blob_object *blob2 = NULL;
1892 char *label_orig = NULL;
1893 if (got_fileindex_entry_has_blob(ie)) {
1894 struct got_object_id id2;
1895 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1896 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1897 if (err)
1898 goto done;
1900 if (got_fileindex_entry_has_commit(ie)) {
1901 char id_str[SHA1_DIGEST_STRING_LENGTH];
1902 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1903 sizeof(id_str)) == NULL) {
1904 err = got_error_path(id_str,
1905 GOT_ERR_BAD_OBJ_ID_STR);
1906 goto done;
1908 if (asprintf(&label_orig, "%s: commit %s",
1909 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1910 err = got_error_from_errno("asprintf");
1911 goto done;
1914 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1915 char *link_target;
1916 err = got_object_blob_read_to_str(&link_target, blob);
1917 if (err)
1918 goto done;
1919 err = merge_symlink(worktree, blob2, ondisk_path, path,
1920 label_orig, link_target, worktree->base_commit_id,
1921 repo, progress_cb, progress_arg);
1922 free(link_target);
1923 } else {
1924 err = merge_blob(&update_timestamps, worktree, blob2,
1925 ondisk_path, path, sb.st_mode, label_orig, blob,
1926 worktree->base_commit_id, repo,
1927 progress_cb, progress_arg);
1929 free(label_orig);
1930 if (blob2)
1931 got_object_blob_close(blob2);
1932 if (err)
1933 goto done;
1935 * Do not update timestamps of files with local changes.
1936 * Otherwise, a future status walk would treat them as
1937 * unmodified files again.
1939 err = got_fileindex_entry_update(ie, ondisk_path,
1940 blob->id.sha1, worktree->base_commit_id->sha1,
1941 update_timestamps);
1942 } else if (status == GOT_STATUS_MODE_CHANGE) {
1943 err = got_fileindex_entry_update(ie, ondisk_path,
1944 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1945 } else if (status == GOT_STATUS_DELETE) {
1946 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1947 if (err)
1948 goto done;
1949 err = got_fileindex_entry_update(ie, ondisk_path,
1950 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1951 if (err)
1952 goto done;
1953 } else {
1954 int is_bad_symlink = 0;
1955 if (S_ISLNK(te->mode)) {
1956 err = install_symlink(&is_bad_symlink, worktree,
1957 ondisk_path, path, blob,
1958 status == GOT_STATUS_MISSING, 0,
1959 status == GOT_STATUS_UNVERSIONED, repo,
1960 progress_cb, progress_arg);
1961 } else {
1962 err = install_blob(worktree, ondisk_path, path,
1963 te->mode, sb.st_mode, blob,
1964 status == GOT_STATUS_MISSING, 0, 0,
1965 status == GOT_STATUS_UNVERSIONED, repo,
1966 progress_cb, progress_arg);
1968 if (err)
1969 goto done;
1971 if (ie) {
1972 err = got_fileindex_entry_update(ie, ondisk_path,
1973 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1974 } else {
1975 err = create_fileindex_entry(&ie, fileindex,
1976 worktree->base_commit_id, ondisk_path, path,
1977 &blob->id);
1979 if (err)
1980 goto done;
1982 if (is_bad_symlink) {
1983 got_fileindex_entry_filetype_set(ie,
1984 GOT_FILEIDX_MODE_BAD_SYMLINK);
1987 got_object_blob_close(blob);
1988 done:
1989 free(ondisk_path);
1990 return err;
1993 static const struct got_error *
1994 remove_ondisk_file(const char *root_path, const char *path)
1996 const struct got_error *err = NULL;
1997 char *ondisk_path = NULL;
1999 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2000 return got_error_from_errno("asprintf");
2002 if (unlink(ondisk_path) == -1) {
2003 if (errno != ENOENT)
2004 err = got_error_from_errno2("unlink", ondisk_path);
2005 } else {
2006 char *parent = dirname(ondisk_path);
2007 while (parent && strcmp(parent, root_path) != 0) {
2008 if (rmdir(parent) == -1) {
2009 if (errno != ENOTEMPTY)
2010 err = got_error_from_errno2("rmdir",
2011 parent);
2012 break;
2014 parent = dirname(parent);
2017 free(ondisk_path);
2018 return err;
2021 static const struct got_error *
2022 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2023 struct got_fileindex_entry *ie, struct got_repository *repo,
2024 got_worktree_checkout_cb progress_cb, void *progress_arg)
2026 const struct got_error *err = NULL;
2027 unsigned char status;
2028 struct stat sb;
2029 char *ondisk_path;
2031 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2032 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2034 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2035 == -1)
2036 return got_error_from_errno("asprintf");
2038 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2039 if (err)
2040 goto done;
2042 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2043 char ondisk_target[PATH_MAX];
2044 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2045 sizeof(ondisk_target));
2046 if (ondisk_len == -1) {
2047 err = got_error_from_errno2("readlink", ondisk_path);
2048 goto done;
2050 ondisk_target[ondisk_len] = '\0';
2051 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2052 NULL, NULL, /* XXX pass common ancestor info? */
2053 ondisk_target, ondisk_path);
2054 if (err)
2055 goto done;
2056 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2057 ie->path);
2058 goto done;
2061 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2062 status == GOT_STATUS_ADD) {
2063 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2064 if (err)
2065 goto done;
2067 * Preserve the working file and change the deleted blob's
2068 * entry into a schedule-add entry.
2070 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2071 0);
2072 } else {
2073 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2074 if (err)
2075 goto done;
2076 if (status == GOT_STATUS_NO_CHANGE) {
2077 err = remove_ondisk_file(worktree->root_path, ie->path);
2078 if (err)
2079 goto done;
2081 got_fileindex_entry_remove(fileindex, ie);
2083 done:
2084 free(ondisk_path);
2085 return err;
2088 struct diff_cb_arg {
2089 struct got_fileindex *fileindex;
2090 struct got_worktree *worktree;
2091 struct got_repository *repo;
2092 got_worktree_checkout_cb progress_cb;
2093 void *progress_arg;
2094 got_cancel_cb cancel_cb;
2095 void *cancel_arg;
2098 static const struct got_error *
2099 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2100 struct got_tree_entry *te, const char *parent_path)
2102 struct diff_cb_arg *a = arg;
2104 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2105 return got_error(GOT_ERR_CANCELLED);
2107 return update_blob(a->worktree, a->fileindex, ie, te,
2108 ie->path, a->repo, a->progress_cb, a->progress_arg);
2111 static const struct got_error *
2112 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2114 struct diff_cb_arg *a = arg;
2116 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2117 return got_error(GOT_ERR_CANCELLED);
2119 return delete_blob(a->worktree, a->fileindex, ie,
2120 a->repo, a->progress_cb, a->progress_arg);
2123 static const struct got_error *
2124 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2126 struct diff_cb_arg *a = arg;
2127 const struct got_error *err;
2128 char *path;
2130 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2131 return got_error(GOT_ERR_CANCELLED);
2133 if (got_object_tree_entry_is_submodule(te))
2134 return NULL;
2136 if (asprintf(&path, "%s%s%s", parent_path,
2137 parent_path[0] ? "/" : "", te->name)
2138 == -1)
2139 return got_error_from_errno("asprintf");
2141 if (S_ISDIR(te->mode))
2142 err = add_dir_on_disk(a->worktree, path);
2143 else
2144 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2145 a->repo, a->progress_cb, a->progress_arg);
2147 free(path);
2148 return err;
2151 static const struct got_error *
2152 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2154 const struct got_error *err = NULL;
2155 char *uuidstr = NULL;
2156 uint32_t uuid_status;
2158 *refname = NULL;
2160 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2161 if (uuid_status != uuid_s_ok)
2162 return got_error_uuid(uuid_status, "uuid_to_string");
2164 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2165 == -1) {
2166 err = got_error_from_errno("asprintf");
2167 *refname = NULL;
2169 free(uuidstr);
2170 return err;
2173 const struct got_error *
2174 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2176 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2179 static const struct got_error *
2180 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2182 return get_ref_name(refname, worktree,
2183 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2186 static const struct got_error *
2187 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2189 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2192 static const struct got_error *
2193 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2195 return get_ref_name(refname, worktree,
2196 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2199 static const struct got_error *
2200 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2202 return get_ref_name(refname, worktree,
2203 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2206 static const struct got_error *
2207 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2209 return get_ref_name(refname, worktree,
2210 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2213 static const struct got_error *
2214 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2216 return get_ref_name(refname, worktree,
2217 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2220 static const struct got_error *
2221 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2227 static const struct got_error *
2228 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2234 const struct got_error *
2235 got_worktree_get_histedit_script_path(char **path,
2236 struct got_worktree *worktree)
2238 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2239 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2240 *path = NULL;
2241 return got_error_from_errno("asprintf");
2243 return NULL;
2247 * Prevent Git's garbage collector from deleting our base commit by
2248 * setting a reference to our base commit's ID.
2250 static const struct got_error *
2251 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2253 const struct got_error *err = NULL;
2254 struct got_reference *ref = NULL;
2255 char *refname;
2257 err = got_worktree_get_base_ref_name(&refname, worktree);
2258 if (err)
2259 return err;
2261 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2262 if (err)
2263 goto done;
2265 err = got_ref_write(ref, repo);
2266 done:
2267 free(refname);
2268 if (ref)
2269 got_ref_close(ref);
2270 return err;
2273 static const struct got_error *
2274 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2276 const struct got_error *err = NULL;
2278 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2279 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2280 err = got_error_from_errno("asprintf");
2281 *fileindex_path = NULL;
2283 return err;
2287 static const struct got_error *
2288 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2289 struct got_worktree *worktree)
2291 const struct got_error *err = NULL;
2292 FILE *index = NULL;
2294 *fileindex_path = NULL;
2295 *fileindex = got_fileindex_alloc();
2296 if (*fileindex == NULL)
2297 return got_error_from_errno("got_fileindex_alloc");
2299 err = get_fileindex_path(fileindex_path, worktree);
2300 if (err)
2301 goto done;
2303 index = fopen(*fileindex_path, "rb");
2304 if (index == NULL) {
2305 if (errno != ENOENT)
2306 err = got_error_from_errno2("fopen", *fileindex_path);
2307 } else {
2308 err = got_fileindex_read(*fileindex, index);
2309 if (fclose(index) != 0 && err == NULL)
2310 err = got_error_from_errno("fclose");
2312 done:
2313 if (err) {
2314 free(*fileindex_path);
2315 *fileindex_path = NULL;
2316 got_fileindex_free(*fileindex);
2317 *fileindex = NULL;
2319 return err;
2322 struct bump_base_commit_id_arg {
2323 struct got_object_id *base_commit_id;
2324 const char *path;
2325 size_t path_len;
2326 const char *entry_name;
2327 got_worktree_checkout_cb progress_cb;
2328 void *progress_arg;
2331 /* Bump base commit ID of all files within an updated part of the work tree. */
2332 static const struct got_error *
2333 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2335 const struct got_error *err;
2336 struct bump_base_commit_id_arg *a = arg;
2338 if (a->entry_name) {
2339 if (strcmp(ie->path, a->path) != 0)
2340 return NULL;
2341 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2342 return NULL;
2344 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2345 SHA1_DIGEST_LENGTH) == 0)
2346 return NULL;
2348 if (a->progress_cb) {
2349 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2350 ie->path);
2351 if (err)
2352 return err;
2354 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2355 return NULL;
2358 static const struct got_error *
2359 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2361 const struct got_error *err = NULL;
2362 char *new_fileindex_path = NULL;
2363 FILE *new_index = NULL;
2364 struct timespec timeout;
2366 err = got_opentemp_named(&new_fileindex_path, &new_index,
2367 fileindex_path);
2368 if (err)
2369 goto done;
2371 err = got_fileindex_write(fileindex, new_index);
2372 if (err)
2373 goto done;
2375 if (rename(new_fileindex_path, fileindex_path) != 0) {
2376 err = got_error_from_errno3("rename", new_fileindex_path,
2377 fileindex_path);
2378 unlink(new_fileindex_path);
2382 * Sleep for a short amount of time to ensure that files modified after
2383 * this program exits have a different time stamp from the one which
2384 * was recorded in the file index.
2386 timeout.tv_sec = 0;
2387 timeout.tv_nsec = 1;
2388 nanosleep(&timeout, NULL);
2389 done:
2390 if (new_index)
2391 fclose(new_index);
2392 free(new_fileindex_path);
2393 return err;
2396 static const struct got_error *
2397 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2398 struct got_object_id **tree_id, const char *wt_relpath,
2399 struct got_worktree *worktree, struct got_repository *repo)
2401 const struct got_error *err = NULL;
2402 struct got_object_id *id = NULL;
2403 char *in_repo_path = NULL;
2404 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2406 *entry_type = GOT_OBJ_TYPE_ANY;
2407 *tree_relpath = NULL;
2408 *tree_id = NULL;
2410 if (wt_relpath[0] == '\0') {
2411 /* Check out all files within the work tree. */
2412 *entry_type = GOT_OBJ_TYPE_TREE;
2413 *tree_relpath = strdup("");
2414 if (*tree_relpath == NULL) {
2415 err = got_error_from_errno("strdup");
2416 goto done;
2418 err = got_object_id_by_path(tree_id, repo,
2419 worktree->base_commit_id, worktree->path_prefix);
2420 if (err)
2421 goto done;
2422 return NULL;
2425 /* Check out a subset of files in the work tree. */
2427 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2428 is_root_wt ? "" : "/", wt_relpath) == -1) {
2429 err = got_error_from_errno("asprintf");
2430 goto done;
2433 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2434 in_repo_path);
2435 if (err)
2436 goto done;
2438 free(in_repo_path);
2439 in_repo_path = NULL;
2441 err = got_object_get_type(entry_type, repo, id);
2442 if (err)
2443 goto done;
2445 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2446 /* Check out a single file. */
2447 if (strchr(wt_relpath, '/') == NULL) {
2448 /* Check out a single file in work tree's root dir. */
2449 in_repo_path = strdup(worktree->path_prefix);
2450 if (in_repo_path == NULL) {
2451 err = got_error_from_errno("strdup");
2452 goto done;
2454 *tree_relpath = strdup("");
2455 if (*tree_relpath == NULL) {
2456 err = got_error_from_errno("strdup");
2457 goto done;
2459 } else {
2460 /* Check out a single file in a subdirectory. */
2461 err = got_path_dirname(tree_relpath, wt_relpath);
2462 if (err)
2463 return err;
2464 if (asprintf(&in_repo_path, "%s%s%s",
2465 worktree->path_prefix, is_root_wt ? "" : "/",
2466 *tree_relpath) == -1) {
2467 err = got_error_from_errno("asprintf");
2468 goto done;
2471 err = got_object_id_by_path(tree_id, repo,
2472 worktree->base_commit_id, in_repo_path);
2473 } else {
2474 /* Check out all files within a subdirectory. */
2475 *tree_id = got_object_id_dup(id);
2476 if (*tree_id == NULL) {
2477 err = got_error_from_errno("got_object_id_dup");
2478 goto done;
2480 *tree_relpath = strdup(wt_relpath);
2481 if (*tree_relpath == NULL) {
2482 err = got_error_from_errno("strdup");
2483 goto done;
2486 done:
2487 free(id);
2488 free(in_repo_path);
2489 if (err) {
2490 *entry_type = GOT_OBJ_TYPE_ANY;
2491 free(*tree_relpath);
2492 *tree_relpath = NULL;
2493 free(*tree_id);
2494 *tree_id = NULL;
2496 return err;
2499 static const struct got_error *
2500 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2501 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2502 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2503 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2505 const struct got_error *err = NULL;
2506 struct got_commit_object *commit = NULL;
2507 struct got_tree_object *tree = NULL;
2508 struct got_fileindex_diff_tree_cb diff_cb;
2509 struct diff_cb_arg arg;
2511 err = ref_base_commit(worktree, repo);
2512 if (err) {
2513 if (!(err->code == GOT_ERR_ERRNO &&
2514 (errno == EACCES || errno == EROFS)))
2515 goto done;
2516 err = (*progress_cb)(progress_arg,
2517 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2518 if (err)
2519 return err;
2522 err = got_object_open_as_commit(&commit, repo,
2523 worktree->base_commit_id);
2524 if (err)
2525 goto done;
2527 err = got_object_open_as_tree(&tree, repo, tree_id);
2528 if (err)
2529 goto done;
2531 if (entry_name &&
2532 got_object_tree_find_entry(tree, entry_name) == NULL) {
2533 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2534 goto done;
2537 diff_cb.diff_old_new = diff_old_new;
2538 diff_cb.diff_old = diff_old;
2539 diff_cb.diff_new = diff_new;
2540 arg.fileindex = fileindex;
2541 arg.worktree = worktree;
2542 arg.repo = repo;
2543 arg.progress_cb = progress_cb;
2544 arg.progress_arg = progress_arg;
2545 arg.cancel_cb = cancel_cb;
2546 arg.cancel_arg = cancel_arg;
2547 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2548 entry_name, repo, &diff_cb, &arg);
2549 done:
2550 if (tree)
2551 got_object_tree_close(tree);
2552 if (commit)
2553 got_object_commit_close(commit);
2554 return err;
2557 const struct got_error *
2558 got_worktree_checkout_files(struct got_worktree *worktree,
2559 struct got_pathlist_head *paths, struct got_repository *repo,
2560 got_worktree_checkout_cb progress_cb, void *progress_arg,
2561 got_cancel_cb cancel_cb, void *cancel_arg)
2563 const struct got_error *err = NULL, *sync_err, *unlockerr;
2564 struct got_commit_object *commit = NULL;
2565 struct got_tree_object *tree = NULL;
2566 struct got_fileindex *fileindex = NULL;
2567 char *fileindex_path = NULL;
2568 struct got_pathlist_entry *pe;
2569 struct tree_path_data {
2570 SIMPLEQ_ENTRY(tree_path_data) entry;
2571 struct got_object_id *tree_id;
2572 int entry_type;
2573 char *relpath;
2574 char *entry_name;
2575 } *tpd = NULL;
2576 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2578 SIMPLEQ_INIT(&tree_paths);
2580 err = lock_worktree(worktree, LOCK_EX);
2581 if (err)
2582 return err;
2584 /* Map all specified paths to in-repository trees. */
2585 TAILQ_FOREACH(pe, paths, entry) {
2586 tpd = malloc(sizeof(*tpd));
2587 if (tpd == NULL) {
2588 err = got_error_from_errno("malloc");
2589 goto done;
2592 err = find_tree_entry_for_checkout(&tpd->entry_type,
2593 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2594 if (err) {
2595 free(tpd);
2596 goto done;
2599 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2600 err = got_path_basename(&tpd->entry_name, pe->path);
2601 if (err) {
2602 free(tpd->relpath);
2603 free(tpd->tree_id);
2604 free(tpd);
2605 goto done;
2607 } else
2608 tpd->entry_name = NULL;
2610 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2614 * Read the file index.
2615 * Checking out files is supposed to be an idempotent operation.
2616 * If the on-disk file index is incomplete we will try to complete it.
2618 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2619 if (err)
2620 goto done;
2622 tpd = SIMPLEQ_FIRST(&tree_paths);
2623 TAILQ_FOREACH(pe, paths, entry) {
2624 struct bump_base_commit_id_arg bbc_arg;
2626 err = checkout_files(worktree, fileindex, tpd->relpath,
2627 tpd->tree_id, tpd->entry_name, repo,
2628 progress_cb, progress_arg, cancel_cb, cancel_arg);
2629 if (err)
2630 break;
2632 bbc_arg.base_commit_id = worktree->base_commit_id;
2633 bbc_arg.entry_name = tpd->entry_name;
2634 bbc_arg.path = pe->path;
2635 bbc_arg.path_len = pe->path_len;
2636 bbc_arg.progress_cb = progress_cb;
2637 bbc_arg.progress_arg = progress_arg;
2638 err = got_fileindex_for_each_entry_safe(fileindex,
2639 bump_base_commit_id, &bbc_arg);
2640 if (err)
2641 break;
2643 tpd = SIMPLEQ_NEXT(tpd, entry);
2645 sync_err = sync_fileindex(fileindex, fileindex_path);
2646 if (sync_err && err == NULL)
2647 err = sync_err;
2648 done:
2649 free(fileindex_path);
2650 if (tree)
2651 got_object_tree_close(tree);
2652 if (commit)
2653 got_object_commit_close(commit);
2654 if (fileindex)
2655 got_fileindex_free(fileindex);
2656 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2657 tpd = SIMPLEQ_FIRST(&tree_paths);
2658 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2659 free(tpd->relpath);
2660 free(tpd->tree_id);
2661 free(tpd);
2663 unlockerr = lock_worktree(worktree, LOCK_SH);
2664 if (unlockerr && err == NULL)
2665 err = unlockerr;
2666 return err;
2669 struct merge_file_cb_arg {
2670 struct got_worktree *worktree;
2671 struct got_fileindex *fileindex;
2672 got_worktree_checkout_cb progress_cb;
2673 void *progress_arg;
2674 got_cancel_cb cancel_cb;
2675 void *cancel_arg;
2676 const char *label_orig;
2677 struct got_object_id *commit_id2;
2680 static const struct got_error *
2681 merge_file_cb(void *arg, struct got_blob_object *blob1,
2682 struct got_blob_object *blob2, struct got_object_id *id1,
2683 struct got_object_id *id2, const char *path1, const char *path2,
2684 mode_t mode1, mode_t mode2, struct got_repository *repo)
2686 static const struct got_error *err = NULL;
2687 struct merge_file_cb_arg *a = arg;
2688 struct got_fileindex_entry *ie;
2689 char *ondisk_path = NULL;
2690 struct stat sb;
2691 unsigned char status;
2692 int local_changes_subsumed;
2694 if (blob1 && blob2) {
2695 ie = got_fileindex_entry_get(a->fileindex, path2,
2696 strlen(path2));
2697 if (ie == NULL)
2698 return (*a->progress_cb)(a->progress_arg,
2699 GOT_STATUS_MISSING, path2);
2701 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2702 path2) == -1)
2703 return got_error_from_errno("asprintf");
2705 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2706 repo);
2707 if (err)
2708 goto done;
2710 if (status == GOT_STATUS_DELETE) {
2711 err = (*a->progress_cb)(a->progress_arg,
2712 GOT_STATUS_MERGE, path2);
2713 goto done;
2715 if (status != GOT_STATUS_NO_CHANGE &&
2716 status != GOT_STATUS_MODIFY &&
2717 status != GOT_STATUS_CONFLICT &&
2718 status != GOT_STATUS_ADD) {
2719 err = (*a->progress_cb)(a->progress_arg, status, path2);
2720 goto done;
2723 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2724 char *link_target2;
2725 err = got_object_blob_read_to_str(&link_target2, blob2);
2726 if (err)
2727 goto done;
2728 err = merge_symlink(a->worktree, blob1, ondisk_path,
2729 path2, a->label_orig, link_target2, a->commit_id2,
2730 repo, a->progress_cb, a->progress_arg);
2731 free(link_target2);
2732 } else {
2733 err = merge_blob(&local_changes_subsumed, a->worktree,
2734 blob1, ondisk_path, path2, sb.st_mode,
2735 a->label_orig, blob2, a->commit_id2, repo,
2736 a->progress_cb, a->progress_arg);
2738 } else if (blob1) {
2739 ie = got_fileindex_entry_get(a->fileindex, path1,
2740 strlen(path1));
2741 if (ie == NULL)
2742 return (*a->progress_cb)(a->progress_arg,
2743 GOT_STATUS_MISSING, path1);
2745 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2746 path1) == -1)
2747 return got_error_from_errno("asprintf");
2749 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2750 repo);
2751 if (err)
2752 goto done;
2754 switch (status) {
2755 case GOT_STATUS_NO_CHANGE:
2756 err = (*a->progress_cb)(a->progress_arg,
2757 GOT_STATUS_DELETE, path1);
2758 if (err)
2759 goto done;
2760 err = remove_ondisk_file(a->worktree->root_path, path1);
2761 if (err)
2762 goto done;
2763 if (ie)
2764 got_fileindex_entry_mark_deleted_from_disk(ie);
2765 break;
2766 case GOT_STATUS_DELETE:
2767 case GOT_STATUS_MISSING:
2768 err = (*a->progress_cb)(a->progress_arg,
2769 GOT_STATUS_DELETE, path1);
2770 if (err)
2771 goto done;
2772 if (ie)
2773 got_fileindex_entry_mark_deleted_from_disk(ie);
2774 break;
2775 case GOT_STATUS_ADD:
2776 case GOT_STATUS_MODIFY:
2777 case GOT_STATUS_CONFLICT:
2778 err = (*a->progress_cb)(a->progress_arg,
2779 GOT_STATUS_CANNOT_DELETE, path1);
2780 if (err)
2781 goto done;
2782 break;
2783 case GOT_STATUS_OBSTRUCTED:
2784 err = (*a->progress_cb)(a->progress_arg, status, path1);
2785 if (err)
2786 goto done;
2787 break;
2788 default:
2789 break;
2791 } else if (blob2) {
2792 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2793 path2) == -1)
2794 return got_error_from_errno("asprintf");
2795 ie = got_fileindex_entry_get(a->fileindex, path2,
2796 strlen(path2));
2797 if (ie) {
2798 err = get_file_status(&status, &sb, ie, ondisk_path,
2799 -1, NULL, repo);
2800 if (err)
2801 goto done;
2802 if (status != GOT_STATUS_NO_CHANGE &&
2803 status != GOT_STATUS_MODIFY &&
2804 status != GOT_STATUS_CONFLICT &&
2805 status != GOT_STATUS_ADD) {
2806 err = (*a->progress_cb)(a->progress_arg,
2807 status, path2);
2808 goto done;
2810 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2811 char *link_target2;
2812 err = got_object_blob_read_to_str(&link_target2,
2813 blob2);
2814 if (err)
2815 goto done;
2816 err = merge_symlink(a->worktree, NULL,
2817 ondisk_path, path2, a->label_orig,
2818 link_target2, a->commit_id2, repo,
2819 a->progress_cb, a->progress_arg);
2820 free(link_target2);
2821 } else if (S_ISREG(sb.st_mode)) {
2822 err = merge_blob(&local_changes_subsumed,
2823 a->worktree, NULL, ondisk_path, path2,
2824 sb.st_mode, a->label_orig, blob2,
2825 a->commit_id2, repo, a->progress_cb,
2826 a->progress_arg);
2827 } else {
2828 err = got_error_path(ondisk_path,
2829 GOT_ERR_FILE_OBSTRUCTED);
2831 if (err)
2832 goto done;
2833 if (status == GOT_STATUS_DELETE) {
2834 err = got_fileindex_entry_update(ie,
2835 ondisk_path, blob2->id.sha1,
2836 a->worktree->base_commit_id->sha1, 0);
2837 if (err)
2838 goto done;
2840 } else {
2841 int is_bad_symlink = 0;
2842 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2843 if (S_ISLNK(mode2)) {
2844 err = install_symlink(&is_bad_symlink,
2845 a->worktree, ondisk_path, path2, blob2, 0,
2846 0, 1, repo, a->progress_cb, a->progress_arg);
2847 } else {
2848 err = install_blob(a->worktree, ondisk_path, path2,
2849 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2850 a->progress_cb, a->progress_arg);
2852 if (err)
2853 goto done;
2854 err = got_fileindex_entry_alloc(&ie, path2);
2855 if (err)
2856 goto done;
2857 err = got_fileindex_entry_update(ie, ondisk_path,
2858 NULL, NULL, 1);
2859 if (err) {
2860 got_fileindex_entry_free(ie);
2861 goto done;
2863 err = got_fileindex_entry_add(a->fileindex, ie);
2864 if (err) {
2865 got_fileindex_entry_free(ie);
2866 goto done;
2868 if (is_bad_symlink) {
2869 got_fileindex_entry_filetype_set(ie,
2870 GOT_FILEIDX_MODE_BAD_SYMLINK);
2874 done:
2875 free(ondisk_path);
2876 return err;
2879 struct check_merge_ok_arg {
2880 struct got_worktree *worktree;
2881 struct got_repository *repo;
2884 static const struct got_error *
2885 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2887 const struct got_error *err = NULL;
2888 struct check_merge_ok_arg *a = arg;
2889 unsigned char status;
2890 struct stat sb;
2891 char *ondisk_path;
2893 /* Reject merges into a work tree with mixed base commits. */
2894 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2895 SHA1_DIGEST_LENGTH))
2896 return got_error(GOT_ERR_MIXED_COMMITS);
2898 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2899 == -1)
2900 return got_error_from_errno("asprintf");
2902 /* Reject merges into a work tree with conflicted files. */
2903 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2904 if (err)
2905 return err;
2906 if (status == GOT_STATUS_CONFLICT)
2907 return got_error(GOT_ERR_CONFLICTS);
2909 return NULL;
2912 static const struct got_error *
2913 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2914 const char *fileindex_path, struct got_object_id *commit_id1,
2915 struct got_object_id *commit_id2, struct got_repository *repo,
2916 got_worktree_checkout_cb progress_cb, void *progress_arg,
2917 got_cancel_cb cancel_cb, void *cancel_arg)
2919 const struct got_error *err = NULL, *sync_err;
2920 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2921 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2922 struct merge_file_cb_arg arg;
2923 char *label_orig = NULL;
2925 if (commit_id1) {
2926 char *id_str;
2928 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2929 worktree->path_prefix);
2930 if (err)
2931 goto done;
2933 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2934 if (err)
2935 goto done;
2937 err = got_object_id_str(&id_str, commit_id1);
2938 if (err)
2939 goto done;
2941 if (asprintf(&label_orig, "%s: commit %s",
2942 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2943 err = got_error_from_errno("asprintf");
2944 free(id_str);
2945 goto done;
2947 free(id_str);
2950 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2951 worktree->path_prefix);
2952 if (err)
2953 goto done;
2955 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2956 if (err)
2957 goto done;
2959 arg.worktree = worktree;
2960 arg.fileindex = fileindex;
2961 arg.progress_cb = progress_cb;
2962 arg.progress_arg = progress_arg;
2963 arg.cancel_cb = cancel_cb;
2964 arg.cancel_arg = cancel_arg;
2965 arg.label_orig = label_orig;
2966 arg.commit_id2 = commit_id2;
2967 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2968 sync_err = sync_fileindex(fileindex, fileindex_path);
2969 if (sync_err && err == NULL)
2970 err = sync_err;
2971 done:
2972 if (tree1)
2973 got_object_tree_close(tree1);
2974 if (tree2)
2975 got_object_tree_close(tree2);
2976 free(label_orig);
2977 return err;
2980 const struct got_error *
2981 got_worktree_merge_files(struct got_worktree *worktree,
2982 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2983 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2984 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2986 const struct got_error *err, *unlockerr;
2987 char *fileindex_path = NULL;
2988 struct got_fileindex *fileindex = NULL;
2989 struct check_merge_ok_arg mok_arg;
2991 err = lock_worktree(worktree, LOCK_EX);
2992 if (err)
2993 return err;
2995 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2996 if (err)
2997 goto done;
2999 mok_arg.worktree = worktree;
3000 mok_arg.repo = repo;
3001 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3002 &mok_arg);
3003 if (err)
3004 goto done;
3006 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3007 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3008 done:
3009 if (fileindex)
3010 got_fileindex_free(fileindex);
3011 free(fileindex_path);
3012 unlockerr = lock_worktree(worktree, LOCK_SH);
3013 if (unlockerr && err == NULL)
3014 err = unlockerr;
3015 return err;
3018 struct diff_dir_cb_arg {
3019 struct got_fileindex *fileindex;
3020 struct got_worktree *worktree;
3021 const char *status_path;
3022 size_t status_path_len;
3023 struct got_repository *repo;
3024 got_worktree_status_cb status_cb;
3025 void *status_arg;
3026 got_cancel_cb cancel_cb;
3027 void *cancel_arg;
3028 /* A pathlist containing per-directory pathlists of ignore patterns. */
3029 struct got_pathlist_head ignores;
3030 int report_unchanged;
3031 int no_ignores;
3034 static const struct got_error *
3035 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3036 int dirfd, const char *de_name,
3037 got_worktree_status_cb status_cb, void *status_arg,
3038 struct got_repository *repo, int report_unchanged)
3040 const struct got_error *err = NULL;
3041 unsigned char status = GOT_STATUS_NO_CHANGE;
3042 unsigned char staged_status = get_staged_status(ie);
3043 struct stat sb;
3044 struct got_object_id blob_id, commit_id, staged_blob_id;
3045 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3046 struct got_object_id *staged_blob_idp = NULL;
3048 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3049 if (err)
3050 return err;
3052 if (status == GOT_STATUS_NO_CHANGE &&
3053 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3054 return NULL;
3056 if (got_fileindex_entry_has_blob(ie)) {
3057 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3058 blob_idp = &blob_id;
3060 if (got_fileindex_entry_has_commit(ie)) {
3061 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3062 commit_idp = &commit_id;
3064 if (staged_status == GOT_STATUS_ADD ||
3065 staged_status == GOT_STATUS_MODIFY) {
3066 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3067 SHA1_DIGEST_LENGTH);
3068 staged_blob_idp = &staged_blob_id;
3071 return (*status_cb)(status_arg, status, staged_status,
3072 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3075 static const struct got_error *
3076 status_old_new(void *arg, struct got_fileindex_entry *ie,
3077 struct dirent *de, const char *parent_path, int dirfd)
3079 const struct got_error *err = NULL;
3080 struct diff_dir_cb_arg *a = arg;
3081 char *abspath;
3083 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3084 return got_error(GOT_ERR_CANCELLED);
3086 if (got_path_cmp(parent_path, a->status_path,
3087 strlen(parent_path), a->status_path_len) != 0 &&
3088 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3089 return NULL;
3091 if (parent_path[0]) {
3092 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3093 parent_path, de->d_name) == -1)
3094 return got_error_from_errno("asprintf");
3095 } else {
3096 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3097 de->d_name) == -1)
3098 return got_error_from_errno("asprintf");
3101 err = report_file_status(ie, abspath, dirfd, de->d_name,
3102 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3103 free(abspath);
3104 return err;
3107 static const struct got_error *
3108 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3110 struct diff_dir_cb_arg *a = arg;
3111 struct got_object_id blob_id, commit_id;
3112 unsigned char status;
3114 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3115 return got_error(GOT_ERR_CANCELLED);
3117 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3118 return NULL;
3120 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3121 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3122 if (got_fileindex_entry_has_file_on_disk(ie))
3123 status = GOT_STATUS_MISSING;
3124 else
3125 status = GOT_STATUS_DELETE;
3126 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3127 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3130 void
3131 free_ignorelist(struct got_pathlist_head *ignorelist)
3133 struct got_pathlist_entry *pe;
3135 TAILQ_FOREACH(pe, ignorelist, entry)
3136 free((char *)pe->path);
3137 got_pathlist_free(ignorelist);
3140 void
3141 free_ignores(struct got_pathlist_head *ignores)
3143 struct got_pathlist_entry *pe;
3145 TAILQ_FOREACH(pe, ignores, entry) {
3146 struct got_pathlist_head *ignorelist = pe->data;
3147 free_ignorelist(ignorelist);
3148 free((char *)pe->path);
3150 got_pathlist_free(ignores);
3153 static const struct got_error *
3154 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3156 const struct got_error *err = NULL;
3157 struct got_pathlist_entry *pe = NULL;
3158 struct got_pathlist_head *ignorelist;
3159 char *line = NULL, *pattern, *dirpath = NULL;
3160 size_t linesize = 0;
3161 ssize_t linelen;
3163 ignorelist = calloc(1, sizeof(*ignorelist));
3164 if (ignorelist == NULL)
3165 return got_error_from_errno("calloc");
3166 TAILQ_INIT(ignorelist);
3168 while ((linelen = getline(&line, &linesize, f)) != -1) {
3169 if (linelen > 0 && line[linelen - 1] == '\n')
3170 line[linelen - 1] = '\0';
3172 /* Git's ignores may contain comments. */
3173 if (line[0] == '#')
3174 continue;
3176 /* Git's negated patterns are not (yet?) supported. */
3177 if (line[0] == '!')
3178 continue;
3180 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3181 line) == -1) {
3182 err = got_error_from_errno("asprintf");
3183 goto done;
3185 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3186 if (err)
3187 goto done;
3189 if (ferror(f)) {
3190 err = got_error_from_errno("getline");
3191 goto done;
3194 dirpath = strdup(path);
3195 if (dirpath == NULL) {
3196 err = got_error_from_errno("strdup");
3197 goto done;
3199 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3200 done:
3201 free(line);
3202 if (err || pe == NULL) {
3203 free(dirpath);
3204 free_ignorelist(ignorelist);
3206 return err;
3209 int
3210 match_ignores(struct got_pathlist_head *ignores, const char *path)
3212 struct got_pathlist_entry *pe;
3214 /* Handle patterns which match in all directories. */
3215 TAILQ_FOREACH(pe, ignores, entry) {
3216 struct got_pathlist_head *ignorelist = pe->data;
3217 struct got_pathlist_entry *pi;
3219 TAILQ_FOREACH(pi, ignorelist, entry) {
3220 const char *p, *pattern = pi->path;
3222 if (strncmp(pattern, "**/", 3) != 0)
3223 continue;
3224 pattern += 3;
3225 p = path;
3226 while (*p) {
3227 if (fnmatch(pattern, p,
3228 FNM_PATHNAME | FNM_LEADING_DIR)) {
3229 /* Retry in next directory. */
3230 while (*p && *p != '/')
3231 p++;
3232 while (*p == '/')
3233 p++;
3234 continue;
3236 return 1;
3242 * The ignores pathlist contains ignore lists from children before
3243 * parents, so we can find the most specific ignorelist by walking
3244 * ignores backwards.
3246 pe = TAILQ_LAST(ignores, got_pathlist_head);
3247 while (pe) {
3248 if (got_path_is_child(path, pe->path, pe->path_len)) {
3249 struct got_pathlist_head *ignorelist = pe->data;
3250 struct got_pathlist_entry *pi;
3251 TAILQ_FOREACH(pi, ignorelist, entry) {
3252 const char *pattern = pi->path;
3253 int flags = FNM_LEADING_DIR;
3254 if (strstr(pattern, "/**/") == NULL)
3255 flags |= FNM_PATHNAME;
3256 if (fnmatch(pattern, path, flags))
3257 continue;
3258 return 1;
3261 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3264 return 0;
3267 static const struct got_error *
3268 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3269 const char *path, int dirfd, const char *ignores_filename)
3271 const struct got_error *err = NULL;
3272 char *ignorespath;
3273 int fd = -1;
3274 FILE *ignoresfile = NULL;
3276 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3277 path[0] ? "/" : "", ignores_filename) == -1)
3278 return got_error_from_errno("asprintf");
3280 if (dirfd != -1) {
3281 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3282 if (fd == -1) {
3283 if (errno != ENOENT && errno != EACCES)
3284 err = got_error_from_errno2("openat",
3285 ignorespath);
3286 } else {
3287 ignoresfile = fdopen(fd, "r");
3288 if (ignoresfile == NULL)
3289 err = got_error_from_errno2("fdopen",
3290 ignorespath);
3291 else {
3292 fd = -1;
3293 err = read_ignores(ignores, path, ignoresfile);
3296 } else {
3297 ignoresfile = fopen(ignorespath, "r");
3298 if (ignoresfile == NULL) {
3299 if (errno != ENOENT && errno != EACCES)
3300 err = got_error_from_errno2("fopen",
3301 ignorespath);
3302 } else
3303 err = read_ignores(ignores, path, ignoresfile);
3306 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3307 err = got_error_from_errno2("fclose", path);
3308 if (fd != -1 && close(fd) == -1 && err == NULL)
3309 err = got_error_from_errno2("close", path);
3310 free(ignorespath);
3311 return err;
3314 static const struct got_error *
3315 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3317 const struct got_error *err = NULL;
3318 struct diff_dir_cb_arg *a = arg;
3319 char *path = NULL;
3321 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3322 return got_error(GOT_ERR_CANCELLED);
3324 if (parent_path[0]) {
3325 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3326 return got_error_from_errno("asprintf");
3327 } else {
3328 path = de->d_name;
3331 if (de->d_type != DT_DIR &&
3332 got_path_is_child(path, a->status_path, a->status_path_len)
3333 && !match_ignores(&a->ignores, path))
3334 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3335 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3336 if (parent_path[0])
3337 free(path);
3338 return err;
3341 static const struct got_error *
3342 status_traverse(void *arg, const char *path, int dirfd)
3344 const struct got_error *err = NULL;
3345 struct diff_dir_cb_arg *a = arg;
3347 if (a->no_ignores)
3348 return NULL;
3350 err = add_ignores(&a->ignores, a->worktree->root_path,
3351 path, dirfd, ".cvsignore");
3352 if (err)
3353 return err;
3355 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3356 dirfd, ".gitignore");
3358 return err;
3361 static const struct got_error *
3362 report_single_file_status(const char *path, const char *ondisk_path,
3363 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3364 void *status_arg, struct got_repository *repo, int report_unchanged)
3366 struct got_fileindex_entry *ie;
3367 struct stat sb;
3369 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3370 if (ie)
3371 return report_file_status(ie, ondisk_path, -1, NULL,
3372 status_cb, status_arg, repo, report_unchanged);
3374 if (lstat(ondisk_path, &sb) == -1) {
3375 if (errno != ENOENT)
3376 return got_error_from_errno2("lstat", ondisk_path);
3377 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3378 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3379 return NULL;
3382 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3383 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3384 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3386 return NULL;
3389 static const struct got_error *
3390 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3391 const char *root_path, const char *path)
3393 const struct got_error *err;
3394 char *parent_path, *next_parent_path = NULL;
3396 err = add_ignores(ignores, root_path, "", -1,
3397 ".cvsignore");
3398 if (err)
3399 return err;
3401 err = add_ignores(ignores, root_path, "", -1,
3402 ".gitignore");
3403 if (err)
3404 return err;
3406 err = got_path_dirname(&parent_path, path);
3407 if (err) {
3408 if (err->code == GOT_ERR_BAD_PATH)
3409 return NULL; /* cannot traverse parent */
3410 return err;
3412 for (;;) {
3413 err = add_ignores(ignores, root_path, parent_path, -1,
3414 ".cvsignore");
3415 if (err)
3416 break;
3417 err = add_ignores(ignores, root_path, parent_path, -1,
3418 ".gitignore");
3419 if (err)
3420 break;
3421 err = got_path_dirname(&next_parent_path, parent_path);
3422 if (err) {
3423 if (err->code == GOT_ERR_BAD_PATH)
3424 err = NULL; /* traversed everything */
3425 break;
3427 free(parent_path);
3428 parent_path = next_parent_path;
3429 next_parent_path = NULL;
3432 free(parent_path);
3433 free(next_parent_path);
3434 return err;
3437 static const struct got_error *
3438 worktree_status(struct got_worktree *worktree, const char *path,
3439 struct got_fileindex *fileindex, struct got_repository *repo,
3440 got_worktree_status_cb status_cb, void *status_arg,
3441 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3442 int report_unchanged)
3444 const struct got_error *err = NULL;
3445 int fd = -1;
3446 struct got_fileindex_diff_dir_cb fdiff_cb;
3447 struct diff_dir_cb_arg arg;
3448 char *ondisk_path = NULL;
3450 TAILQ_INIT(&arg.ignores);
3452 if (asprintf(&ondisk_path, "%s%s%s",
3453 worktree->root_path, path[0] ? "/" : "", path) == -1)
3454 return got_error_from_errno("asprintf");
3456 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3457 if (fd == -1) {
3458 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3459 errno != ELOOP)
3460 err = got_error_from_errno2("open", ondisk_path);
3461 else
3462 err = report_single_file_status(path, ondisk_path,
3463 fileindex, status_cb, status_arg, repo,
3464 report_unchanged);
3465 } else {
3466 fdiff_cb.diff_old_new = status_old_new;
3467 fdiff_cb.diff_old = status_old;
3468 fdiff_cb.diff_new = status_new;
3469 fdiff_cb.diff_traverse = status_traverse;
3470 arg.fileindex = fileindex;
3471 arg.worktree = worktree;
3472 arg.status_path = path;
3473 arg.status_path_len = strlen(path);
3474 arg.repo = repo;
3475 arg.status_cb = status_cb;
3476 arg.status_arg = status_arg;
3477 arg.cancel_cb = cancel_cb;
3478 arg.cancel_arg = cancel_arg;
3479 arg.report_unchanged = report_unchanged;
3480 arg.no_ignores = no_ignores;
3481 if (!no_ignores) {
3482 err = add_ignores_from_parent_paths(&arg.ignores,
3483 worktree->root_path, path);
3484 if (err)
3485 goto done;
3487 err = got_fileindex_diff_dir(fileindex, fd,
3488 worktree->root_path, path, repo, &fdiff_cb, &arg);
3490 done:
3491 free_ignores(&arg.ignores);
3492 if (fd != -1 && close(fd) != 0 && err == NULL)
3493 err = got_error_from_errno("close");
3494 free(ondisk_path);
3495 return err;
3498 const struct got_error *
3499 got_worktree_status(struct got_worktree *worktree,
3500 struct got_pathlist_head *paths, struct got_repository *repo,
3501 got_worktree_status_cb status_cb, void *status_arg,
3502 got_cancel_cb cancel_cb, void *cancel_arg)
3504 const struct got_error *err = NULL;
3505 char *fileindex_path = NULL;
3506 struct got_fileindex *fileindex = NULL;
3507 struct got_pathlist_entry *pe;
3509 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3510 if (err)
3511 return err;
3513 TAILQ_FOREACH(pe, paths, entry) {
3514 err = worktree_status(worktree, pe->path, fileindex, repo,
3515 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3516 if (err)
3517 break;
3519 free(fileindex_path);
3520 got_fileindex_free(fileindex);
3521 return err;
3524 const struct got_error *
3525 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3526 const char *arg)
3528 const struct got_error *err = NULL;
3529 char *resolved = NULL, *cwd = NULL, *path = NULL;
3530 size_t len;
3531 struct stat sb;
3533 *wt_path = NULL;
3535 cwd = getcwd(NULL, 0);
3536 if (cwd == NULL)
3537 return got_error_from_errno("getcwd");
3539 if (lstat(arg, &sb) == -1) {
3540 if (errno != ENOENT) {
3541 err = got_error_from_errno2("lstat", arg);
3542 goto done;
3545 if (S_ISLNK(sb.st_mode)) {
3547 * We cannot use realpath(3) with symlinks since we want to
3548 * operate on the symlink itself.
3549 * But we can make the path absolute, assuming it is relative
3550 * to the current working directory, and then canonicalize it.
3552 char *abspath = NULL;
3553 char canonpath[PATH_MAX];
3554 if (!got_path_is_absolute(arg)) {
3555 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3556 err = got_error_from_errno("asprintf");
3557 goto done;
3561 err = got_canonpath(abspath ? abspath : arg, canonpath,
3562 sizeof(canonpath));
3563 if (err)
3564 goto done;
3565 resolved = strdup(canonpath);
3566 if (resolved == NULL) {
3567 err = got_error_from_errno("strdup");
3568 goto done;
3570 } else {
3571 resolved = realpath(arg, NULL);
3572 if (resolved == NULL) {
3573 if (errno != ENOENT) {
3574 err = got_error_from_errno2("realpath", arg);
3575 goto done;
3577 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3578 err = got_error_from_errno("asprintf");
3579 goto done;
3584 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3585 strlen(got_worktree_get_root_path(worktree)))) {
3586 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3587 goto done;
3590 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3591 err = got_path_skip_common_ancestor(&path,
3592 got_worktree_get_root_path(worktree), resolved);
3593 if (err)
3594 goto done;
3595 } else {
3596 path = strdup("");
3597 if (path == NULL) {
3598 err = got_error_from_errno("strdup");
3599 goto done;
3603 /* XXX status walk can't deal with trailing slash! */
3604 len = strlen(path);
3605 while (len > 0 && path[len - 1] == '/') {
3606 path[len - 1] = '\0';
3607 len--;
3609 done:
3610 free(resolved);
3611 free(cwd);
3612 if (err == NULL)
3613 *wt_path = path;
3614 else
3615 free(path);
3616 return err;
3619 struct schedule_addition_args {
3620 struct got_worktree *worktree;
3621 struct got_fileindex *fileindex;
3622 got_worktree_checkout_cb progress_cb;
3623 void *progress_arg;
3624 struct got_repository *repo;
3627 static const struct got_error *
3628 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3629 const char *relpath, struct got_object_id *blob_id,
3630 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3631 int dirfd, const char *de_name)
3633 struct schedule_addition_args *a = arg;
3634 const struct got_error *err = NULL;
3635 struct got_fileindex_entry *ie;
3636 struct stat sb;
3637 char *ondisk_path;
3639 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3640 relpath) == -1)
3641 return got_error_from_errno("asprintf");
3643 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3644 if (ie) {
3645 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3646 de_name, a->repo);
3647 if (err)
3648 goto done;
3649 /* Re-adding an existing entry is a no-op. */
3650 if (status == GOT_STATUS_ADD)
3651 goto done;
3652 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3653 if (err)
3654 goto done;
3657 if (status != GOT_STATUS_UNVERSIONED) {
3658 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3659 goto done;
3662 err = got_fileindex_entry_alloc(&ie, relpath);
3663 if (err)
3664 goto done;
3665 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3666 if (err) {
3667 got_fileindex_entry_free(ie);
3668 goto done;
3670 err = got_fileindex_entry_add(a->fileindex, ie);
3671 if (err) {
3672 got_fileindex_entry_free(ie);
3673 goto done;
3675 done:
3676 free(ondisk_path);
3677 if (err)
3678 return err;
3679 if (status == GOT_STATUS_ADD)
3680 return NULL;
3681 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3684 const struct got_error *
3685 got_worktree_schedule_add(struct got_worktree *worktree,
3686 struct got_pathlist_head *paths,
3687 got_worktree_checkout_cb progress_cb, void *progress_arg,
3688 struct got_repository *repo, int no_ignores)
3690 struct got_fileindex *fileindex = NULL;
3691 char *fileindex_path = NULL;
3692 const struct got_error *err = NULL, *sync_err, *unlockerr;
3693 struct got_pathlist_entry *pe;
3694 struct schedule_addition_args saa;
3696 err = lock_worktree(worktree, LOCK_EX);
3697 if (err)
3698 return err;
3700 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3701 if (err)
3702 goto done;
3704 saa.worktree = worktree;
3705 saa.fileindex = fileindex;
3706 saa.progress_cb = progress_cb;
3707 saa.progress_arg = progress_arg;
3708 saa.repo = repo;
3710 TAILQ_FOREACH(pe, paths, entry) {
3711 err = worktree_status(worktree, pe->path, fileindex, repo,
3712 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3713 if (err)
3714 break;
3716 sync_err = sync_fileindex(fileindex, fileindex_path);
3717 if (sync_err && err == NULL)
3718 err = sync_err;
3719 done:
3720 free(fileindex_path);
3721 if (fileindex)
3722 got_fileindex_free(fileindex);
3723 unlockerr = lock_worktree(worktree, LOCK_SH);
3724 if (unlockerr && err == NULL)
3725 err = unlockerr;
3726 return err;
3729 struct schedule_deletion_args {
3730 struct got_worktree *worktree;
3731 struct got_fileindex *fileindex;
3732 got_worktree_delete_cb progress_cb;
3733 void *progress_arg;
3734 struct got_repository *repo;
3735 int delete_local_mods;
3736 int keep_on_disk;
3739 static const struct got_error *
3740 schedule_for_deletion(void *arg, unsigned char status,
3741 unsigned char staged_status, const char *relpath,
3742 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3743 struct got_object_id *commit_id, int dirfd, const char *de_name)
3745 struct schedule_deletion_args *a = arg;
3746 const struct got_error *err = NULL;
3747 struct got_fileindex_entry *ie = NULL;
3748 struct stat sb;
3749 char *ondisk_path, *parent = NULL;
3751 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3752 if (ie == NULL)
3753 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3755 staged_status = get_staged_status(ie);
3756 if (staged_status != GOT_STATUS_NO_CHANGE) {
3757 if (staged_status == GOT_STATUS_DELETE)
3758 return NULL;
3759 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3762 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3763 relpath) == -1)
3764 return got_error_from_errno("asprintf");
3766 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3767 a->repo);
3768 if (err)
3769 goto done;
3771 if (status != GOT_STATUS_NO_CHANGE) {
3772 if (status == GOT_STATUS_DELETE)
3773 goto done;
3774 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3775 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3776 goto done;
3778 if (status != GOT_STATUS_MODIFY &&
3779 status != GOT_STATUS_MISSING) {
3780 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3781 goto done;
3785 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3786 if (dirfd != -1) {
3787 if (unlinkat(dirfd, de_name, 0) != 0) {
3788 err = got_error_from_errno2("unlinkat",
3789 ondisk_path);
3790 goto done;
3792 } else if (unlink(ondisk_path) != 0) {
3793 err = got_error_from_errno2("unlink", ondisk_path);
3794 goto done;
3797 parent = dirname(ondisk_path);
3799 if (parent == NULL) {
3800 err = got_error_from_errno2("dirname", ondisk_path);
3801 goto done;
3803 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3804 if (rmdir(parent) == -1) {
3805 if (errno != ENOTEMPTY)
3806 err = got_error_from_errno2("rmdir",
3807 parent);
3808 break;
3810 parent = dirname(parent);
3811 if (parent == NULL) {
3812 err = got_error_from_errno2("dirname", parent);
3813 goto done;
3818 got_fileindex_entry_mark_deleted_from_disk(ie);
3819 done:
3820 free(ondisk_path);
3821 if (err)
3822 return err;
3823 if (status == GOT_STATUS_DELETE)
3824 return NULL;
3825 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3826 staged_status, relpath);
3829 const struct got_error *
3830 got_worktree_schedule_delete(struct got_worktree *worktree,
3831 struct got_pathlist_head *paths, int delete_local_mods,
3832 got_worktree_delete_cb progress_cb, void *progress_arg,
3833 struct got_repository *repo, int keep_on_disk)
3835 struct got_fileindex *fileindex = NULL;
3836 char *fileindex_path = NULL;
3837 const struct got_error *err = NULL, *sync_err, *unlockerr;
3838 struct got_pathlist_entry *pe;
3839 struct schedule_deletion_args sda;
3841 err = lock_worktree(worktree, LOCK_EX);
3842 if (err)
3843 return err;
3845 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3846 if (err)
3847 goto done;
3849 sda.worktree = worktree;
3850 sda.fileindex = fileindex;
3851 sda.progress_cb = progress_cb;
3852 sda.progress_arg = progress_arg;
3853 sda.repo = repo;
3854 sda.delete_local_mods = delete_local_mods;
3855 sda.keep_on_disk = keep_on_disk;
3857 TAILQ_FOREACH(pe, paths, entry) {
3858 err = worktree_status(worktree, pe->path, fileindex, repo,
3859 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3860 if (err)
3861 break;
3863 sync_err = sync_fileindex(fileindex, fileindex_path);
3864 if (sync_err && err == NULL)
3865 err = sync_err;
3866 done:
3867 free(fileindex_path);
3868 if (fileindex)
3869 got_fileindex_free(fileindex);
3870 unlockerr = lock_worktree(worktree, LOCK_SH);
3871 if (unlockerr && err == NULL)
3872 err = unlockerr;
3873 return err;
3876 static const struct got_error *
3877 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3879 const struct got_error *err = NULL;
3880 char *line = NULL;
3881 size_t linesize = 0, n;
3882 ssize_t linelen;
3884 linelen = getline(&line, &linesize, infile);
3885 if (linelen == -1) {
3886 if (ferror(infile)) {
3887 err = got_error_from_errno("getline");
3888 goto done;
3890 return NULL;
3892 if (outfile) {
3893 n = fwrite(line, 1, linelen, outfile);
3894 if (n != linelen) {
3895 err = got_ferror(outfile, GOT_ERR_IO);
3896 goto done;
3899 if (rejectfile) {
3900 n = fwrite(line, 1, linelen, rejectfile);
3901 if (n != linelen)
3902 err = got_ferror(outfile, GOT_ERR_IO);
3904 done:
3905 free(line);
3906 return err;
3909 static const struct got_error *
3910 skip_one_line(FILE *f)
3912 char *line = NULL;
3913 size_t linesize = 0;
3914 ssize_t linelen;
3916 linelen = getline(&line, &linesize, f);
3917 if (linelen == -1) {
3918 if (ferror(f))
3919 return got_error_from_errno("getline");
3920 return NULL;
3922 free(line);
3923 return NULL;
3926 static const struct got_error *
3927 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3928 int start_old, int end_old, int start_new, int end_new,
3929 FILE *outfile, FILE *rejectfile)
3931 const struct got_error *err;
3933 /* Copy old file's lines leading up to patch. */
3934 while (!feof(f1) && *line_cur1 < start_old) {
3935 err = copy_one_line(f1, outfile, NULL);
3936 if (err)
3937 return err;
3938 (*line_cur1)++;
3940 /* Skip new file's lines leading up to patch. */
3941 while (!feof(f2) && *line_cur2 < start_new) {
3942 if (rejectfile)
3943 err = copy_one_line(f2, NULL, rejectfile);
3944 else
3945 err = skip_one_line(f2);
3946 if (err)
3947 return err;
3948 (*line_cur2)++;
3950 /* Copy patched lines. */
3951 while (!feof(f2) && *line_cur2 <= end_new) {
3952 err = copy_one_line(f2, outfile, NULL);
3953 if (err)
3954 return err;
3955 (*line_cur2)++;
3957 /* Skip over old file's replaced lines. */
3958 while (!feof(f1) && *line_cur1 <= end_old) {
3959 if (rejectfile)
3960 err = copy_one_line(f1, NULL, rejectfile);
3961 else
3962 err = skip_one_line(f1);
3963 if (err)
3964 return err;
3965 (*line_cur1)++;
3968 return NULL;
3971 static const struct got_error *
3972 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3973 FILE *outfile, FILE *rejectfile)
3975 const struct got_error *err;
3977 if (outfile) {
3978 /* Copy old file's lines until EOF. */
3979 while (!feof(f1)) {
3980 err = copy_one_line(f1, outfile, NULL);
3981 if (err)
3982 return err;
3983 (*line_cur1)++;
3986 if (rejectfile) {
3987 /* Copy new file's lines until EOF. */
3988 while (!feof(f2)) {
3989 err = copy_one_line(f2, NULL, rejectfile);
3990 if (err)
3991 return err;
3992 (*line_cur2)++;
3996 return NULL;
3999 static const struct got_error *
4000 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4001 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4002 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4003 int *line_cur2, FILE *outfile, FILE *rejectfile,
4004 got_worktree_patch_cb patch_cb, void *patch_arg)
4006 const struct got_error *err = NULL;
4007 int start_old = change->cv.a;
4008 int end_old = change->cv.b;
4009 int start_new = change->cv.c;
4010 int end_new = change->cv.d;
4011 long pos1, pos2;
4012 FILE *hunkfile;
4014 *choice = GOT_PATCH_CHOICE_NONE;
4016 hunkfile = got_opentemp();
4017 if (hunkfile == NULL)
4018 return got_error_from_errno("got_opentemp");
4020 pos1 = ftell(f1);
4021 pos2 = ftell(f2);
4023 /* XXX TODO needs error checking */
4024 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4026 if (fseek(f1, pos1, SEEK_SET) == -1) {
4027 err = got_ferror(f1, GOT_ERR_IO);
4028 goto done;
4030 if (fseek(f2, pos2, SEEK_SET) == -1) {
4031 err = got_ferror(f1, GOT_ERR_IO);
4032 goto done;
4034 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4035 err = got_ferror(hunkfile, GOT_ERR_IO);
4036 goto done;
4039 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4040 hunkfile, n, nchanges);
4041 if (err)
4042 goto done;
4044 switch (*choice) {
4045 case GOT_PATCH_CHOICE_YES:
4046 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4047 end_old, start_new, end_new, outfile, rejectfile);
4048 break;
4049 case GOT_PATCH_CHOICE_NO:
4050 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4051 end_old, start_new, end_new, rejectfile, outfile);
4052 break;
4053 case GOT_PATCH_CHOICE_QUIT:
4054 break;
4055 default:
4056 err = got_error(GOT_ERR_PATCH_CHOICE);
4057 break;
4059 done:
4060 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4061 err = got_error_from_errno("fclose");
4062 return err;
4065 struct revert_file_args {
4066 struct got_worktree *worktree;
4067 struct got_fileindex *fileindex;
4068 got_worktree_checkout_cb progress_cb;
4069 void *progress_arg;
4070 got_worktree_patch_cb patch_cb;
4071 void *patch_arg;
4072 struct got_repository *repo;
4075 static const struct got_error *
4076 create_patched_content(char **path_outfile, int reverse_patch,
4077 struct got_object_id *blob_id, const char *path2,
4078 int dirfd2, const char *de_name2,
4079 const char *relpath, struct got_repository *repo,
4080 got_worktree_patch_cb patch_cb, void *patch_arg)
4082 const struct got_error *err;
4083 struct got_blob_object *blob = NULL;
4084 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4085 int fd2 = -1;
4086 char link_target[PATH_MAX];
4087 ssize_t link_len = 0;
4088 char *path1 = NULL, *id_str = NULL;
4089 struct stat sb1, sb2;
4090 struct got_diff_changes *changes = NULL;
4091 struct got_diff_state *ds = NULL;
4092 struct got_diff_args *args = NULL;
4093 struct got_diff_change *change;
4094 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4095 int n = 0;
4097 *path_outfile = NULL;
4099 err = got_object_id_str(&id_str, blob_id);
4100 if (err)
4101 return err;
4103 if (dirfd2 != -1) {
4104 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4105 if (fd2 == -1) {
4106 if (errno != ELOOP) {
4107 err = got_error_from_errno2("openat", path2);
4108 goto done;
4110 link_len = readlinkat(dirfd2, de_name2,
4111 link_target, sizeof(link_target));
4112 if (link_len == -1)
4113 return got_error_from_errno2("readlinkat", path2);
4114 sb2.st_mode = S_IFLNK;
4115 sb2.st_size = link_len;
4117 } else {
4118 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4119 if (fd2 == -1) {
4120 if (errno != ELOOP) {
4121 err = got_error_from_errno2("open", path2);
4122 goto done;
4124 link_len = readlink(path2, link_target,
4125 sizeof(link_target));
4126 if (link_len == -1)
4127 return got_error_from_errno2("readlink", path2);
4128 sb2.st_mode = S_IFLNK;
4129 sb2.st_size = link_len;
4132 if (fd2 != -1) {
4133 if (fstat(fd2, &sb2) == -1) {
4134 err = got_error_from_errno2("fstat", path2);
4135 goto done;
4138 f2 = fdopen(fd2, "r");
4139 if (f2 == NULL) {
4140 err = got_error_from_errno2("fdopen", path2);
4141 goto done;
4143 fd2 = -1;
4144 } else {
4145 size_t n;
4146 f2 = got_opentemp();
4147 if (f2 == NULL) {
4148 err = got_error_from_errno2("got_opentemp", path2);
4149 goto done;
4151 n = fwrite(link_target, 1, link_len, f2);
4152 if (n != link_len) {
4153 err = got_ferror(f2, GOT_ERR_IO);
4154 goto done;
4156 if (fflush(f2) == EOF) {
4157 err = got_error_from_errno("fflush");
4158 goto done;
4160 rewind(f2);
4163 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4164 if (err)
4165 goto done;
4167 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4168 if (err)
4169 goto done;
4171 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4172 if (err)
4173 goto done;
4175 if (stat(path1, &sb1) == -1) {
4176 err = got_error_from_errno2("stat", path1);
4177 goto done;
4180 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4181 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4182 if (err)
4183 goto done;
4185 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4186 if (err)
4187 goto done;
4189 if (fseek(f1, 0L, SEEK_SET) == -1)
4190 return got_ferror(f1, GOT_ERR_IO);
4191 if (fseek(f2, 0L, SEEK_SET) == -1)
4192 return got_ferror(f2, GOT_ERR_IO);
4193 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4194 int choice;
4195 err = apply_or_reject_change(&choice, change, ++n,
4196 changes->nchanges, ds, args, diff_flags, relpath,
4197 f1, f2, &line_cur1, &line_cur2,
4198 reverse_patch ? NULL : outfile,
4199 reverse_patch ? outfile : NULL,
4200 patch_cb, patch_arg);
4201 if (err)
4202 goto done;
4203 if (choice == GOT_PATCH_CHOICE_YES)
4204 have_content = 1;
4205 else if (choice == GOT_PATCH_CHOICE_QUIT)
4206 break;
4208 if (have_content) {
4209 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4210 reverse_patch ? NULL : outfile,
4211 reverse_patch ? outfile : NULL);
4212 if (err)
4213 goto done;
4215 if (!S_ISLNK(sb2.st_mode)) {
4216 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4217 err = got_error_from_errno2("chmod", path2);
4218 goto done;
4222 done:
4223 free(id_str);
4224 if (blob)
4225 got_object_blob_close(blob);
4226 if (f1 && fclose(f1) == EOF && err == NULL)
4227 err = got_error_from_errno2("fclose", path1);
4228 if (f2 && fclose(f2) == EOF && err == NULL)
4229 err = got_error_from_errno2("fclose", path2);
4230 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4231 err = got_error_from_errno2("close", path2);
4232 if (outfile && fclose(outfile) == EOF && err == NULL)
4233 err = got_error_from_errno2("fclose", *path_outfile);
4234 if (path1 && unlink(path1) == -1 && err == NULL)
4235 err = got_error_from_errno2("unlink", path1);
4236 if (err || !have_content) {
4237 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4238 err = got_error_from_errno2("unlink", *path_outfile);
4239 free(*path_outfile);
4240 *path_outfile = NULL;
4242 free(args);
4243 if (ds) {
4244 got_diff_state_free(ds);
4245 free(ds);
4247 if (changes)
4248 got_diff_free_changes(changes);
4249 free(path1);
4250 return err;
4253 static const struct got_error *
4254 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4255 const char *relpath, struct got_object_id *blob_id,
4256 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4257 int dirfd, const char *de_name)
4259 struct revert_file_args *a = arg;
4260 const struct got_error *err = NULL;
4261 char *parent_path = NULL;
4262 struct got_fileindex_entry *ie;
4263 struct got_tree_object *tree = NULL;
4264 struct got_object_id *tree_id = NULL;
4265 const struct got_tree_entry *te = NULL;
4266 char *tree_path = NULL, *te_name;
4267 char *ondisk_path = NULL, *path_content = NULL;
4268 struct got_blob_object *blob = NULL;
4270 /* Reverting a staged deletion is a no-op. */
4271 if (status == GOT_STATUS_DELETE &&
4272 staged_status != GOT_STATUS_NO_CHANGE)
4273 return NULL;
4275 if (status == GOT_STATUS_UNVERSIONED)
4276 return (*a->progress_cb)(a->progress_arg,
4277 GOT_STATUS_UNVERSIONED, relpath);
4279 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4280 if (ie == NULL)
4281 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4283 /* Construct in-repository path of tree which contains this blob. */
4284 err = got_path_dirname(&parent_path, ie->path);
4285 if (err) {
4286 if (err->code != GOT_ERR_BAD_PATH)
4287 goto done;
4288 parent_path = strdup("/");
4289 if (parent_path == NULL) {
4290 err = got_error_from_errno("strdup");
4291 goto done;
4294 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4295 tree_path = strdup(parent_path);
4296 if (tree_path == NULL) {
4297 err = got_error_from_errno("strdup");
4298 goto done;
4300 } else {
4301 if (got_path_is_root_dir(parent_path)) {
4302 tree_path = strdup(a->worktree->path_prefix);
4303 if (tree_path == NULL) {
4304 err = got_error_from_errno("strdup");
4305 goto done;
4307 } else {
4308 if (asprintf(&tree_path, "%s/%s",
4309 a->worktree->path_prefix, parent_path) == -1) {
4310 err = got_error_from_errno("asprintf");
4311 goto done;
4316 err = got_object_id_by_path(&tree_id, a->repo,
4317 a->worktree->base_commit_id, tree_path);
4318 if (err) {
4319 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4320 (status == GOT_STATUS_ADD ||
4321 staged_status == GOT_STATUS_ADD)))
4322 goto done;
4323 } else {
4324 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4325 if (err)
4326 goto done;
4328 te_name = basename(ie->path);
4329 if (te_name == NULL) {
4330 err = got_error_from_errno2("basename", ie->path);
4331 goto done;
4334 te = got_object_tree_find_entry(tree, te_name);
4335 if (te == NULL && status != GOT_STATUS_ADD &&
4336 staged_status != GOT_STATUS_ADD) {
4337 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4338 goto done;
4342 switch (status) {
4343 case GOT_STATUS_ADD:
4344 if (a->patch_cb) {
4345 int choice = GOT_PATCH_CHOICE_NONE;
4346 err = (*a->patch_cb)(&choice, a->patch_arg,
4347 status, ie->path, NULL, 1, 1);
4348 if (err)
4349 goto done;
4350 if (choice != GOT_PATCH_CHOICE_YES)
4351 break;
4353 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4354 ie->path);
4355 if (err)
4356 goto done;
4357 got_fileindex_entry_remove(a->fileindex, ie);
4358 break;
4359 case GOT_STATUS_DELETE:
4360 if (a->patch_cb) {
4361 int choice = GOT_PATCH_CHOICE_NONE;
4362 err = (*a->patch_cb)(&choice, a->patch_arg,
4363 status, ie->path, NULL, 1, 1);
4364 if (err)
4365 goto done;
4366 if (choice != GOT_PATCH_CHOICE_YES)
4367 break;
4369 /* fall through */
4370 case GOT_STATUS_MODIFY:
4371 case GOT_STATUS_MODE_CHANGE:
4372 case GOT_STATUS_CONFLICT:
4373 case GOT_STATUS_MISSING: {
4374 struct got_object_id id;
4375 if (staged_status == GOT_STATUS_ADD ||
4376 staged_status == GOT_STATUS_MODIFY) {
4377 memcpy(id.sha1, ie->staged_blob_sha1,
4378 SHA1_DIGEST_LENGTH);
4379 } else
4380 memcpy(id.sha1, ie->blob_sha1,
4381 SHA1_DIGEST_LENGTH);
4382 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4383 if (err)
4384 goto done;
4386 if (asprintf(&ondisk_path, "%s/%s",
4387 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4388 err = got_error_from_errno("asprintf");
4389 goto done;
4392 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4393 status == GOT_STATUS_CONFLICT)) {
4394 int is_bad_symlink = 0;
4395 err = create_patched_content(&path_content, 1, &id,
4396 ondisk_path, dirfd, de_name, ie->path, a->repo,
4397 a->patch_cb, a->patch_arg);
4398 if (err || path_content == NULL)
4399 break;
4400 if (te && S_ISLNK(te->mode)) {
4401 if (unlink(path_content) == -1) {
4402 err = got_error_from_errno2("unlink",
4403 path_content);
4404 break;
4406 err = install_symlink(&is_bad_symlink,
4407 a->worktree, ondisk_path, ie->path,
4408 blob, 0, 1, 0, a->repo,
4409 a->progress_cb, a->progress_arg);
4410 } else {
4411 if (rename(path_content, ondisk_path) == -1) {
4412 err = got_error_from_errno3("rename",
4413 path_content, ondisk_path);
4414 goto done;
4417 } else {
4418 int is_bad_symlink = 0;
4419 if (te && S_ISLNK(te->mode)) {
4420 err = install_symlink(&is_bad_symlink,
4421 a->worktree, ondisk_path, ie->path,
4422 blob, 0, 1, 0, a->repo,
4423 a->progress_cb, a->progress_arg);
4424 } else {
4425 err = install_blob(a->worktree, ondisk_path,
4426 ie->path,
4427 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4428 got_fileindex_perms_to_st(ie), blob,
4429 0, 1, 0, 0, a->repo,
4430 a->progress_cb, a->progress_arg);
4432 if (err)
4433 goto done;
4434 if (status == GOT_STATUS_DELETE ||
4435 status == GOT_STATUS_MODE_CHANGE) {
4436 err = got_fileindex_entry_update(ie,
4437 ondisk_path, blob->id.sha1,
4438 a->worktree->base_commit_id->sha1, 1);
4439 if (err)
4440 goto done;
4442 if (is_bad_symlink) {
4443 got_fileindex_entry_filetype_set(ie,
4444 GOT_FILEIDX_MODE_BAD_SYMLINK);
4447 break;
4449 default:
4450 break;
4452 done:
4453 free(ondisk_path);
4454 free(path_content);
4455 free(parent_path);
4456 free(tree_path);
4457 if (blob)
4458 got_object_blob_close(blob);
4459 if (tree)
4460 got_object_tree_close(tree);
4461 free(tree_id);
4462 return err;
4465 const struct got_error *
4466 got_worktree_revert(struct got_worktree *worktree,
4467 struct got_pathlist_head *paths,
4468 got_worktree_checkout_cb progress_cb, void *progress_arg,
4469 got_worktree_patch_cb patch_cb, void *patch_arg,
4470 struct got_repository *repo)
4472 struct got_fileindex *fileindex = NULL;
4473 char *fileindex_path = NULL;
4474 const struct got_error *err = NULL, *unlockerr = NULL;
4475 const struct got_error *sync_err = NULL;
4476 struct got_pathlist_entry *pe;
4477 struct revert_file_args rfa;
4479 err = lock_worktree(worktree, LOCK_EX);
4480 if (err)
4481 return err;
4483 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4484 if (err)
4485 goto done;
4487 rfa.worktree = worktree;
4488 rfa.fileindex = fileindex;
4489 rfa.progress_cb = progress_cb;
4490 rfa.progress_arg = progress_arg;
4491 rfa.patch_cb = patch_cb;
4492 rfa.patch_arg = patch_arg;
4493 rfa.repo = repo;
4494 TAILQ_FOREACH(pe, paths, entry) {
4495 err = worktree_status(worktree, pe->path, fileindex, repo,
4496 revert_file, &rfa, NULL, NULL, 0, 0);
4497 if (err)
4498 break;
4500 sync_err = sync_fileindex(fileindex, fileindex_path);
4501 if (sync_err && err == NULL)
4502 err = sync_err;
4503 done:
4504 free(fileindex_path);
4505 if (fileindex)
4506 got_fileindex_free(fileindex);
4507 unlockerr = lock_worktree(worktree, LOCK_SH);
4508 if (unlockerr && err == NULL)
4509 err = unlockerr;
4510 return err;
4513 static void
4514 free_commitable(struct got_commitable *ct)
4516 free(ct->path);
4517 free(ct->in_repo_path);
4518 free(ct->ondisk_path);
4519 free(ct->blob_id);
4520 free(ct->base_blob_id);
4521 free(ct->staged_blob_id);
4522 free(ct->base_commit_id);
4523 free(ct);
4526 struct collect_commitables_arg {
4527 struct got_pathlist_head *commitable_paths;
4528 struct got_repository *repo;
4529 struct got_worktree *worktree;
4530 struct got_fileindex *fileindex;
4531 int have_staged_files;
4532 int allow_bad_symlinks;
4535 static const struct got_error *
4536 collect_commitables(void *arg, unsigned char status,
4537 unsigned char staged_status, const char *relpath,
4538 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4539 struct got_object_id *commit_id, int dirfd, const char *de_name)
4541 struct collect_commitables_arg *a = arg;
4542 const struct got_error *err = NULL;
4543 struct got_commitable *ct = NULL;
4544 struct got_pathlist_entry *new = NULL;
4545 char *parent_path = NULL, *path = NULL;
4546 struct stat sb;
4548 if (a->have_staged_files) {
4549 if (staged_status != GOT_STATUS_MODIFY &&
4550 staged_status != GOT_STATUS_ADD &&
4551 staged_status != GOT_STATUS_DELETE)
4552 return NULL;
4553 } else {
4554 if (status == GOT_STATUS_CONFLICT)
4555 return got_error(GOT_ERR_COMMIT_CONFLICT);
4557 if (status != GOT_STATUS_MODIFY &&
4558 status != GOT_STATUS_MODE_CHANGE &&
4559 status != GOT_STATUS_ADD &&
4560 status != GOT_STATUS_DELETE)
4561 return NULL;
4564 if (asprintf(&path, "/%s", relpath) == -1) {
4565 err = got_error_from_errno("asprintf");
4566 goto done;
4568 if (strcmp(path, "/") == 0) {
4569 parent_path = strdup("");
4570 if (parent_path == NULL)
4571 return got_error_from_errno("strdup");
4572 } else {
4573 err = got_path_dirname(&parent_path, path);
4574 if (err)
4575 return err;
4578 ct = calloc(1, sizeof(*ct));
4579 if (ct == NULL) {
4580 err = got_error_from_errno("calloc");
4581 goto done;
4584 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4585 relpath) == -1) {
4586 err = got_error_from_errno("asprintf");
4587 goto done;
4590 if (staged_status == GOT_STATUS_ADD ||
4591 staged_status == GOT_STATUS_MODIFY) {
4592 struct got_fileindex_entry *ie;
4593 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4594 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4595 case GOT_FILEIDX_MODE_REGULAR_FILE:
4596 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4597 ct->mode = S_IFREG;
4598 break;
4599 case GOT_FILEIDX_MODE_SYMLINK:
4600 ct->mode = S_IFLNK;
4601 break;
4602 default:
4603 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4604 goto done;
4606 ct->mode |= got_fileindex_entry_perms_get(ie);
4607 } else if (status != GOT_STATUS_DELETE &&
4608 staged_status != GOT_STATUS_DELETE) {
4609 if (dirfd != -1) {
4610 if (fstatat(dirfd, de_name, &sb,
4611 AT_SYMLINK_NOFOLLOW) == -1) {
4612 err = got_error_from_errno2("fstatat",
4613 ct->ondisk_path);
4614 goto done;
4616 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4617 err = got_error_from_errno2("lstat", ct->ondisk_path);
4618 goto done;
4620 ct->mode = sb.st_mode;
4623 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4624 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4625 relpath) == -1) {
4626 err = got_error_from_errno("asprintf");
4627 goto done;
4630 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4631 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4632 int is_bad_symlink;
4633 char target_path[PATH_MAX];
4634 ssize_t target_len;
4635 target_len = readlink(ct->ondisk_path, target_path,
4636 sizeof(target_path));
4637 if (target_len == -1) {
4638 err = got_error_from_errno2("readlink",
4639 ct->ondisk_path);
4640 goto done;
4642 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4643 target_len, ct->ondisk_path, a->worktree->root_path);
4644 if (err)
4645 goto done;
4646 if (is_bad_symlink) {
4647 err = got_error_path(ct->ondisk_path,
4648 GOT_ERR_BAD_SYMLINK);
4649 goto done;
4654 ct->status = status;
4655 ct->staged_status = staged_status;
4656 ct->blob_id = NULL; /* will be filled in when blob gets created */
4657 if (ct->status != GOT_STATUS_ADD &&
4658 ct->staged_status != GOT_STATUS_ADD) {
4659 ct->base_blob_id = got_object_id_dup(blob_id);
4660 if (ct->base_blob_id == NULL) {
4661 err = got_error_from_errno("got_object_id_dup");
4662 goto done;
4664 ct->base_commit_id = got_object_id_dup(commit_id);
4665 if (ct->base_commit_id == NULL) {
4666 err = got_error_from_errno("got_object_id_dup");
4667 goto done;
4670 if (ct->staged_status == GOT_STATUS_ADD ||
4671 ct->staged_status == GOT_STATUS_MODIFY) {
4672 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4673 if (ct->staged_blob_id == NULL) {
4674 err = got_error_from_errno("got_object_id_dup");
4675 goto done;
4678 ct->path = strdup(path);
4679 if (ct->path == NULL) {
4680 err = got_error_from_errno("strdup");
4681 goto done;
4683 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4684 done:
4685 if (ct && (err || new == NULL))
4686 free_commitable(ct);
4687 free(parent_path);
4688 free(path);
4689 return err;
4692 static const struct got_error *write_tree(struct got_object_id **, int *,
4693 struct got_tree_object *, const char *, struct got_pathlist_head *,
4694 got_worktree_status_cb status_cb, void *status_arg,
4695 struct got_repository *);
4697 static const struct got_error *
4698 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4699 struct got_tree_entry *te, const char *parent_path,
4700 struct got_pathlist_head *commitable_paths,
4701 got_worktree_status_cb status_cb, void *status_arg,
4702 struct got_repository *repo)
4704 const struct got_error *err = NULL;
4705 struct got_tree_object *subtree;
4706 char *subpath;
4708 if (asprintf(&subpath, "%s%s%s", parent_path,
4709 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4710 return got_error_from_errno("asprintf");
4712 err = got_object_open_as_tree(&subtree, repo, &te->id);
4713 if (err)
4714 return err;
4716 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4717 commitable_paths, status_cb, status_arg, repo);
4718 got_object_tree_close(subtree);
4719 free(subpath);
4720 return err;
4723 static const struct got_error *
4724 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4726 const struct got_error *err = NULL;
4727 char *ct_parent_path = NULL;
4729 *match = 0;
4731 if (strchr(ct->in_repo_path, '/') == NULL) {
4732 *match = got_path_is_root_dir(path);
4733 return NULL;
4736 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4737 if (err)
4738 return err;
4739 *match = (strcmp(path, ct_parent_path) == 0);
4740 free(ct_parent_path);
4741 return err;
4744 static mode_t
4745 get_ct_file_mode(struct got_commitable *ct)
4747 if (S_ISLNK(ct->mode))
4748 return S_IFLNK;
4750 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4753 static const struct got_error *
4754 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4755 struct got_tree_entry *te, struct got_commitable *ct)
4757 const struct got_error *err = NULL;
4759 *new_te = NULL;
4761 err = got_object_tree_entry_dup(new_te, te);
4762 if (err)
4763 goto done;
4765 (*new_te)->mode = get_ct_file_mode(ct);
4767 if (ct->staged_status == GOT_STATUS_MODIFY)
4768 memcpy(&(*new_te)->id, ct->staged_blob_id,
4769 sizeof((*new_te)->id));
4770 else
4771 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4772 done:
4773 if (err && *new_te) {
4774 free(*new_te);
4775 *new_te = NULL;
4777 return err;
4780 static const struct got_error *
4781 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4782 struct got_commitable *ct)
4784 const struct got_error *err = NULL;
4785 char *ct_name;
4787 *new_te = NULL;
4789 *new_te = calloc(1, sizeof(**new_te));
4790 if (*new_te == NULL)
4791 return got_error_from_errno("calloc");
4793 ct_name = basename(ct->path);
4794 if (ct_name == NULL) {
4795 err = got_error_from_errno2("basename", ct->path);
4796 goto done;
4798 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4799 sizeof((*new_te)->name)) {
4800 err = got_error(GOT_ERR_NO_SPACE);
4801 goto done;
4804 (*new_te)->mode = get_ct_file_mode(ct);
4806 if (ct->staged_status == GOT_STATUS_ADD)
4807 memcpy(&(*new_te)->id, ct->staged_blob_id,
4808 sizeof((*new_te)->id));
4809 else
4810 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4811 done:
4812 if (err && *new_te) {
4813 free(*new_te);
4814 *new_te = NULL;
4816 return err;
4819 static const struct got_error *
4820 insert_tree_entry(struct got_tree_entry *new_te,
4821 struct got_pathlist_head *paths)
4823 const struct got_error *err = NULL;
4824 struct got_pathlist_entry *new_pe;
4826 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4827 if (err)
4828 return err;
4829 if (new_pe == NULL)
4830 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4831 return NULL;
4834 static const struct got_error *
4835 report_ct_status(struct got_commitable *ct,
4836 got_worktree_status_cb status_cb, void *status_arg)
4838 const char *ct_path = ct->path;
4839 unsigned char status;
4841 while (ct_path[0] == '/')
4842 ct_path++;
4844 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4845 status = ct->staged_status;
4846 else
4847 status = ct->status;
4849 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4850 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4853 static const struct got_error *
4854 match_modified_subtree(int *modified, struct got_tree_entry *te,
4855 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4857 const struct got_error *err = NULL;
4858 struct got_pathlist_entry *pe;
4859 char *te_path;
4861 *modified = 0;
4863 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4864 got_path_is_root_dir(base_tree_path) ? "" : "/",
4865 te->name) == -1)
4866 return got_error_from_errno("asprintf");
4868 TAILQ_FOREACH(pe, commitable_paths, entry) {
4869 struct got_commitable *ct = pe->data;
4870 *modified = got_path_is_child(ct->in_repo_path, te_path,
4871 strlen(te_path));
4872 if (*modified)
4873 break;
4876 free(te_path);
4877 return err;
4880 static const struct got_error *
4881 match_deleted_or_modified_ct(struct got_commitable **ctp,
4882 struct got_tree_entry *te, const char *base_tree_path,
4883 struct got_pathlist_head *commitable_paths)
4885 const struct got_error *err = NULL;
4886 struct got_pathlist_entry *pe;
4888 *ctp = NULL;
4890 TAILQ_FOREACH(pe, commitable_paths, entry) {
4891 struct got_commitable *ct = pe->data;
4892 char *ct_name = NULL;
4893 int path_matches;
4895 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4896 if (ct->status != GOT_STATUS_MODIFY &&
4897 ct->status != GOT_STATUS_MODE_CHANGE &&
4898 ct->status != GOT_STATUS_DELETE)
4899 continue;
4900 } else {
4901 if (ct->staged_status != GOT_STATUS_MODIFY &&
4902 ct->staged_status != GOT_STATUS_DELETE)
4903 continue;
4906 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4907 continue;
4909 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4910 if (err)
4911 return err;
4912 if (!path_matches)
4913 continue;
4915 ct_name = basename(pe->path);
4916 if (ct_name == NULL)
4917 return got_error_from_errno2("basename", pe->path);
4919 if (strcmp(te->name, ct_name) != 0)
4920 continue;
4922 *ctp = ct;
4923 break;
4926 return err;
4929 static const struct got_error *
4930 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4931 const char *child_path, const char *path_base_tree,
4932 struct got_pathlist_head *commitable_paths,
4933 got_worktree_status_cb status_cb, void *status_arg,
4934 struct got_repository *repo)
4936 const struct got_error *err = NULL;
4937 struct got_tree_entry *new_te;
4938 char *subtree_path;
4939 struct got_object_id *id = NULL;
4940 int nentries;
4942 *new_tep = NULL;
4944 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4945 got_path_is_root_dir(path_base_tree) ? "" : "/",
4946 child_path) == -1)
4947 return got_error_from_errno("asprintf");
4949 new_te = calloc(1, sizeof(*new_te));
4950 if (new_te == NULL)
4951 return got_error_from_errno("calloc");
4952 new_te->mode = S_IFDIR;
4954 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4955 sizeof(new_te->name)) {
4956 err = got_error(GOT_ERR_NO_SPACE);
4957 goto done;
4959 err = write_tree(&id, &nentries, NULL, subtree_path,
4960 commitable_paths, status_cb, status_arg, repo);
4961 if (err) {
4962 free(new_te);
4963 goto done;
4965 memcpy(&new_te->id, id, sizeof(new_te->id));
4966 done:
4967 free(id);
4968 free(subtree_path);
4969 if (err == NULL)
4970 *new_tep = new_te;
4971 return err;
4974 static const struct got_error *
4975 write_tree(struct got_object_id **new_tree_id, int *nentries,
4976 struct got_tree_object *base_tree, const char *path_base_tree,
4977 struct got_pathlist_head *commitable_paths,
4978 got_worktree_status_cb status_cb, void *status_arg,
4979 struct got_repository *repo)
4981 const struct got_error *err = NULL;
4982 struct got_pathlist_head paths;
4983 struct got_tree_entry *te, *new_te = NULL;
4984 struct got_pathlist_entry *pe;
4986 TAILQ_INIT(&paths);
4987 *nentries = 0;
4989 /* Insert, and recurse into, newly added entries first. */
4990 TAILQ_FOREACH(pe, commitable_paths, entry) {
4991 struct got_commitable *ct = pe->data;
4992 char *child_path = NULL, *slash;
4994 if ((ct->status != GOT_STATUS_ADD &&
4995 ct->staged_status != GOT_STATUS_ADD) ||
4996 (ct->flags & GOT_COMMITABLE_ADDED))
4997 continue;
4999 if (!got_path_is_child(pe->path, path_base_tree,
5000 strlen(path_base_tree)))
5001 continue;
5003 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5004 pe->path);
5005 if (err)
5006 goto done;
5008 slash = strchr(child_path, '/');
5009 if (slash == NULL) {
5010 err = alloc_added_blob_tree_entry(&new_te, ct);
5011 if (err)
5012 goto done;
5013 err = report_ct_status(ct, status_cb, status_arg);
5014 if (err)
5015 goto done;
5016 ct->flags |= GOT_COMMITABLE_ADDED;
5017 err = insert_tree_entry(new_te, &paths);
5018 if (err)
5019 goto done;
5020 (*nentries)++;
5021 } else {
5022 *slash = '\0'; /* trim trailing path components */
5023 if (base_tree == NULL ||
5024 got_object_tree_find_entry(base_tree, child_path)
5025 == NULL) {
5026 err = make_subtree_for_added_blob(&new_te,
5027 child_path, path_base_tree,
5028 commitable_paths, status_cb, status_arg,
5029 repo);
5030 if (err)
5031 goto done;
5032 err = insert_tree_entry(new_te, &paths);
5033 if (err)
5034 goto done;
5035 (*nentries)++;
5040 if (base_tree) {
5041 int i, nbase_entries;
5042 /* Handle modified and deleted entries. */
5043 nbase_entries = got_object_tree_get_nentries(base_tree);
5044 for (i = 0; i < nbase_entries; i++) {
5045 struct got_commitable *ct = NULL;
5047 te = got_object_tree_get_entry(base_tree, i);
5048 if (got_object_tree_entry_is_submodule(te)) {
5049 /* Entry is a submodule; just copy it. */
5050 err = got_object_tree_entry_dup(&new_te, te);
5051 if (err)
5052 goto done;
5053 err = insert_tree_entry(new_te, &paths);
5054 if (err)
5055 goto done;
5056 (*nentries)++;
5057 continue;
5060 if (S_ISDIR(te->mode)) {
5061 int modified;
5062 err = got_object_tree_entry_dup(&new_te, te);
5063 if (err)
5064 goto done;
5065 err = match_modified_subtree(&modified, te,
5066 path_base_tree, commitable_paths);
5067 if (err)
5068 goto done;
5069 /* Avoid recursion into unmodified subtrees. */
5070 if (modified) {
5071 struct got_object_id *new_id;
5072 int nsubentries;
5073 err = write_subtree(&new_id,
5074 &nsubentries, te,
5075 path_base_tree, commitable_paths,
5076 status_cb, status_arg, repo);
5077 if (err)
5078 goto done;
5079 if (nsubentries == 0) {
5080 /* All entries were deleted. */
5081 free(new_id);
5082 continue;
5084 memcpy(&new_te->id, new_id,
5085 sizeof(new_te->id));
5086 free(new_id);
5088 err = insert_tree_entry(new_te, &paths);
5089 if (err)
5090 goto done;
5091 (*nentries)++;
5092 continue;
5095 err = match_deleted_or_modified_ct(&ct, te,
5096 path_base_tree, commitable_paths);
5097 if (err)
5098 goto done;
5099 if (ct) {
5100 /* NB: Deleted entries get dropped here. */
5101 if (ct->status == GOT_STATUS_MODIFY ||
5102 ct->status == GOT_STATUS_MODE_CHANGE ||
5103 ct->staged_status == GOT_STATUS_MODIFY) {
5104 err = alloc_modified_blob_tree_entry(
5105 &new_te, te, ct);
5106 if (err)
5107 goto done;
5108 err = insert_tree_entry(new_te, &paths);
5109 if (err)
5110 goto done;
5111 (*nentries)++;
5113 err = report_ct_status(ct, status_cb,
5114 status_arg);
5115 if (err)
5116 goto done;
5117 } else {
5118 /* Entry is unchanged; just copy it. */
5119 err = got_object_tree_entry_dup(&new_te, te);
5120 if (err)
5121 goto done;
5122 err = insert_tree_entry(new_te, &paths);
5123 if (err)
5124 goto done;
5125 (*nentries)++;
5130 /* Write new list of entries; deleted entries have been dropped. */
5131 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5132 done:
5133 got_pathlist_free(&paths);
5134 return err;
5137 static const struct got_error *
5138 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5139 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5140 int have_staged_files)
5142 const struct got_error *err = NULL;
5143 struct got_pathlist_entry *pe;
5145 TAILQ_FOREACH(pe, commitable_paths, entry) {
5146 struct got_fileindex_entry *ie;
5147 struct got_commitable *ct = pe->data;
5149 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5150 if (ie) {
5151 if (ct->status == GOT_STATUS_DELETE ||
5152 ct->staged_status == GOT_STATUS_DELETE) {
5153 got_fileindex_entry_remove(fileindex, ie);
5154 } else if (ct->staged_status == GOT_STATUS_ADD ||
5155 ct->staged_status == GOT_STATUS_MODIFY) {
5156 got_fileindex_entry_stage_set(ie,
5157 GOT_FILEIDX_STAGE_NONE);
5158 err = got_fileindex_entry_update(ie,
5159 ct->ondisk_path, ct->staged_blob_id->sha1,
5160 new_base_commit_id->sha1,
5161 !have_staged_files);
5162 } else
5163 err = got_fileindex_entry_update(ie,
5164 ct->ondisk_path, ct->blob_id->sha1,
5165 new_base_commit_id->sha1,
5166 !have_staged_files);
5167 } else {
5168 err = got_fileindex_entry_alloc(&ie, pe->path);
5169 if (err)
5170 break;
5171 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5172 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5173 if (err) {
5174 got_fileindex_entry_free(ie);
5175 break;
5177 err = got_fileindex_entry_add(fileindex, ie);
5178 if (err) {
5179 got_fileindex_entry_free(ie);
5180 break;
5184 return err;
5188 static const struct got_error *
5189 check_out_of_date(const char *in_repo_path, unsigned char status,
5190 unsigned char staged_status, struct got_object_id *base_blob_id,
5191 struct got_object_id *base_commit_id,
5192 struct got_object_id *head_commit_id, struct got_repository *repo,
5193 int ood_errcode)
5195 const struct got_error *err = NULL;
5196 struct got_object_id *id = NULL;
5198 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5199 /* Trivial case: base commit == head commit */
5200 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5201 return NULL;
5203 * Ensure file content which local changes were based
5204 * on matches file content in the branch head.
5206 err = got_object_id_by_path(&id, repo, head_commit_id,
5207 in_repo_path);
5208 if (err) {
5209 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5210 err = got_error(ood_errcode);
5211 goto done;
5212 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5213 err = got_error(ood_errcode);
5214 } else {
5215 /* Require that added files don't exist in the branch head. */
5216 err = got_object_id_by_path(&id, repo, head_commit_id,
5217 in_repo_path);
5218 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5219 goto done;
5220 err = id ? got_error(ood_errcode) : NULL;
5222 done:
5223 free(id);
5224 return err;
5227 const struct got_error *
5228 commit_worktree(struct got_object_id **new_commit_id,
5229 struct got_pathlist_head *commitable_paths,
5230 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5231 const char *author, const char *committer,
5232 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5233 got_worktree_status_cb status_cb, void *status_arg,
5234 struct got_repository *repo)
5236 const struct got_error *err = NULL, *unlockerr = NULL;
5237 struct got_pathlist_entry *pe;
5238 const char *head_ref_name = NULL;
5239 struct got_commit_object *head_commit = NULL;
5240 struct got_reference *head_ref2 = NULL;
5241 struct got_object_id *head_commit_id2 = NULL;
5242 struct got_tree_object *head_tree = NULL;
5243 struct got_object_id *new_tree_id = NULL;
5244 int nentries;
5245 struct got_object_id_queue parent_ids;
5246 struct got_object_qid *pid = NULL;
5247 char *logmsg = NULL;
5249 *new_commit_id = NULL;
5251 SIMPLEQ_INIT(&parent_ids);
5253 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5254 if (err)
5255 goto done;
5257 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5258 if (err)
5259 goto done;
5261 if (commit_msg_cb != NULL) {
5262 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5263 if (err)
5264 goto done;
5267 if (logmsg == NULL || strlen(logmsg) == 0) {
5268 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5269 goto done;
5272 /* Create blobs from added and modified files and record their IDs. */
5273 TAILQ_FOREACH(pe, commitable_paths, entry) {
5274 struct got_commitable *ct = pe->data;
5275 char *ondisk_path;
5277 /* Blobs for staged files already exist. */
5278 if (ct->staged_status == GOT_STATUS_ADD ||
5279 ct->staged_status == GOT_STATUS_MODIFY)
5280 continue;
5282 if (ct->status != GOT_STATUS_ADD &&
5283 ct->status != GOT_STATUS_MODIFY &&
5284 ct->status != GOT_STATUS_MODE_CHANGE)
5285 continue;
5287 if (asprintf(&ondisk_path, "%s/%s",
5288 worktree->root_path, pe->path) == -1) {
5289 err = got_error_from_errno("asprintf");
5290 goto done;
5292 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5293 free(ondisk_path);
5294 if (err)
5295 goto done;
5298 /* Recursively write new tree objects. */
5299 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5300 commitable_paths, status_cb, status_arg, repo);
5301 if (err)
5302 goto done;
5304 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5305 if (err)
5306 goto done;
5307 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5308 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5309 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5310 got_object_qid_free(pid);
5311 if (logmsg != NULL)
5312 free(logmsg);
5313 if (err)
5314 goto done;
5316 /* Check if a concurrent commit to our branch has occurred. */
5317 head_ref_name = got_worktree_get_head_ref_name(worktree);
5318 if (head_ref_name == NULL) {
5319 err = got_error_from_errno("got_worktree_get_head_ref_name");
5320 goto done;
5322 /* Lock the reference here to prevent concurrent modification. */
5323 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5324 if (err)
5325 goto done;
5326 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5327 if (err)
5328 goto done;
5329 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5330 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5331 goto done;
5333 /* Update branch head in repository. */
5334 err = got_ref_change_ref(head_ref2, *new_commit_id);
5335 if (err)
5336 goto done;
5337 err = got_ref_write(head_ref2, repo);
5338 if (err)
5339 goto done;
5341 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5342 if (err)
5343 goto done;
5345 err = ref_base_commit(worktree, repo);
5346 if (err)
5347 goto done;
5348 done:
5349 if (head_tree)
5350 got_object_tree_close(head_tree);
5351 if (head_commit)
5352 got_object_commit_close(head_commit);
5353 free(head_commit_id2);
5354 if (head_ref2) {
5355 unlockerr = got_ref_unlock(head_ref2);
5356 if (unlockerr && err == NULL)
5357 err = unlockerr;
5358 got_ref_close(head_ref2);
5360 return err;
5363 static const struct got_error *
5364 check_path_is_commitable(const char *path,
5365 struct got_pathlist_head *commitable_paths)
5367 struct got_pathlist_entry *cpe = NULL;
5368 size_t path_len = strlen(path);
5370 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5371 struct got_commitable *ct = cpe->data;
5372 const char *ct_path = ct->path;
5374 while (ct_path[0] == '/')
5375 ct_path++;
5377 if (strcmp(path, ct_path) == 0 ||
5378 got_path_is_child(ct_path, path, path_len))
5379 break;
5382 if (cpe == NULL)
5383 return got_error_path(path, GOT_ERR_BAD_PATH);
5385 return NULL;
5388 static const struct got_error *
5389 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5391 int *have_staged_files = arg;
5393 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5394 *have_staged_files = 1;
5395 return got_error(GOT_ERR_CANCELLED);
5398 return NULL;
5401 static const struct got_error *
5402 check_non_staged_files(struct got_fileindex *fileindex,
5403 struct got_pathlist_head *paths)
5405 struct got_pathlist_entry *pe;
5406 struct got_fileindex_entry *ie;
5408 TAILQ_FOREACH(pe, paths, entry) {
5409 if (pe->path[0] == '\0')
5410 continue;
5411 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5412 if (ie == NULL)
5413 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5414 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5415 return got_error_path(pe->path,
5416 GOT_ERR_FILE_NOT_STAGED);
5419 return NULL;
5422 const struct got_error *
5423 got_worktree_commit(struct got_object_id **new_commit_id,
5424 struct got_worktree *worktree, struct got_pathlist_head *paths,
5425 const char *author, const char *committer, int allow_bad_symlinks,
5426 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5427 got_worktree_status_cb status_cb, void *status_arg,
5428 struct got_repository *repo)
5430 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5431 struct got_fileindex *fileindex = NULL;
5432 char *fileindex_path = NULL;
5433 struct got_pathlist_head commitable_paths;
5434 struct collect_commitables_arg cc_arg;
5435 struct got_pathlist_entry *pe;
5436 struct got_reference *head_ref = NULL;
5437 struct got_object_id *head_commit_id = NULL;
5438 int have_staged_files = 0;
5440 *new_commit_id = NULL;
5442 TAILQ_INIT(&commitable_paths);
5444 err = lock_worktree(worktree, LOCK_EX);
5445 if (err)
5446 goto done;
5448 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5449 if (err)
5450 goto done;
5452 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5453 if (err)
5454 goto done;
5456 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5457 if (err)
5458 goto done;
5460 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5461 &have_staged_files);
5462 if (err && err->code != GOT_ERR_CANCELLED)
5463 goto done;
5464 if (have_staged_files) {
5465 err = check_non_staged_files(fileindex, paths);
5466 if (err)
5467 goto done;
5470 cc_arg.commitable_paths = &commitable_paths;
5471 cc_arg.worktree = worktree;
5472 cc_arg.fileindex = fileindex;
5473 cc_arg.repo = repo;
5474 cc_arg.have_staged_files = have_staged_files;
5475 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5476 TAILQ_FOREACH(pe, paths, entry) {
5477 err = worktree_status(worktree, pe->path, fileindex, repo,
5478 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5479 if (err)
5480 goto done;
5483 if (TAILQ_EMPTY(&commitable_paths)) {
5484 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5485 goto done;
5488 TAILQ_FOREACH(pe, paths, entry) {
5489 err = check_path_is_commitable(pe->path, &commitable_paths);
5490 if (err)
5491 goto done;
5494 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5495 struct got_commitable *ct = pe->data;
5496 const char *ct_path = ct->in_repo_path;
5498 while (ct_path[0] == '/')
5499 ct_path++;
5500 err = check_out_of_date(ct_path, ct->status,
5501 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5502 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5503 if (err)
5504 goto done;
5508 err = commit_worktree(new_commit_id, &commitable_paths,
5509 head_commit_id, worktree, author, committer,
5510 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5511 if (err)
5512 goto done;
5514 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5515 fileindex, have_staged_files);
5516 sync_err = sync_fileindex(fileindex, fileindex_path);
5517 if (sync_err && err == NULL)
5518 err = sync_err;
5519 done:
5520 if (fileindex)
5521 got_fileindex_free(fileindex);
5522 free(fileindex_path);
5523 unlockerr = lock_worktree(worktree, LOCK_SH);
5524 if (unlockerr && err == NULL)
5525 err = unlockerr;
5526 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5527 struct got_commitable *ct = pe->data;
5528 free_commitable(ct);
5530 got_pathlist_free(&commitable_paths);
5531 return err;
5534 const char *
5535 got_commitable_get_path(struct got_commitable *ct)
5537 return ct->path;
5540 unsigned int
5541 got_commitable_get_status(struct got_commitable *ct)
5543 return ct->status;
5546 struct check_rebase_ok_arg {
5547 struct got_worktree *worktree;
5548 struct got_repository *repo;
5551 static const struct got_error *
5552 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5554 const struct got_error *err = NULL;
5555 struct check_rebase_ok_arg *a = arg;
5556 unsigned char status;
5557 struct stat sb;
5558 char *ondisk_path;
5560 /* Reject rebase of a work tree with mixed base commits. */
5561 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5562 SHA1_DIGEST_LENGTH))
5563 return got_error(GOT_ERR_MIXED_COMMITS);
5565 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5566 == -1)
5567 return got_error_from_errno("asprintf");
5569 /* Reject rebase of a work tree with modified or staged files. */
5570 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5571 free(ondisk_path);
5572 if (err)
5573 return err;
5575 if (status != GOT_STATUS_NO_CHANGE)
5576 return got_error(GOT_ERR_MODIFIED);
5577 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5578 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5580 return NULL;
5583 const struct got_error *
5584 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5585 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5586 struct got_worktree *worktree, struct got_reference *branch,
5587 struct got_repository *repo)
5589 const struct got_error *err = NULL;
5590 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5591 char *branch_ref_name = NULL;
5592 char *fileindex_path = NULL;
5593 struct check_rebase_ok_arg ok_arg;
5594 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5595 struct got_object_id *wt_branch_tip = NULL;
5597 *new_base_branch_ref = NULL;
5598 *tmp_branch = NULL;
5599 *fileindex = NULL;
5601 err = lock_worktree(worktree, LOCK_EX);
5602 if (err)
5603 return err;
5605 err = open_fileindex(fileindex, &fileindex_path, worktree);
5606 if (err)
5607 goto done;
5609 ok_arg.worktree = worktree;
5610 ok_arg.repo = repo;
5611 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5612 &ok_arg);
5613 if (err)
5614 goto done;
5616 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5617 if (err)
5618 goto done;
5620 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5621 if (err)
5622 goto done;
5624 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5625 if (err)
5626 goto done;
5628 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5629 0);
5630 if (err)
5631 goto done;
5633 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5634 if (err)
5635 goto done;
5636 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5637 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5638 goto done;
5641 err = got_ref_alloc_symref(new_base_branch_ref,
5642 new_base_branch_ref_name, wt_branch);
5643 if (err)
5644 goto done;
5645 err = got_ref_write(*new_base_branch_ref, repo);
5646 if (err)
5647 goto done;
5649 /* TODO Lock original branch's ref while rebasing? */
5651 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5652 if (err)
5653 goto done;
5655 err = got_ref_write(branch_ref, repo);
5656 if (err)
5657 goto done;
5659 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5660 worktree->base_commit_id);
5661 if (err)
5662 goto done;
5663 err = got_ref_write(*tmp_branch, repo);
5664 if (err)
5665 goto done;
5667 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5668 if (err)
5669 goto done;
5670 done:
5671 free(fileindex_path);
5672 free(tmp_branch_name);
5673 free(new_base_branch_ref_name);
5674 free(branch_ref_name);
5675 if (branch_ref)
5676 got_ref_close(branch_ref);
5677 if (wt_branch)
5678 got_ref_close(wt_branch);
5679 free(wt_branch_tip);
5680 if (err) {
5681 if (*new_base_branch_ref) {
5682 got_ref_close(*new_base_branch_ref);
5683 *new_base_branch_ref = NULL;
5685 if (*tmp_branch) {
5686 got_ref_close(*tmp_branch);
5687 *tmp_branch = NULL;
5689 if (*fileindex) {
5690 got_fileindex_free(*fileindex);
5691 *fileindex = NULL;
5693 lock_worktree(worktree, LOCK_SH);
5695 return err;
5698 const struct got_error *
5699 got_worktree_rebase_continue(struct got_object_id **commit_id,
5700 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5701 struct got_reference **branch, struct got_fileindex **fileindex,
5702 struct got_worktree *worktree, struct got_repository *repo)
5704 const struct got_error *err;
5705 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5706 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5707 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5708 char *fileindex_path = NULL;
5709 int have_staged_files = 0;
5711 *commit_id = NULL;
5712 *new_base_branch = NULL;
5713 *tmp_branch = NULL;
5714 *branch = NULL;
5715 *fileindex = NULL;
5717 err = lock_worktree(worktree, LOCK_EX);
5718 if (err)
5719 return err;
5721 err = open_fileindex(fileindex, &fileindex_path, worktree);
5722 if (err)
5723 goto done;
5725 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5726 &have_staged_files);
5727 if (err && err->code != GOT_ERR_CANCELLED)
5728 goto done;
5729 if (have_staged_files) {
5730 err = got_error(GOT_ERR_STAGED_PATHS);
5731 goto done;
5734 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5735 if (err)
5736 goto done;
5738 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5739 if (err)
5740 goto done;
5742 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5743 if (err)
5744 goto done;
5746 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5747 if (err)
5748 goto done;
5750 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5751 if (err)
5752 goto done;
5754 err = got_ref_open(branch, repo,
5755 got_ref_get_symref_target(branch_ref), 0);
5756 if (err)
5757 goto done;
5759 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5760 if (err)
5761 goto done;
5763 err = got_ref_resolve(commit_id, repo, commit_ref);
5764 if (err)
5765 goto done;
5767 err = got_ref_open(new_base_branch, repo,
5768 new_base_branch_ref_name, 0);
5769 if (err)
5770 goto done;
5772 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5773 if (err)
5774 goto done;
5775 done:
5776 free(commit_ref_name);
5777 free(branch_ref_name);
5778 free(fileindex_path);
5779 if (commit_ref)
5780 got_ref_close(commit_ref);
5781 if (branch_ref)
5782 got_ref_close(branch_ref);
5783 if (err) {
5784 free(*commit_id);
5785 *commit_id = NULL;
5786 if (*tmp_branch) {
5787 got_ref_close(*tmp_branch);
5788 *tmp_branch = NULL;
5790 if (*new_base_branch) {
5791 got_ref_close(*new_base_branch);
5792 *new_base_branch = NULL;
5794 if (*branch) {
5795 got_ref_close(*branch);
5796 *branch = NULL;
5798 if (*fileindex) {
5799 got_fileindex_free(*fileindex);
5800 *fileindex = NULL;
5802 lock_worktree(worktree, LOCK_SH);
5804 return err;
5807 const struct got_error *
5808 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5810 const struct got_error *err;
5811 char *tmp_branch_name = NULL;
5813 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5814 if (err)
5815 return err;
5817 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5818 free(tmp_branch_name);
5819 return NULL;
5822 static const struct got_error *
5823 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5824 char **logmsg, void *arg)
5826 *logmsg = arg;
5827 return NULL;
5830 static const struct got_error *
5831 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5832 const char *path, struct got_object_id *blob_id,
5833 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5834 int dirfd, const char *de_name)
5836 return NULL;
5839 struct collect_merged_paths_arg {
5840 got_worktree_checkout_cb progress_cb;
5841 void *progress_arg;
5842 struct got_pathlist_head *merged_paths;
5845 static const struct got_error *
5846 collect_merged_paths(void *arg, unsigned char status, const char *path)
5848 const struct got_error *err;
5849 struct collect_merged_paths_arg *a = arg;
5850 char *p;
5851 struct got_pathlist_entry *new;
5853 err = (*a->progress_cb)(a->progress_arg, status, path);
5854 if (err)
5855 return err;
5857 if (status != GOT_STATUS_MERGE &&
5858 status != GOT_STATUS_ADD &&
5859 status != GOT_STATUS_DELETE &&
5860 status != GOT_STATUS_CONFLICT)
5861 return NULL;
5863 p = strdup(path);
5864 if (p == NULL)
5865 return got_error_from_errno("strdup");
5867 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5868 if (err || new == NULL)
5869 free(p);
5870 return err;
5873 void
5874 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5876 struct got_pathlist_entry *pe;
5878 TAILQ_FOREACH(pe, merged_paths, entry)
5879 free((char *)pe->path);
5881 got_pathlist_free(merged_paths);
5884 static const struct got_error *
5885 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5886 int is_rebase, struct got_repository *repo)
5888 const struct got_error *err;
5889 struct got_reference *commit_ref = NULL;
5891 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5892 if (err) {
5893 if (err->code != GOT_ERR_NOT_REF)
5894 goto done;
5895 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5896 if (err)
5897 goto done;
5898 err = got_ref_write(commit_ref, repo);
5899 if (err)
5900 goto done;
5901 } else if (is_rebase) {
5902 struct got_object_id *stored_id;
5903 int cmp;
5905 err = got_ref_resolve(&stored_id, repo, commit_ref);
5906 if (err)
5907 goto done;
5908 cmp = got_object_id_cmp(commit_id, stored_id);
5909 free(stored_id);
5910 if (cmp != 0) {
5911 err = got_error(GOT_ERR_REBASE_COMMITID);
5912 goto done;
5915 done:
5916 if (commit_ref)
5917 got_ref_close(commit_ref);
5918 return err;
5921 static const struct got_error *
5922 rebase_merge_files(struct got_pathlist_head *merged_paths,
5923 const char *commit_ref_name, struct got_worktree *worktree,
5924 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5925 struct got_object_id *commit_id, struct got_repository *repo,
5926 got_worktree_checkout_cb progress_cb, void *progress_arg,
5927 got_cancel_cb cancel_cb, void *cancel_arg)
5929 const struct got_error *err;
5930 struct got_reference *commit_ref = NULL;
5931 struct collect_merged_paths_arg cmp_arg;
5932 char *fileindex_path;
5934 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5936 err = get_fileindex_path(&fileindex_path, worktree);
5937 if (err)
5938 return err;
5940 cmp_arg.progress_cb = progress_cb;
5941 cmp_arg.progress_arg = progress_arg;
5942 cmp_arg.merged_paths = merged_paths;
5943 err = merge_files(worktree, fileindex, fileindex_path,
5944 parent_commit_id, commit_id, repo, collect_merged_paths,
5945 &cmp_arg, cancel_cb, cancel_arg);
5946 if (commit_ref)
5947 got_ref_close(commit_ref);
5948 return err;
5951 const struct got_error *
5952 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5953 struct got_worktree *worktree, struct got_fileindex *fileindex,
5954 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5955 struct got_repository *repo,
5956 got_worktree_checkout_cb progress_cb, void *progress_arg,
5957 got_cancel_cb cancel_cb, void *cancel_arg)
5959 const struct got_error *err;
5960 char *commit_ref_name;
5962 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5963 if (err)
5964 return err;
5966 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5967 if (err)
5968 goto done;
5970 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5971 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5972 progress_arg, cancel_cb, cancel_arg);
5973 done:
5974 free(commit_ref_name);
5975 return err;
5978 const struct got_error *
5979 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5980 struct got_worktree *worktree, struct got_fileindex *fileindex,
5981 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5982 struct got_repository *repo,
5983 got_worktree_checkout_cb progress_cb, void *progress_arg,
5984 got_cancel_cb cancel_cb, void *cancel_arg)
5986 const struct got_error *err;
5987 char *commit_ref_name;
5989 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5990 if (err)
5991 return err;
5993 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5994 if (err)
5995 goto done;
5997 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5998 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5999 progress_arg, cancel_cb, cancel_arg);
6000 done:
6001 free(commit_ref_name);
6002 return err;
6005 static const struct got_error *
6006 rebase_commit(struct got_object_id **new_commit_id,
6007 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6008 struct got_worktree *worktree, struct got_fileindex *fileindex,
6009 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6010 const char *new_logmsg, struct got_repository *repo)
6012 const struct got_error *err, *sync_err;
6013 struct got_pathlist_head commitable_paths;
6014 struct collect_commitables_arg cc_arg;
6015 char *fileindex_path = NULL;
6016 struct got_reference *head_ref = NULL;
6017 struct got_object_id *head_commit_id = NULL;
6018 char *logmsg = NULL;
6020 TAILQ_INIT(&commitable_paths);
6021 *new_commit_id = NULL;
6023 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6025 err = get_fileindex_path(&fileindex_path, worktree);
6026 if (err)
6027 return err;
6029 cc_arg.commitable_paths = &commitable_paths;
6030 cc_arg.worktree = worktree;
6031 cc_arg.repo = repo;
6032 cc_arg.have_staged_files = 0;
6034 * If possible get the status of individual files directly to
6035 * avoid crawling the entire work tree once per rebased commit.
6036 * TODO: Ideally, merged_paths would contain a list of commitables
6037 * we could use so we could skip worktree_status() entirely.
6039 if (merged_paths) {
6040 struct got_pathlist_entry *pe;
6041 TAILQ_FOREACH(pe, merged_paths, entry) {
6042 err = worktree_status(worktree, pe->path, fileindex,
6043 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6044 0);
6045 if (err)
6046 goto done;
6048 } else {
6049 err = worktree_status(worktree, "", fileindex, repo,
6050 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6051 if (err)
6052 goto done;
6055 if (TAILQ_EMPTY(&commitable_paths)) {
6056 /* No-op change; commit will be elided. */
6057 err = got_ref_delete(commit_ref, repo);
6058 if (err)
6059 goto done;
6060 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6061 goto done;
6064 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6065 if (err)
6066 goto done;
6068 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6069 if (err)
6070 goto done;
6072 if (new_logmsg) {
6073 logmsg = strdup(new_logmsg);
6074 if (logmsg == NULL) {
6075 err = got_error_from_errno("strdup");
6076 goto done;
6078 } else {
6079 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6080 if (err)
6081 goto done;
6084 /* NB: commit_worktree will call free(logmsg) */
6085 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6086 worktree, got_object_commit_get_author(orig_commit),
6087 got_object_commit_get_committer(orig_commit),
6088 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6089 if (err)
6090 goto done;
6092 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6093 if (err)
6094 goto done;
6096 err = got_ref_delete(commit_ref, repo);
6097 if (err)
6098 goto done;
6100 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6101 fileindex, 0);
6102 sync_err = sync_fileindex(fileindex, fileindex_path);
6103 if (sync_err && err == NULL)
6104 err = sync_err;
6105 done:
6106 free(fileindex_path);
6107 free(head_commit_id);
6108 if (head_ref)
6109 got_ref_close(head_ref);
6110 if (err) {
6111 free(*new_commit_id);
6112 *new_commit_id = NULL;
6114 return err;
6117 const struct got_error *
6118 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6119 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6120 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6121 struct got_commit_object *orig_commit,
6122 struct got_object_id *orig_commit_id, struct got_repository *repo)
6124 const struct got_error *err;
6125 char *commit_ref_name;
6126 struct got_reference *commit_ref = NULL;
6127 struct got_object_id *commit_id = NULL;
6129 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6130 if (err)
6131 return err;
6133 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6134 if (err)
6135 goto done;
6136 err = got_ref_resolve(&commit_id, repo, commit_ref);
6137 if (err)
6138 goto done;
6139 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6140 err = got_error(GOT_ERR_REBASE_COMMITID);
6141 goto done;
6144 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6145 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6146 done:
6147 if (commit_ref)
6148 got_ref_close(commit_ref);
6149 free(commit_ref_name);
6150 free(commit_id);
6151 return err;
6154 const struct got_error *
6155 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6156 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6157 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6158 struct got_commit_object *orig_commit,
6159 struct got_object_id *orig_commit_id, const char *new_logmsg,
6160 struct got_repository *repo)
6162 const struct got_error *err;
6163 char *commit_ref_name;
6164 struct got_reference *commit_ref = NULL;
6166 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6167 if (err)
6168 return err;
6170 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6171 if (err)
6172 goto done;
6174 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6175 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6176 done:
6177 if (commit_ref)
6178 got_ref_close(commit_ref);
6179 free(commit_ref_name);
6180 return err;
6183 const struct got_error *
6184 got_worktree_rebase_postpone(struct got_worktree *worktree,
6185 struct got_fileindex *fileindex)
6187 if (fileindex)
6188 got_fileindex_free(fileindex);
6189 return lock_worktree(worktree, LOCK_SH);
6192 static const struct got_error *
6193 delete_ref(const char *name, struct got_repository *repo)
6195 const struct got_error *err;
6196 struct got_reference *ref;
6198 err = got_ref_open(&ref, repo, name, 0);
6199 if (err) {
6200 if (err->code == GOT_ERR_NOT_REF)
6201 return NULL;
6202 return err;
6205 err = got_ref_delete(ref, repo);
6206 got_ref_close(ref);
6207 return err;
6210 static const struct got_error *
6211 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6213 const struct got_error *err;
6214 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6215 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6217 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6218 if (err)
6219 goto done;
6220 err = delete_ref(tmp_branch_name, repo);
6221 if (err)
6222 goto done;
6224 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6225 if (err)
6226 goto done;
6227 err = delete_ref(new_base_branch_ref_name, repo);
6228 if (err)
6229 goto done;
6231 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6232 if (err)
6233 goto done;
6234 err = delete_ref(branch_ref_name, repo);
6235 if (err)
6236 goto done;
6238 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6239 if (err)
6240 goto done;
6241 err = delete_ref(commit_ref_name, repo);
6242 if (err)
6243 goto done;
6245 done:
6246 free(tmp_branch_name);
6247 free(new_base_branch_ref_name);
6248 free(branch_ref_name);
6249 free(commit_ref_name);
6250 return err;
6253 const struct got_error *
6254 got_worktree_rebase_complete(struct got_worktree *worktree,
6255 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6256 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6257 struct got_repository *repo)
6259 const struct got_error *err, *unlockerr;
6260 struct got_object_id *new_head_commit_id = NULL;
6262 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6263 if (err)
6264 return err;
6266 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6267 if (err)
6268 goto done;
6270 err = got_ref_write(rebased_branch, repo);
6271 if (err)
6272 goto done;
6274 err = got_worktree_set_head_ref(worktree, rebased_branch);
6275 if (err)
6276 goto done;
6278 err = delete_rebase_refs(worktree, repo);
6279 done:
6280 if (fileindex)
6281 got_fileindex_free(fileindex);
6282 free(new_head_commit_id);
6283 unlockerr = lock_worktree(worktree, LOCK_SH);
6284 if (unlockerr && err == NULL)
6285 err = unlockerr;
6286 return err;
6289 const struct got_error *
6290 got_worktree_rebase_abort(struct got_worktree *worktree,
6291 struct got_fileindex *fileindex, struct got_repository *repo,
6292 struct got_reference *new_base_branch,
6293 got_worktree_checkout_cb progress_cb, void *progress_arg)
6295 const struct got_error *err, *unlockerr, *sync_err;
6296 struct got_reference *resolved = NULL;
6297 struct got_object_id *commit_id = NULL;
6298 char *fileindex_path = NULL;
6299 struct revert_file_args rfa;
6300 struct got_object_id *tree_id = NULL;
6302 err = lock_worktree(worktree, LOCK_EX);
6303 if (err)
6304 return err;
6306 err = got_ref_open(&resolved, repo,
6307 got_ref_get_symref_target(new_base_branch), 0);
6308 if (err)
6309 goto done;
6311 err = got_worktree_set_head_ref(worktree, resolved);
6312 if (err)
6313 goto done;
6316 * XXX commits to the base branch could have happened while
6317 * we were busy rebasing; should we store the original commit ID
6318 * when rebase begins and read it back here?
6320 err = got_ref_resolve(&commit_id, repo, resolved);
6321 if (err)
6322 goto done;
6324 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6325 if (err)
6326 goto done;
6328 err = got_object_id_by_path(&tree_id, repo,
6329 worktree->base_commit_id, worktree->path_prefix);
6330 if (err)
6331 goto done;
6333 err = delete_rebase_refs(worktree, repo);
6334 if (err)
6335 goto done;
6337 err = get_fileindex_path(&fileindex_path, worktree);
6338 if (err)
6339 goto done;
6341 rfa.worktree = worktree;
6342 rfa.fileindex = fileindex;
6343 rfa.progress_cb = progress_cb;
6344 rfa.progress_arg = progress_arg;
6345 rfa.patch_cb = NULL;
6346 rfa.patch_arg = NULL;
6347 rfa.repo = repo;
6348 err = worktree_status(worktree, "", fileindex, repo,
6349 revert_file, &rfa, NULL, NULL, 0, 0);
6350 if (err)
6351 goto sync;
6353 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6354 repo, progress_cb, progress_arg, NULL, NULL);
6355 sync:
6356 sync_err = sync_fileindex(fileindex, fileindex_path);
6357 if (sync_err && err == NULL)
6358 err = sync_err;
6359 done:
6360 got_ref_close(resolved);
6361 free(tree_id);
6362 free(commit_id);
6363 if (fileindex)
6364 got_fileindex_free(fileindex);
6365 free(fileindex_path);
6367 unlockerr = lock_worktree(worktree, LOCK_SH);
6368 if (unlockerr && err == NULL)
6369 err = unlockerr;
6370 return err;
6373 const struct got_error *
6374 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6375 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6376 struct got_fileindex **fileindex, struct got_worktree *worktree,
6377 struct got_repository *repo)
6379 const struct got_error *err = NULL;
6380 char *tmp_branch_name = NULL;
6381 char *branch_ref_name = NULL;
6382 char *base_commit_ref_name = NULL;
6383 char *fileindex_path = NULL;
6384 struct check_rebase_ok_arg ok_arg;
6385 struct got_reference *wt_branch = NULL;
6386 struct got_reference *base_commit_ref = NULL;
6388 *tmp_branch = NULL;
6389 *branch_ref = NULL;
6390 *base_commit_id = NULL;
6391 *fileindex = NULL;
6393 err = lock_worktree(worktree, LOCK_EX);
6394 if (err)
6395 return err;
6397 err = open_fileindex(fileindex, &fileindex_path, worktree);
6398 if (err)
6399 goto done;
6401 ok_arg.worktree = worktree;
6402 ok_arg.repo = repo;
6403 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6404 &ok_arg);
6405 if (err)
6406 goto done;
6408 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6409 if (err)
6410 goto done;
6412 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6413 if (err)
6414 goto done;
6416 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6417 worktree);
6418 if (err)
6419 goto done;
6421 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6422 0);
6423 if (err)
6424 goto done;
6426 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6427 if (err)
6428 goto done;
6430 err = got_ref_write(*branch_ref, repo);
6431 if (err)
6432 goto done;
6434 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6435 worktree->base_commit_id);
6436 if (err)
6437 goto done;
6438 err = got_ref_write(base_commit_ref, repo);
6439 if (err)
6440 goto done;
6441 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6442 if (*base_commit_id == NULL) {
6443 err = got_error_from_errno("got_object_id_dup");
6444 goto done;
6447 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6448 worktree->base_commit_id);
6449 if (err)
6450 goto done;
6451 err = got_ref_write(*tmp_branch, repo);
6452 if (err)
6453 goto done;
6455 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6456 if (err)
6457 goto done;
6458 done:
6459 free(fileindex_path);
6460 free(tmp_branch_name);
6461 free(branch_ref_name);
6462 free(base_commit_ref_name);
6463 if (wt_branch)
6464 got_ref_close(wt_branch);
6465 if (err) {
6466 if (*branch_ref) {
6467 got_ref_close(*branch_ref);
6468 *branch_ref = NULL;
6470 if (*tmp_branch) {
6471 got_ref_close(*tmp_branch);
6472 *tmp_branch = NULL;
6474 free(*base_commit_id);
6475 if (*fileindex) {
6476 got_fileindex_free(*fileindex);
6477 *fileindex = NULL;
6479 lock_worktree(worktree, LOCK_SH);
6481 return err;
6484 const struct got_error *
6485 got_worktree_histedit_postpone(struct got_worktree *worktree,
6486 struct got_fileindex *fileindex)
6488 if (fileindex)
6489 got_fileindex_free(fileindex);
6490 return lock_worktree(worktree, LOCK_SH);
6493 const struct got_error *
6494 got_worktree_histedit_in_progress(int *in_progress,
6495 struct got_worktree *worktree)
6497 const struct got_error *err;
6498 char *tmp_branch_name = NULL;
6500 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6501 if (err)
6502 return err;
6504 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6505 free(tmp_branch_name);
6506 return NULL;
6509 const struct got_error *
6510 got_worktree_histedit_continue(struct got_object_id **commit_id,
6511 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6512 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6513 struct got_worktree *worktree, struct got_repository *repo)
6515 const struct got_error *err;
6516 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6517 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6518 struct got_reference *commit_ref = NULL;
6519 struct got_reference *base_commit_ref = NULL;
6520 char *fileindex_path = NULL;
6521 int have_staged_files = 0;
6523 *commit_id = NULL;
6524 *tmp_branch = NULL;
6525 *base_commit_id = NULL;
6526 *fileindex = NULL;
6528 err = lock_worktree(worktree, LOCK_EX);
6529 if (err)
6530 return err;
6532 err = open_fileindex(fileindex, &fileindex_path, worktree);
6533 if (err)
6534 goto done;
6536 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6537 &have_staged_files);
6538 if (err && err->code != GOT_ERR_CANCELLED)
6539 goto done;
6540 if (have_staged_files) {
6541 err = got_error(GOT_ERR_STAGED_PATHS);
6542 goto done;
6545 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6546 if (err)
6547 goto done;
6549 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6550 if (err)
6551 goto done;
6553 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6554 if (err)
6555 goto done;
6557 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6558 worktree);
6559 if (err)
6560 goto done;
6562 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6563 if (err)
6564 goto done;
6566 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6567 if (err)
6568 goto done;
6569 err = got_ref_resolve(commit_id, repo, commit_ref);
6570 if (err)
6571 goto done;
6573 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6574 if (err)
6575 goto done;
6576 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6577 if (err)
6578 goto done;
6580 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6581 if (err)
6582 goto done;
6583 done:
6584 free(commit_ref_name);
6585 free(branch_ref_name);
6586 free(fileindex_path);
6587 if (commit_ref)
6588 got_ref_close(commit_ref);
6589 if (base_commit_ref)
6590 got_ref_close(base_commit_ref);
6591 if (err) {
6592 free(*commit_id);
6593 *commit_id = NULL;
6594 free(*base_commit_id);
6595 *base_commit_id = NULL;
6596 if (*tmp_branch) {
6597 got_ref_close(*tmp_branch);
6598 *tmp_branch = NULL;
6600 if (*fileindex) {
6601 got_fileindex_free(*fileindex);
6602 *fileindex = NULL;
6604 lock_worktree(worktree, LOCK_EX);
6606 return err;
6609 static const struct got_error *
6610 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6612 const struct got_error *err;
6613 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6614 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6616 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6617 if (err)
6618 goto done;
6619 err = delete_ref(tmp_branch_name, repo);
6620 if (err)
6621 goto done;
6623 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6624 worktree);
6625 if (err)
6626 goto done;
6627 err = delete_ref(base_commit_ref_name, repo);
6628 if (err)
6629 goto done;
6631 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6632 if (err)
6633 goto done;
6634 err = delete_ref(branch_ref_name, repo);
6635 if (err)
6636 goto done;
6638 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6639 if (err)
6640 goto done;
6641 err = delete_ref(commit_ref_name, repo);
6642 if (err)
6643 goto done;
6644 done:
6645 free(tmp_branch_name);
6646 free(base_commit_ref_name);
6647 free(branch_ref_name);
6648 free(commit_ref_name);
6649 return err;
6652 const struct got_error *
6653 got_worktree_histedit_abort(struct got_worktree *worktree,
6654 struct got_fileindex *fileindex, struct got_repository *repo,
6655 struct got_reference *branch, struct got_object_id *base_commit_id,
6656 got_worktree_checkout_cb progress_cb, void *progress_arg)
6658 const struct got_error *err, *unlockerr, *sync_err;
6659 struct got_reference *resolved = NULL;
6660 char *fileindex_path = NULL;
6661 struct got_object_id *tree_id = NULL;
6662 struct revert_file_args rfa;
6664 err = lock_worktree(worktree, LOCK_EX);
6665 if (err)
6666 return err;
6668 err = got_ref_open(&resolved, repo,
6669 got_ref_get_symref_target(branch), 0);
6670 if (err)
6671 goto done;
6673 err = got_worktree_set_head_ref(worktree, resolved);
6674 if (err)
6675 goto done;
6677 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6678 if (err)
6679 goto done;
6681 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6682 worktree->path_prefix);
6683 if (err)
6684 goto done;
6686 err = delete_histedit_refs(worktree, repo);
6687 if (err)
6688 goto done;
6690 err = get_fileindex_path(&fileindex_path, worktree);
6691 if (err)
6692 goto done;
6694 rfa.worktree = worktree;
6695 rfa.fileindex = fileindex;
6696 rfa.progress_cb = progress_cb;
6697 rfa.progress_arg = progress_arg;
6698 rfa.patch_cb = NULL;
6699 rfa.patch_arg = NULL;
6700 rfa.repo = repo;
6701 err = worktree_status(worktree, "", fileindex, repo,
6702 revert_file, &rfa, NULL, NULL, 0, 0);
6703 if (err)
6704 goto sync;
6706 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6707 repo, progress_cb, progress_arg, NULL, NULL);
6708 sync:
6709 sync_err = sync_fileindex(fileindex, fileindex_path);
6710 if (sync_err && err == NULL)
6711 err = sync_err;
6712 done:
6713 got_ref_close(resolved);
6714 free(tree_id);
6715 free(fileindex_path);
6717 unlockerr = lock_worktree(worktree, LOCK_SH);
6718 if (unlockerr && err == NULL)
6719 err = unlockerr;
6720 return err;
6723 const struct got_error *
6724 got_worktree_histedit_complete(struct got_worktree *worktree,
6725 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6726 struct got_reference *edited_branch, struct got_repository *repo)
6728 const struct got_error *err, *unlockerr;
6729 struct got_object_id *new_head_commit_id = NULL;
6730 struct got_reference *resolved = NULL;
6732 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6733 if (err)
6734 return err;
6736 err = got_ref_open(&resolved, repo,
6737 got_ref_get_symref_target(edited_branch), 0);
6738 if (err)
6739 goto done;
6741 err = got_ref_change_ref(resolved, new_head_commit_id);
6742 if (err)
6743 goto done;
6745 err = got_ref_write(resolved, repo);
6746 if (err)
6747 goto done;
6749 err = got_worktree_set_head_ref(worktree, resolved);
6750 if (err)
6751 goto done;
6753 err = delete_histedit_refs(worktree, repo);
6754 done:
6755 if (fileindex)
6756 got_fileindex_free(fileindex);
6757 free(new_head_commit_id);
6758 unlockerr = lock_worktree(worktree, LOCK_SH);
6759 if (unlockerr && err == NULL)
6760 err = unlockerr;
6761 return err;
6764 const struct got_error *
6765 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6766 struct got_object_id *commit_id, struct got_repository *repo)
6768 const struct got_error *err;
6769 char *commit_ref_name;
6771 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6772 if (err)
6773 return err;
6775 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6776 if (err)
6777 goto done;
6779 err = delete_ref(commit_ref_name, repo);
6780 done:
6781 free(commit_ref_name);
6782 return err;
6785 const struct got_error *
6786 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6787 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6788 struct got_worktree *worktree, const char *refname,
6789 struct got_repository *repo)
6791 const struct got_error *err = NULL;
6792 char *fileindex_path = NULL;
6793 struct check_rebase_ok_arg ok_arg;
6795 *fileindex = NULL;
6796 *branch_ref = NULL;
6797 *base_branch_ref = NULL;
6799 err = lock_worktree(worktree, LOCK_EX);
6800 if (err)
6801 return err;
6803 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6804 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6805 "cannot integrate a branch into itself; "
6806 "update -b or different branch name required");
6807 goto done;
6810 err = open_fileindex(fileindex, &fileindex_path, worktree);
6811 if (err)
6812 goto done;
6814 /* Preconditions are the same as for rebase. */
6815 ok_arg.worktree = worktree;
6816 ok_arg.repo = repo;
6817 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6818 &ok_arg);
6819 if (err)
6820 goto done;
6822 err = got_ref_open(branch_ref, repo, refname, 1);
6823 if (err)
6824 goto done;
6826 err = got_ref_open(base_branch_ref, repo,
6827 got_worktree_get_head_ref_name(worktree), 1);
6828 done:
6829 if (err) {
6830 if (*branch_ref) {
6831 got_ref_close(*branch_ref);
6832 *branch_ref = NULL;
6834 if (*base_branch_ref) {
6835 got_ref_close(*base_branch_ref);
6836 *base_branch_ref = NULL;
6838 if (*fileindex) {
6839 got_fileindex_free(*fileindex);
6840 *fileindex = NULL;
6842 lock_worktree(worktree, LOCK_SH);
6844 return err;
6847 const struct got_error *
6848 got_worktree_integrate_continue(struct got_worktree *worktree,
6849 struct got_fileindex *fileindex, struct got_repository *repo,
6850 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6851 got_worktree_checkout_cb progress_cb, void *progress_arg,
6852 got_cancel_cb cancel_cb, void *cancel_arg)
6854 const struct got_error *err = NULL, *sync_err, *unlockerr;
6855 char *fileindex_path = NULL;
6856 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6858 err = get_fileindex_path(&fileindex_path, worktree);
6859 if (err)
6860 goto done;
6862 err = got_ref_resolve(&commit_id, repo, branch_ref);
6863 if (err)
6864 goto done;
6866 err = got_object_id_by_path(&tree_id, repo, commit_id,
6867 worktree->path_prefix);
6868 if (err)
6869 goto done;
6871 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6872 if (err)
6873 goto done;
6875 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6876 progress_cb, progress_arg, cancel_cb, cancel_arg);
6877 if (err)
6878 goto sync;
6880 err = got_ref_change_ref(base_branch_ref, commit_id);
6881 if (err)
6882 goto sync;
6884 err = got_ref_write(base_branch_ref, repo);
6885 sync:
6886 sync_err = sync_fileindex(fileindex, fileindex_path);
6887 if (sync_err && err == NULL)
6888 err = sync_err;
6890 done:
6891 unlockerr = got_ref_unlock(branch_ref);
6892 if (unlockerr && err == NULL)
6893 err = unlockerr;
6894 got_ref_close(branch_ref);
6896 unlockerr = got_ref_unlock(base_branch_ref);
6897 if (unlockerr && err == NULL)
6898 err = unlockerr;
6899 got_ref_close(base_branch_ref);
6901 got_fileindex_free(fileindex);
6902 free(fileindex_path);
6903 free(tree_id);
6905 unlockerr = lock_worktree(worktree, LOCK_SH);
6906 if (unlockerr && err == NULL)
6907 err = unlockerr;
6908 return err;
6911 const struct got_error *
6912 got_worktree_integrate_abort(struct got_worktree *worktree,
6913 struct got_fileindex *fileindex, struct got_repository *repo,
6914 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6916 const struct got_error *err = NULL, *unlockerr = NULL;
6918 got_fileindex_free(fileindex);
6920 err = lock_worktree(worktree, LOCK_SH);
6922 unlockerr = got_ref_unlock(branch_ref);
6923 if (unlockerr && err == NULL)
6924 err = unlockerr;
6925 got_ref_close(branch_ref);
6927 unlockerr = got_ref_unlock(base_branch_ref);
6928 if (unlockerr && err == NULL)
6929 err = unlockerr;
6930 got_ref_close(base_branch_ref);
6932 return err;
6935 struct check_stage_ok_arg {
6936 struct got_object_id *head_commit_id;
6937 struct got_worktree *worktree;
6938 struct got_fileindex *fileindex;
6939 struct got_repository *repo;
6940 int have_changes;
6943 const struct got_error *
6944 check_stage_ok(void *arg, unsigned char status,
6945 unsigned char staged_status, const char *relpath,
6946 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6947 struct got_object_id *commit_id, int dirfd, const char *de_name)
6949 struct check_stage_ok_arg *a = arg;
6950 const struct got_error *err = NULL;
6951 struct got_fileindex_entry *ie;
6952 struct got_object_id base_commit_id;
6953 struct got_object_id *base_commit_idp = NULL;
6954 char *in_repo_path = NULL, *p;
6956 if (status == GOT_STATUS_UNVERSIONED ||
6957 status == GOT_STATUS_NO_CHANGE)
6958 return NULL;
6959 if (status == GOT_STATUS_NONEXISTENT)
6960 return got_error_set_errno(ENOENT, relpath);
6962 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6963 if (ie == NULL)
6964 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6966 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6967 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6968 relpath) == -1)
6969 return got_error_from_errno("asprintf");
6971 if (got_fileindex_entry_has_commit(ie)) {
6972 memcpy(base_commit_id.sha1, ie->commit_sha1,
6973 SHA1_DIGEST_LENGTH);
6974 base_commit_idp = &base_commit_id;
6977 if (status == GOT_STATUS_CONFLICT) {
6978 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6979 goto done;
6980 } else if (status != GOT_STATUS_ADD &&
6981 status != GOT_STATUS_MODIFY &&
6982 status != GOT_STATUS_DELETE) {
6983 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6984 goto done;
6987 a->have_changes = 1;
6989 p = in_repo_path;
6990 while (p[0] == '/')
6991 p++;
6992 err = check_out_of_date(p, status, staged_status,
6993 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6994 GOT_ERR_STAGE_OUT_OF_DATE);
6995 done:
6996 free(in_repo_path);
6997 return err;
7000 struct stage_path_arg {
7001 struct got_worktree *worktree;
7002 struct got_fileindex *fileindex;
7003 struct got_repository *repo;
7004 got_worktree_status_cb status_cb;
7005 void *status_arg;
7006 got_worktree_patch_cb patch_cb;
7007 void *patch_arg;
7008 int staged_something;
7009 int allow_bad_symlinks;
7012 static const struct got_error *
7013 stage_path(void *arg, unsigned char status,
7014 unsigned char staged_status, const char *relpath,
7015 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7016 struct got_object_id *commit_id, int dirfd, const char *de_name)
7018 struct stage_path_arg *a = arg;
7019 const struct got_error *err = NULL;
7020 struct got_fileindex_entry *ie;
7021 char *ondisk_path = NULL, *path_content = NULL;
7022 uint32_t stage;
7023 struct got_object_id *new_staged_blob_id = NULL;
7024 struct stat sb;
7026 if (status == GOT_STATUS_UNVERSIONED)
7027 return NULL;
7029 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7030 if (ie == NULL)
7031 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7033 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7034 relpath)== -1)
7035 return got_error_from_errno("asprintf");
7037 switch (status) {
7038 case GOT_STATUS_ADD:
7039 case GOT_STATUS_MODIFY:
7040 /* XXX could sb.st_mode be passed in by our caller? */
7041 if (lstat(ondisk_path, &sb) == -1) {
7042 err = got_error_from_errno2("lstat", ondisk_path);
7043 break;
7045 if (a->patch_cb) {
7046 if (status == GOT_STATUS_ADD) {
7047 int choice = GOT_PATCH_CHOICE_NONE;
7048 err = (*a->patch_cb)(&choice, a->patch_arg,
7049 status, ie->path, NULL, 1, 1);
7050 if (err)
7051 break;
7052 if (choice != GOT_PATCH_CHOICE_YES)
7053 break;
7054 } else {
7055 err = create_patched_content(&path_content, 0,
7056 staged_blob_id ? staged_blob_id : blob_id,
7057 ondisk_path, dirfd, de_name, ie->path,
7058 a->repo, a->patch_cb, a->patch_arg);
7059 if (err || path_content == NULL)
7060 break;
7063 err = got_object_blob_create(&new_staged_blob_id,
7064 path_content ? path_content : ondisk_path, a->repo);
7065 if (err)
7066 break;
7067 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7068 SHA1_DIGEST_LENGTH);
7069 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7070 stage = GOT_FILEIDX_STAGE_ADD;
7071 else
7072 stage = GOT_FILEIDX_STAGE_MODIFY;
7073 got_fileindex_entry_stage_set(ie, stage);
7074 if (S_ISLNK(sb.st_mode)) {
7075 int is_bad_symlink = 0;
7076 if (!a->allow_bad_symlinks) {
7077 char target_path[PATH_MAX];
7078 ssize_t target_len;
7079 target_len = readlink(ondisk_path, target_path,
7080 sizeof(target_path));
7081 if (target_len == -1) {
7082 err = got_error_from_errno2("readlink",
7083 ondisk_path);
7084 break;
7086 err = is_bad_symlink_target(&is_bad_symlink,
7087 target_path, target_len, ondisk_path,
7088 a->worktree->root_path);
7089 if (err)
7090 break;
7091 if (is_bad_symlink) {
7092 err = got_error_path(ondisk_path,
7093 GOT_ERR_BAD_SYMLINK);
7094 break;
7097 if (is_bad_symlink)
7098 got_fileindex_entry_staged_filetype_set(ie,
7099 GOT_FILEIDX_MODE_BAD_SYMLINK);
7100 else
7101 got_fileindex_entry_staged_filetype_set(ie,
7102 GOT_FILEIDX_MODE_SYMLINK);
7103 } else {
7104 got_fileindex_entry_staged_filetype_set(ie,
7105 GOT_FILEIDX_MODE_REGULAR_FILE);
7107 a->staged_something = 1;
7108 if (a->status_cb == NULL)
7109 break;
7110 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7111 get_staged_status(ie), relpath, blob_id,
7112 new_staged_blob_id, NULL, dirfd, de_name);
7113 break;
7114 case GOT_STATUS_DELETE:
7115 if (staged_status == GOT_STATUS_DELETE)
7116 break;
7117 if (a->patch_cb) {
7118 int choice = GOT_PATCH_CHOICE_NONE;
7119 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7120 ie->path, NULL, 1, 1);
7121 if (err)
7122 break;
7123 if (choice == GOT_PATCH_CHOICE_NO)
7124 break;
7125 if (choice != GOT_PATCH_CHOICE_YES) {
7126 err = got_error(GOT_ERR_PATCH_CHOICE);
7127 break;
7130 stage = GOT_FILEIDX_STAGE_DELETE;
7131 got_fileindex_entry_stage_set(ie, stage);
7132 a->staged_something = 1;
7133 if (a->status_cb == NULL)
7134 break;
7135 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7136 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7137 de_name);
7138 break;
7139 case GOT_STATUS_NO_CHANGE:
7140 break;
7141 case GOT_STATUS_CONFLICT:
7142 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7143 break;
7144 case GOT_STATUS_NONEXISTENT:
7145 err = got_error_set_errno(ENOENT, relpath);
7146 break;
7147 default:
7148 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7149 break;
7152 if (path_content && unlink(path_content) == -1 && err == NULL)
7153 err = got_error_from_errno2("unlink", path_content);
7154 free(path_content);
7155 free(ondisk_path);
7156 free(new_staged_blob_id);
7157 return err;
7160 const struct got_error *
7161 got_worktree_stage(struct got_worktree *worktree,
7162 struct got_pathlist_head *paths,
7163 got_worktree_status_cb status_cb, void *status_arg,
7164 got_worktree_patch_cb patch_cb, void *patch_arg,
7165 int allow_bad_symlinks, struct got_repository *repo)
7167 const struct got_error *err = NULL, *sync_err, *unlockerr;
7168 struct got_pathlist_entry *pe;
7169 struct got_fileindex *fileindex = NULL;
7170 char *fileindex_path = NULL;
7171 struct got_reference *head_ref = NULL;
7172 struct got_object_id *head_commit_id = NULL;
7173 struct check_stage_ok_arg oka;
7174 struct stage_path_arg spa;
7176 err = lock_worktree(worktree, LOCK_EX);
7177 if (err)
7178 return err;
7180 err = got_ref_open(&head_ref, repo,
7181 got_worktree_get_head_ref_name(worktree), 0);
7182 if (err)
7183 goto done;
7184 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7185 if (err)
7186 goto done;
7187 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7188 if (err)
7189 goto done;
7191 /* Check pre-conditions before staging anything. */
7192 oka.head_commit_id = head_commit_id;
7193 oka.worktree = worktree;
7194 oka.fileindex = fileindex;
7195 oka.repo = repo;
7196 oka.have_changes = 0;
7197 TAILQ_FOREACH(pe, paths, entry) {
7198 err = worktree_status(worktree, pe->path, fileindex, repo,
7199 check_stage_ok, &oka, NULL, NULL, 0, 0);
7200 if (err)
7201 goto done;
7203 if (!oka.have_changes) {
7204 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7205 goto done;
7208 spa.worktree = worktree;
7209 spa.fileindex = fileindex;
7210 spa.repo = repo;
7211 spa.patch_cb = patch_cb;
7212 spa.patch_arg = patch_arg;
7213 spa.status_cb = status_cb;
7214 spa.status_arg = status_arg;
7215 spa.staged_something = 0;
7216 spa.allow_bad_symlinks = allow_bad_symlinks;
7217 TAILQ_FOREACH(pe, paths, entry) {
7218 err = worktree_status(worktree, pe->path, fileindex, repo,
7219 stage_path, &spa, NULL, NULL, 0, 0);
7220 if (err)
7221 goto done;
7223 if (!spa.staged_something) {
7224 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7225 goto done;
7228 sync_err = sync_fileindex(fileindex, fileindex_path);
7229 if (sync_err && err == NULL)
7230 err = sync_err;
7231 done:
7232 if (head_ref)
7233 got_ref_close(head_ref);
7234 free(head_commit_id);
7235 free(fileindex_path);
7236 if (fileindex)
7237 got_fileindex_free(fileindex);
7238 unlockerr = lock_worktree(worktree, LOCK_SH);
7239 if (unlockerr && err == NULL)
7240 err = unlockerr;
7241 return err;
7244 struct unstage_path_arg {
7245 struct got_worktree *worktree;
7246 struct got_fileindex *fileindex;
7247 struct got_repository *repo;
7248 got_worktree_checkout_cb progress_cb;
7249 void *progress_arg;
7250 got_worktree_patch_cb patch_cb;
7251 void *patch_arg;
7254 static const struct got_error *
7255 create_unstaged_content(char **path_unstaged_content,
7256 char **path_new_staged_content, struct got_object_id *blob_id,
7257 struct got_object_id *staged_blob_id, const char *relpath,
7258 struct got_repository *repo,
7259 got_worktree_patch_cb patch_cb, void *patch_arg)
7261 const struct got_error *err;
7262 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7263 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7264 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7265 struct stat sb1, sb2;
7266 struct got_diff_changes *changes = NULL;
7267 struct got_diff_state *ds = NULL;
7268 struct got_diff_args *args = NULL;
7269 struct got_diff_change *change;
7270 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7271 int have_content = 0, have_rejected_content = 0;
7273 *path_unstaged_content = NULL;
7274 *path_new_staged_content = NULL;
7276 err = got_object_id_str(&label1, blob_id);
7277 if (err)
7278 return err;
7279 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7280 if (err)
7281 goto done;
7283 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7284 if (err)
7285 goto done;
7287 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7288 if (err)
7289 goto done;
7291 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7292 if (err)
7293 goto done;
7295 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7296 if (err)
7297 goto done;
7299 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7300 if (err)
7301 goto done;
7303 if (stat(path1, &sb1) == -1) {
7304 err = got_error_from_errno2("stat", path1);
7305 goto done;
7308 if (stat(path2, &sb2) == -1) {
7309 err = got_error_from_errno2("stat", path2);
7310 goto done;
7313 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7314 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7315 if (err)
7316 goto done;
7318 err = got_opentemp_named(path_unstaged_content, &outfile,
7319 "got-unstaged-content");
7320 if (err)
7321 goto done;
7322 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7323 "got-new-staged-content");
7324 if (err)
7325 goto done;
7327 if (fseek(f1, 0L, SEEK_SET) == -1) {
7328 err = got_ferror(f1, GOT_ERR_IO);
7329 goto done;
7331 if (fseek(f2, 0L, SEEK_SET) == -1) {
7332 err = got_ferror(f2, GOT_ERR_IO);
7333 goto done;
7335 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7336 int choice;
7337 err = apply_or_reject_change(&choice, change, ++n,
7338 changes->nchanges, ds, args, diff_flags, relpath,
7339 f1, f2, &line_cur1, &line_cur2,
7340 outfile, rejectfile, patch_cb, patch_arg);
7341 if (err)
7342 goto done;
7343 if (choice == GOT_PATCH_CHOICE_YES)
7344 have_content = 1;
7345 else
7346 have_rejected_content = 1;
7347 if (choice == GOT_PATCH_CHOICE_QUIT)
7348 break;
7350 if (have_content || have_rejected_content)
7351 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7352 outfile, rejectfile);
7353 done:
7354 free(label1);
7355 if (blob)
7356 got_object_blob_close(blob);
7357 if (staged_blob)
7358 got_object_blob_close(staged_blob);
7359 if (f1 && fclose(f1) == EOF && err == NULL)
7360 err = got_error_from_errno2("fclose", path1);
7361 if (f2 && fclose(f2) == EOF && err == NULL)
7362 err = got_error_from_errno2("fclose", path2);
7363 if (outfile && fclose(outfile) == EOF && err == NULL)
7364 err = got_error_from_errno2("fclose", *path_unstaged_content);
7365 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7366 err = got_error_from_errno2("fclose", *path_new_staged_content);
7367 if (path1 && unlink(path1) == -1 && err == NULL)
7368 err = got_error_from_errno2("unlink", path1);
7369 if (path2 && unlink(path2) == -1 && err == NULL)
7370 err = got_error_from_errno2("unlink", path2);
7371 if (err || !have_content) {
7372 if (*path_unstaged_content &&
7373 unlink(*path_unstaged_content) == -1 && err == NULL)
7374 err = got_error_from_errno2("unlink",
7375 *path_unstaged_content);
7376 free(*path_unstaged_content);
7377 *path_unstaged_content = NULL;
7379 if (err || !have_content || !have_rejected_content) {
7380 if (*path_new_staged_content &&
7381 unlink(*path_new_staged_content) == -1 && err == NULL)
7382 err = got_error_from_errno2("unlink",
7383 *path_new_staged_content);
7384 free(*path_new_staged_content);
7385 *path_new_staged_content = NULL;
7387 free(args);
7388 if (ds) {
7389 got_diff_state_free(ds);
7390 free(ds);
7392 if (changes)
7393 got_diff_free_changes(changes);
7394 free(path1);
7395 free(path2);
7396 return err;
7399 static const struct got_error *
7400 unstage_hunks(struct got_object_id *staged_blob_id,
7401 struct got_blob_object *blob_base,
7402 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7403 const char *ondisk_path, const char *label_orig,
7404 struct got_worktree *worktree, struct got_repository *repo,
7405 got_worktree_patch_cb patch_cb, void *patch_arg,
7406 got_worktree_checkout_cb progress_cb, void *progress_arg)
7408 const struct got_error *err = NULL;
7409 char *path_unstaged_content = NULL;
7410 char *path_new_staged_content = NULL;
7411 struct got_object_id *new_staged_blob_id = NULL;
7412 FILE *f = NULL;
7413 struct stat sb;
7415 err = create_unstaged_content(&path_unstaged_content,
7416 &path_new_staged_content, blob_id, staged_blob_id,
7417 ie->path, repo, patch_cb, patch_arg);
7418 if (err)
7419 return err;
7421 if (path_unstaged_content == NULL)
7422 return NULL;
7424 if (path_new_staged_content) {
7425 err = got_object_blob_create(&new_staged_blob_id,
7426 path_new_staged_content, repo);
7427 if (err)
7428 goto done;
7431 f = fopen(path_unstaged_content, "r");
7432 if (f == NULL) {
7433 err = got_error_from_errno2("fopen",
7434 path_unstaged_content);
7435 goto done;
7437 if (fstat(fileno(f), &sb) == -1) {
7438 err = got_error_from_errno2("fstat", path_unstaged_content);
7439 goto done;
7441 if (got_fileindex_entry_staged_filetype_get(ie) ==
7442 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7443 char link_target[PATH_MAX];
7444 size_t r;
7445 r = fread(link_target, 1, sizeof(link_target), f);
7446 if (r == 0 && ferror(f)) {
7447 err = got_error_from_errno("fread");
7448 goto done;
7450 if (r >= sizeof(link_target)) { /* should not happen */
7451 err = got_error(GOT_ERR_NO_SPACE);
7452 goto done;
7454 link_target[r] = '\0';
7455 err = merge_symlink(worktree, blob_base,
7456 ondisk_path, ie->path, label_orig, link_target,
7457 worktree->base_commit_id, repo, progress_cb,
7458 progress_arg);
7459 } else {
7460 int local_changes_subsumed;
7461 err = merge_file(&local_changes_subsumed, worktree,
7462 blob_base, ondisk_path, ie->path,
7463 got_fileindex_perms_to_st(ie),
7464 path_unstaged_content, label_orig, "unstaged",
7465 repo, progress_cb, progress_arg);
7467 if (err)
7468 goto done;
7470 if (new_staged_blob_id) {
7471 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7472 SHA1_DIGEST_LENGTH);
7473 } else
7474 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7475 done:
7476 free(new_staged_blob_id);
7477 if (path_unstaged_content &&
7478 unlink(path_unstaged_content) == -1 && err == NULL)
7479 err = got_error_from_errno2("unlink", path_unstaged_content);
7480 if (path_new_staged_content &&
7481 unlink(path_new_staged_content) == -1 && err == NULL)
7482 err = got_error_from_errno2("unlink", path_new_staged_content);
7483 if (f && fclose(f) != 0 && err == NULL)
7484 err = got_error_from_errno2("fclose", path_unstaged_content);
7485 free(path_unstaged_content);
7486 free(path_new_staged_content);
7487 return err;
7490 static const struct got_error *
7491 unstage_path(void *arg, unsigned char status,
7492 unsigned char staged_status, const char *relpath,
7493 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7494 struct got_object_id *commit_id, int dirfd, const char *de_name)
7496 const struct got_error *err = NULL;
7497 struct unstage_path_arg *a = arg;
7498 struct got_fileindex_entry *ie;
7499 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7500 char *ondisk_path = NULL;
7501 char *id_str = NULL, *label_orig = NULL;
7502 int local_changes_subsumed;
7503 struct stat sb;
7505 if (staged_status != GOT_STATUS_ADD &&
7506 staged_status != GOT_STATUS_MODIFY &&
7507 staged_status != GOT_STATUS_DELETE)
7508 return NULL;
7510 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7511 if (ie == NULL)
7512 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7514 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7515 == -1)
7516 return got_error_from_errno("asprintf");
7518 err = got_object_id_str(&id_str,
7519 commit_id ? commit_id : a->worktree->base_commit_id);
7520 if (err)
7521 goto done;
7522 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7523 id_str) == -1) {
7524 err = got_error_from_errno("asprintf");
7525 goto done;
7528 switch (staged_status) {
7529 case GOT_STATUS_MODIFY:
7530 err = got_object_open_as_blob(&blob_base, a->repo,
7531 blob_id, 8192);
7532 if (err)
7533 break;
7534 /* fall through */
7535 case GOT_STATUS_ADD:
7536 if (a->patch_cb) {
7537 if (staged_status == GOT_STATUS_ADD) {
7538 int choice = GOT_PATCH_CHOICE_NONE;
7539 err = (*a->patch_cb)(&choice, a->patch_arg,
7540 staged_status, ie->path, NULL, 1, 1);
7541 if (err)
7542 break;
7543 if (choice != GOT_PATCH_CHOICE_YES)
7544 break;
7545 } else {
7546 err = unstage_hunks(staged_blob_id,
7547 blob_base, blob_id, ie, ondisk_path,
7548 label_orig, a->worktree, a->repo,
7549 a->patch_cb, a->patch_arg,
7550 a->progress_cb, a->progress_arg);
7551 break; /* Done with this file. */
7554 err = got_object_open_as_blob(&blob_staged, a->repo,
7555 staged_blob_id, 8192);
7556 if (err)
7557 break;
7558 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7559 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7560 case GOT_FILEIDX_MODE_REGULAR_FILE:
7561 err = merge_blob(&local_changes_subsumed, a->worktree,
7562 blob_base, ondisk_path, relpath,
7563 got_fileindex_perms_to_st(ie), label_orig,
7564 blob_staged, commit_id ? commit_id :
7565 a->worktree->base_commit_id, a->repo,
7566 a->progress_cb, a->progress_arg);
7567 break;
7568 case GOT_FILEIDX_MODE_SYMLINK:
7569 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7570 char *staged_target;
7571 err = got_object_blob_read_to_str(
7572 &staged_target, blob_staged);
7573 if (err)
7574 goto done;
7575 err = merge_symlink(a->worktree, blob_base,
7576 ondisk_path, relpath, label_orig,
7577 staged_target, commit_id ? commit_id :
7578 a->worktree->base_commit_id,
7579 a->repo, a->progress_cb, a->progress_arg);
7580 free(staged_target);
7581 } else {
7582 err = merge_blob(&local_changes_subsumed,
7583 a->worktree, blob_base, ondisk_path,
7584 relpath, got_fileindex_perms_to_st(ie),
7585 label_orig, blob_staged,
7586 commit_id ? commit_id :
7587 a->worktree->base_commit_id, a->repo,
7588 a->progress_cb, a->progress_arg);
7590 break;
7591 default:
7592 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7593 break;
7595 if (err == NULL)
7596 got_fileindex_entry_stage_set(ie,
7597 GOT_FILEIDX_STAGE_NONE);
7598 break;
7599 case GOT_STATUS_DELETE:
7600 if (a->patch_cb) {
7601 int choice = GOT_PATCH_CHOICE_NONE;
7602 err = (*a->patch_cb)(&choice, a->patch_arg,
7603 staged_status, ie->path, NULL, 1, 1);
7604 if (err)
7605 break;
7606 if (choice == GOT_PATCH_CHOICE_NO)
7607 break;
7608 if (choice != GOT_PATCH_CHOICE_YES) {
7609 err = got_error(GOT_ERR_PATCH_CHOICE);
7610 break;
7613 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7614 err = get_file_status(&status, &sb, ie, ondisk_path,
7615 dirfd, de_name, a->repo);
7616 if (err)
7617 break;
7618 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7619 break;
7621 done:
7622 free(ondisk_path);
7623 if (blob_base)
7624 got_object_blob_close(blob_base);
7625 if (blob_staged)
7626 got_object_blob_close(blob_staged);
7627 free(id_str);
7628 free(label_orig);
7629 return err;
7632 const struct got_error *
7633 got_worktree_unstage(struct got_worktree *worktree,
7634 struct got_pathlist_head *paths,
7635 got_worktree_checkout_cb progress_cb, void *progress_arg,
7636 got_worktree_patch_cb patch_cb, void *patch_arg,
7637 struct got_repository *repo)
7639 const struct got_error *err = NULL, *sync_err, *unlockerr;
7640 struct got_pathlist_entry *pe;
7641 struct got_fileindex *fileindex = NULL;
7642 char *fileindex_path = NULL;
7643 struct unstage_path_arg upa;
7645 err = lock_worktree(worktree, LOCK_EX);
7646 if (err)
7647 return err;
7649 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7650 if (err)
7651 goto done;
7653 upa.worktree = worktree;
7654 upa.fileindex = fileindex;
7655 upa.repo = repo;
7656 upa.progress_cb = progress_cb;
7657 upa.progress_arg = progress_arg;
7658 upa.patch_cb = patch_cb;
7659 upa.patch_arg = patch_arg;
7660 TAILQ_FOREACH(pe, paths, entry) {
7661 err = worktree_status(worktree, pe->path, fileindex, repo,
7662 unstage_path, &upa, NULL, NULL, 0, 0);
7663 if (err)
7664 goto done;
7667 sync_err = sync_fileindex(fileindex, fileindex_path);
7668 if (sync_err && err == NULL)
7669 err = sync_err;
7670 done:
7671 free(fileindex_path);
7672 if (fileindex)
7673 got_fileindex_free(fileindex);
7674 unlockerr = lock_worktree(worktree, LOCK_SH);
7675 if (unlockerr && err == NULL)
7676 err = unlockerr;
7677 return err;