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"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) != 0 && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
124 char *path;
125 int fd = -1;
126 ssize_t n;
127 struct stat sb;
129 *content = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
133 path = NULL;
134 goto done;
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (fd == -1) {
139 if (errno == ENOENT)
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
148 goto done;
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
153 goto done;
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
188 char *refstr = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
192 if (refstr == NULL)
193 return got_error_from_errno("got_ref_to_str");
194 } else {
195 refstr = strdup(got_ref_get_name(head_ref));
196 if (refstr == NULL)
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 free(refstr);
201 return err;
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
210 uuid_t uuid;
211 uint32_t uuid_status;
212 int obj_type;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
221 goto done;
224 err = got_ref_resolve(&commit_id, repo, head_ref);
225 if (err)
226 return err;
227 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (err)
229 return err;
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
241 goto done;
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
247 goto done;
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
251 goto done;
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 if (err)
257 goto done;
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 if (err)
262 goto done;
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(absprefix);
318 free(basestr);
319 free(uuidstr);
320 return err;
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
327 char *path_got;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
427 goto done;
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
432 done:
433 if (repo)
434 got_repo_close(repo);
435 free(path_got);
436 free(path_lock);
437 free(base_commit_id_str);
438 free(uuidstr);
439 free(formatstr);
440 if (err) {
441 if (fd != -1)
442 close(fd);
443 if (*worktree != NULL)
444 got_worktree_close(*worktree);
445 *worktree = NULL;
446 } else
447 (*worktree)->lockfd = fd;
449 return err;
452 const struct got_error *
453 got_worktree_open(struct got_worktree **worktree, const char *path)
455 const struct got_error *err = NULL;
457 do {
458 err = open_worktree(worktree, path);
459 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
460 return err;
461 if (*worktree)
462 return NULL;
463 path = dirname(path);
464 if (path == NULL)
465 return got_error_from_errno2("dirname", path);
466 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
468 return got_error(GOT_ERR_NOT_WORKTREE);
471 const struct got_error *
472 got_worktree_close(struct got_worktree *worktree)
474 const struct got_error *err = NULL;
475 free(worktree->repo_path);
476 free(worktree->path_prefix);
477 free(worktree->base_commit_id);
478 free(worktree->head_ref_name);
479 if (worktree->lockfd != -1)
480 if (close(worktree->lockfd) != 0)
481 err = got_error_from_errno2("close",
482 got_worktree_get_root_path(worktree));
483 free(worktree->root_path);
484 free(worktree->gotconfig_path);
485 got_gotconfig_free(worktree->gotconfig);
486 free(worktree);
487 return err;
490 const char *
491 got_worktree_get_root_path(struct got_worktree *worktree)
493 return worktree->root_path;
496 const char *
497 got_worktree_get_repo_path(struct got_worktree *worktree)
499 return worktree->repo_path;
501 const char *
502 got_worktree_get_path_prefix(struct got_worktree *worktree)
504 return worktree->path_prefix;
507 const struct got_error *
508 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
509 const char *path_prefix)
511 char *absprefix = NULL;
513 if (!got_path_is_absolute(path_prefix)) {
514 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
515 return got_error_from_errno("asprintf");
517 *match = (strcmp(absprefix ? absprefix : path_prefix,
518 worktree->path_prefix) == 0);
519 free(absprefix);
520 return NULL;
523 const char *
524 got_worktree_get_head_ref_name(struct got_worktree *worktree)
526 return worktree->head_ref_name;
529 const struct got_error *
530 got_worktree_set_head_ref(struct got_worktree *worktree,
531 struct got_reference *head_ref)
533 const struct got_error *err = NULL;
534 char *path_got = NULL, *head_ref_name = NULL;
536 if (asprintf(&path_got, "%s/%s", worktree->root_path,
537 GOT_WORKTREE_GOT_DIR) == -1) {
538 err = got_error_from_errno("asprintf");
539 path_got = NULL;
540 goto done;
543 head_ref_name = strdup(got_ref_get_name(head_ref));
544 if (head_ref_name == NULL) {
545 err = got_error_from_errno("strdup");
546 goto done;
549 err = write_head_ref(path_got, head_ref);
550 if (err)
551 goto done;
553 free(worktree->head_ref_name);
554 worktree->head_ref_name = head_ref_name;
555 done:
556 free(path_got);
557 if (err)
558 free(head_ref_name);
559 return err;
562 struct got_object_id *
563 got_worktree_get_base_commit_id(struct got_worktree *worktree)
565 return worktree->base_commit_id;
568 const struct got_error *
569 got_worktree_set_base_commit_id(struct got_worktree *worktree,
570 struct got_repository *repo, struct got_object_id *commit_id)
572 const struct got_error *err;
573 struct got_object *obj = NULL;
574 char *id_str = NULL;
575 char *path_got = NULL;
577 if (asprintf(&path_got, "%s/%s", worktree->root_path,
578 GOT_WORKTREE_GOT_DIR) == -1) {
579 err = got_error_from_errno("asprintf");
580 path_got = NULL;
581 goto done;
584 err = got_object_open(&obj, repo, commit_id);
585 if (err)
586 return err;
588 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
589 err = got_error(GOT_ERR_OBJ_TYPE);
590 goto done;
593 /* Record our base commit. */
594 err = got_object_id_str(&id_str, commit_id);
595 if (err)
596 goto done;
597 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
598 if (err)
599 goto done;
601 free(worktree->base_commit_id);
602 worktree->base_commit_id = got_object_id_dup(commit_id);
603 if (worktree->base_commit_id == NULL) {
604 err = got_error_from_errno("got_object_id_dup");
605 goto done;
607 done:
608 if (obj)
609 got_object_close(obj);
610 free(id_str);
611 free(path_got);
612 return err;
615 const struct got_gotconfig *
616 got_worktree_get_gotconfig(struct got_worktree *worktree)
618 return worktree->gotconfig;
621 static const struct got_error *
622 lock_worktree(struct got_worktree *worktree, int operation)
624 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
625 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
626 : got_error_from_errno2("flock",
627 got_worktree_get_root_path(worktree)));
628 return NULL;
631 static const struct got_error *
632 add_dir_on_disk(struct got_worktree *worktree, const char *path)
634 const struct got_error *err = NULL;
635 char *abspath;
637 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
638 return got_error_from_errno("asprintf");
640 err = got_path_mkdir(abspath);
641 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
642 struct stat sb;
643 err = NULL;
644 if (lstat(abspath, &sb) == -1) {
645 err = got_error_from_errno2("lstat", abspath);
646 } else if (!S_ISDIR(sb.st_mode)) {
647 /* TODO directory is obstructed; do something */
648 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
651 free(abspath);
652 return err;
655 static const struct got_error *
656 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
658 const struct got_error *err = NULL;
659 uint8_t fbuf1[8192];
660 uint8_t fbuf2[8192];
661 size_t flen1 = 0, flen2 = 0;
663 *same = 1;
665 for (;;) {
666 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
667 if (flen1 == 0 && ferror(f1)) {
668 err = got_error_from_errno("fread");
669 break;
671 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
672 if (flen2 == 0 && ferror(f2)) {
673 err = got_error_from_errno("fread");
674 break;
676 if (flen1 == 0) {
677 if (flen2 != 0)
678 *same = 0;
679 break;
680 } else if (flen2 == 0) {
681 if (flen1 != 0)
682 *same = 0;
683 break;
684 } else if (flen1 == flen2) {
685 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
686 *same = 0;
687 break;
689 } else {
690 *same = 0;
691 break;
695 return err;
698 static const struct got_error *
699 check_files_equal(int *same, const char *f1_path, const char *f2_path)
701 const struct got_error *err = NULL;
702 struct stat sb;
703 size_t size1, size2;
704 FILE *f1 = NULL, *f2 = NULL;
706 *same = 1;
708 if (lstat(f1_path, &sb) != 0) {
709 err = got_error_from_errno2("lstat", f1_path);
710 goto done;
712 size1 = sb.st_size;
714 if (lstat(f2_path, &sb) != 0) {
715 err = got_error_from_errno2("lstat", f2_path);
716 goto done;
718 size2 = sb.st_size;
720 if (size1 != size2) {
721 *same = 0;
722 return NULL;
725 f1 = fopen(f1_path, "r");
726 if (f1 == NULL)
727 return got_error_from_errno2("fopen", f1_path);
729 f2 = fopen(f2_path, "r");
730 if (f2 == NULL) {
731 err = got_error_from_errno2("fopen", f2_path);
732 goto done;
735 err = check_file_contents_equal(same, f1, f2);
736 done:
737 if (f1 && fclose(f1) != 0 && err == NULL)
738 err = got_error_from_errno("fclose");
739 if (f2 && fclose(f2) != 0 && err == NULL)
740 err = got_error_from_errno("fclose");
742 return err;
745 /*
746 * Perform a 3-way merge where blob_orig acts as the common ancestor,
747 * the file at deriv_path acts as the first derived version, and the
748 * file on disk acts as the second derived version.
749 */
750 static const struct got_error *
751 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
752 struct got_blob_object *blob_orig, const char *ondisk_path,
753 const char *path, uint16_t st_mode, const char *deriv_path,
754 const char *label_orig, const char *label_deriv,
755 struct got_repository *repo,
756 got_worktree_checkout_cb progress_cb, void *progress_arg)
758 const struct got_error *err = NULL;
759 int merged_fd = -1;
760 FILE *f_orig = NULL;
761 char *blob_orig_path = NULL;
762 char *merged_path = NULL, *base_path = NULL;
763 int overlapcnt = 0;
764 char *parent;
765 char *symlink_path = NULL;
766 FILE *symlinkf = NULL;
768 *local_changes_subsumed = 0;
770 parent = dirname(ondisk_path);
771 if (parent == NULL)
772 return got_error_from_errno2("dirname", ondisk_path);
774 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
775 return got_error_from_errno("asprintf");
777 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
778 if (err)
779 goto done;
781 free(base_path);
782 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
783 err = got_error_from_errno("asprintf");
784 base_path = NULL;
785 goto done;
788 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
789 if (err)
790 goto done;
791 if (blob_orig) {
792 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
793 blob_orig);
794 if (err)
795 goto done;
796 } else {
797 /*
798 * If the file has no blob, this is an "add vs add" conflict,
799 * and we simply use an empty ancestor file to make both files
800 * appear in the merged result in their entirety.
801 */
804 /*
805 * In order the run a 3-way merge with a symlink we copy the symlink's
806 * target path into a temporary file and use that file with diff3.
807 */
808 if (S_ISLNK(st_mode)) {
809 char target_path[PATH_MAX];
810 ssize_t target_len;
811 size_t n;
813 free(base_path);
814 if (asprintf(&base_path, "%s/got-symlink-merge",
815 parent) == -1) {
816 err = got_error_from_errno("asprintf");
817 base_path = NULL;
818 goto done;
820 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
821 if (err)
822 goto done;
823 target_len = readlink(ondisk_path, target_path,
824 sizeof(target_path));
825 if (target_len == -1) {
826 err = got_error_from_errno2("readlink", ondisk_path);
827 goto done;
829 n = fwrite(target_path, 1, target_len, symlinkf);
830 if (n != target_len) {
831 err = got_ferror(symlinkf, GOT_ERR_IO);
832 goto done;
834 if (fflush(symlinkf) == EOF) {
835 err = got_error_from_errno2("fflush", symlink_path);
836 goto done;
840 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
841 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
842 label_deriv, label_orig, NULL);
843 if (err)
844 goto done;
846 err = (*progress_cb)(progress_arg,
847 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
848 if (err)
849 goto done;
851 if (fsync(merged_fd) != 0) {
852 err = got_error_from_errno("fsync");
853 goto done;
856 /* Check if a clean merge has subsumed all local changes. */
857 if (overlapcnt == 0) {
858 err = check_files_equal(local_changes_subsumed, deriv_path,
859 merged_path);
860 if (err)
861 goto done;
864 if (fchmod(merged_fd, st_mode) != 0) {
865 err = got_error_from_errno2("fchmod", merged_path);
866 goto done;
869 if (rename(merged_path, ondisk_path) != 0) {
870 err = got_error_from_errno3("rename", merged_path,
871 ondisk_path);
872 goto done;
874 done:
875 if (err) {
876 if (merged_path)
877 unlink(merged_path);
879 if (symlink_path) {
880 if (unlink(symlink_path) == -1 && err == NULL)
881 err = got_error_from_errno2("unlink", symlink_path);
883 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
884 err = got_error_from_errno2("fclose", symlink_path);
885 free(symlink_path);
886 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
887 err = got_error_from_errno("close");
888 if (f_orig && fclose(f_orig) != 0 && err == NULL)
889 err = got_error_from_errno("fclose");
890 free(merged_path);
891 free(base_path);
892 if (blob_orig_path) {
893 unlink(blob_orig_path);
894 free(blob_orig_path);
896 return err;
899 static const struct got_error *
900 update_symlink(const char *ondisk_path, const char *target_path,
901 size_t target_len)
903 /* This is not atomic but matches what 'ln -sf' does. */
904 if (unlink(ondisk_path) == -1)
905 return got_error_from_errno2("unlink", ondisk_path);
906 if (symlink(target_path, ondisk_path) == -1)
907 return got_error_from_errno3("symlink", target_path,
908 ondisk_path);
909 return NULL;
912 /*
913 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
914 * in the work tree with a file that contains conflict markers and the
915 * conflicting target paths of the original version, a "derived version"
916 * of a symlink from an incoming change, and a local version of the symlink.
918 * The original versions's target path can be NULL if it is not available,
919 * such as if both derived versions added a new symlink at the same path.
921 * The incoming derived symlink target is NULL in case the incoming change
922 * has deleted this symlink.
923 */
924 static const struct got_error *
925 install_symlink_conflict(const char *deriv_target,
926 struct got_object_id *deriv_base_commit_id, const char *orig_target,
927 const char *label_orig, const char *local_target, const char *ondisk_path)
929 const struct got_error *err;
930 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
931 FILE *f = NULL;
933 err = got_object_id_str(&id_str, deriv_base_commit_id);
934 if (err)
935 return got_error_from_errno("asprintf");
937 if (asprintf(&label_deriv, "%s: commit %s",
938 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
939 err = got_error_from_errno("asprintf");
940 goto done;
943 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
944 if (err)
945 goto done;
947 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
948 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
949 deriv_target ? deriv_target : "(symlink was deleted)",
950 orig_target ? label_orig : "",
951 orig_target ? "\n" : "",
952 orig_target ? orig_target : "",
953 orig_target ? "\n" : "",
954 GOT_DIFF_CONFLICT_MARKER_SEP,
955 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
956 err = got_error_from_errno2("fprintf", path);
957 goto done;
960 if (unlink(ondisk_path) == -1) {
961 err = got_error_from_errno2("unlink", ondisk_path);
962 goto done;
964 if (rename(path, ondisk_path) == -1) {
965 err = got_error_from_errno3("rename", path, ondisk_path);
966 goto done;
968 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
969 err = got_error_from_errno2("chmod", ondisk_path);
970 goto done;
972 done:
973 if (f != NULL && fclose(f) == EOF && err == NULL)
974 err = got_error_from_errno2("fclose", path);
975 free(path);
976 free(id_str);
977 free(label_deriv);
978 return err;
981 /* forward declaration */
982 static const struct got_error *
983 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
984 const char *, const char *, uint16_t, const char *,
985 struct got_blob_object *, struct got_object_id *,
986 struct got_repository *, got_worktree_checkout_cb, void *);
988 /*
989 * Merge a symlink into the work tree, where blob_orig acts as the common
990 * ancestor, deriv_target is the link target of the first derived version,
991 * and the symlink on disk acts as the second derived version.
992 * Assume that contents of both blobs represent symlinks.
993 */
994 static const struct got_error *
995 merge_symlink(struct got_worktree *worktree,
996 struct got_blob_object *blob_orig, const char *ondisk_path,
997 const char *path, const char *label_orig, const char *deriv_target,
998 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
999 got_worktree_checkout_cb progress_cb, void *progress_arg)
1001 const struct got_error *err = NULL;
1002 char *ancestor_target = NULL;
1003 struct stat sb;
1004 ssize_t ondisk_len, deriv_len;
1005 char ondisk_target[PATH_MAX];
1006 int have_local_change = 0;
1007 int have_incoming_change = 0;
1009 if (lstat(ondisk_path, &sb) == -1)
1010 return got_error_from_errno2("lstat", ondisk_path);
1012 ondisk_len = readlink(ondisk_path, ondisk_target,
1013 sizeof(ondisk_target));
1014 if (ondisk_len == -1) {
1015 err = got_error_from_errno2("readlink",
1016 ondisk_path);
1017 goto done;
1019 ondisk_target[ondisk_len] = '\0';
1021 if (blob_orig) {
1022 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1023 if (err)
1024 goto done;
1027 if (ancestor_target == NULL ||
1028 (ondisk_len != strlen(ancestor_target) ||
1029 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1030 have_local_change = 1;
1032 deriv_len = strlen(deriv_target);
1033 if (ancestor_target == NULL ||
1034 (deriv_len != strlen(ancestor_target) ||
1035 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1036 have_incoming_change = 1;
1038 if (!have_local_change && !have_incoming_change) {
1039 if (ancestor_target) {
1040 /* Both sides made the same change. */
1041 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1042 path);
1043 } else if (deriv_len == ondisk_len &&
1044 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1045 /* Both sides added the same symlink. */
1046 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1047 path);
1048 } else {
1049 /* Both sides added symlinks which don't match. */
1050 err = install_symlink_conflict(deriv_target,
1051 deriv_base_commit_id, ancestor_target,
1052 label_orig, ondisk_target, ondisk_path);
1053 if (err)
1054 goto done;
1055 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1056 path);
1058 } else if (!have_local_change && have_incoming_change) {
1059 /* Apply the incoming change. */
1060 err = update_symlink(ondisk_path, deriv_target,
1061 strlen(deriv_target));
1062 if (err)
1063 goto done;
1064 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1065 } else if (have_local_change && have_incoming_change) {
1066 if (deriv_len == ondisk_len &&
1067 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1068 /* Both sides made the same change. */
1069 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1070 path);
1071 } else {
1072 err = install_symlink_conflict(deriv_target,
1073 deriv_base_commit_id, ancestor_target, label_orig,
1074 ondisk_target, ondisk_path);
1075 if (err)
1076 goto done;
1077 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1078 path);
1082 done:
1083 free(ancestor_target);
1084 return err;
1088 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1089 * blob_deriv acts as the first derived version, and the file on disk
1090 * acts as the second derived version.
1092 static const struct got_error *
1093 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1094 struct got_blob_object *blob_orig, const char *ondisk_path,
1095 const char *path, uint16_t st_mode, const char *label_orig,
1096 struct got_blob_object *blob_deriv,
1097 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1098 got_worktree_checkout_cb progress_cb, void *progress_arg)
1100 const struct got_error *err = NULL;
1101 FILE *f_deriv = NULL;
1102 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1103 char *label_deriv = NULL, *parent;
1105 *local_changes_subsumed = 0;
1107 parent = dirname(ondisk_path);
1108 if (parent == NULL)
1109 return got_error_from_errno2("dirname", ondisk_path);
1111 free(base_path);
1112 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1113 err = got_error_from_errno("asprintf");
1114 base_path = NULL;
1115 goto done;
1118 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1119 if (err)
1120 goto done;
1121 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1122 blob_deriv);
1123 if (err)
1124 goto done;
1126 err = got_object_id_str(&id_str, deriv_base_commit_id);
1127 if (err)
1128 goto done;
1129 if (asprintf(&label_deriv, "%s: commit %s",
1130 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1131 err = got_error_from_errno("asprintf");
1132 goto done;
1135 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1136 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1137 label_deriv, repo, progress_cb, progress_arg);
1138 done:
1139 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1140 err = got_error_from_errno("fclose");
1141 free(base_path);
1142 if (blob_deriv_path) {
1143 unlink(blob_deriv_path);
1144 free(blob_deriv_path);
1146 free(id_str);
1147 free(label_deriv);
1148 return err;
1151 static const struct got_error *
1152 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1153 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1154 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1156 const struct got_error *err = NULL;
1157 struct got_fileindex_entry *new_ie;
1159 *new_iep = NULL;
1161 err = got_fileindex_entry_alloc(&new_ie, path);
1162 if (err)
1163 return err;
1165 err = got_fileindex_entry_update(new_ie, ondisk_path,
1166 blob_id->sha1, base_commit_id->sha1, 1);
1167 if (err)
1168 goto done;
1170 err = got_fileindex_entry_add(fileindex, new_ie);
1171 done:
1172 if (err)
1173 got_fileindex_entry_free(new_ie);
1174 else
1175 *new_iep = new_ie;
1176 return err;
1179 static mode_t
1180 get_ondisk_perms(int executable, mode_t st_mode)
1182 mode_t xbits = S_IXUSR;
1184 if (executable) {
1185 /* Map read bits to execute bits. */
1186 if (st_mode & S_IRGRP)
1187 xbits |= S_IXGRP;
1188 if (st_mode & S_IROTH)
1189 xbits |= S_IXOTH;
1190 return st_mode | xbits;
1193 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1196 /* forward declaration */
1197 static const struct got_error *
1198 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1199 const char *path, mode_t te_mode, mode_t st_mode,
1200 struct got_blob_object *blob, int restoring_missing_file,
1201 int reverting_versioned_file, int installing_bad_symlink,
1202 int path_is_unversioned, struct got_repository *repo,
1203 got_worktree_checkout_cb progress_cb, void *progress_arg);
1206 * This function assumes that the provided symlink target points at a
1207 * safe location in the work tree!
1209 static const struct got_error *
1210 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1211 size_t target_len)
1213 const struct got_error *err = NULL;
1214 ssize_t elen;
1215 char etarget[PATH_MAX];
1216 int fd;
1219 * "Bad" symlinks (those pointing outside the work tree or into the
1220 * .got directory) are installed in the work tree as a regular file
1221 * which contains the bad symlink target path.
1222 * The new symlink target has already been checked for safety by our
1223 * caller. If we can successfully open a regular file then we simply
1224 * replace this file with a symlink below.
1226 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1227 if (fd == -1) {
1228 if (errno != ELOOP)
1229 return got_error_from_errno2("open", ondisk_path);
1231 /* We are updating an existing on-disk symlink. */
1232 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1233 if (elen == -1)
1234 return got_error_from_errno2("readlink", ondisk_path);
1236 if (elen == target_len &&
1237 memcmp(etarget, target_path, target_len) == 0)
1238 return NULL; /* nothing to do */
1241 err = update_symlink(ondisk_path, target_path, target_len);
1242 if (fd != -1 && close(fd) == -1 && err == NULL)
1243 err = got_error_from_errno2("close", ondisk_path);
1244 return err;
1247 static const struct got_error *
1248 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1249 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1251 const struct got_error *err = NULL;
1252 char canonpath[PATH_MAX];
1253 char *path_got = NULL;
1255 *is_bad_symlink = 0;
1257 if (target_len >= sizeof(canonpath)) {
1258 *is_bad_symlink = 1;
1259 return NULL;
1263 * We do not use realpath(3) to resolve the symlink's target
1264 * path because we don't want to resolve symlinks recursively.
1265 * Instead we make the path absolute and then canonicalize it.
1266 * Relative symlink target lookup should begin at the directory
1267 * in which the blob object is being installed.
1269 if (!got_path_is_absolute(target_path)) {
1270 char *abspath;
1271 char *parent = dirname(ondisk_path);
1272 if (parent == NULL)
1273 return got_error_from_errno2("dirname", ondisk_path);
1274 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1275 return got_error_from_errno("asprintf");
1276 if (strlen(abspath) >= sizeof(canonpath)) {
1277 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1278 free(abspath);
1279 return err;
1281 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1282 free(abspath);
1283 if (err)
1284 return err;
1285 } else {
1286 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1287 if (err)
1288 return err;
1291 /* Only allow symlinks pointing at paths within the work tree. */
1292 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1293 *is_bad_symlink = 1;
1294 return NULL;
1297 /* Do not allow symlinks pointing into the .got directory. */
1298 if (asprintf(&path_got, "%s/%s", wtroot_path,
1299 GOT_WORKTREE_GOT_DIR) == -1)
1300 return got_error_from_errno("asprintf");
1301 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1302 *is_bad_symlink = 1;
1304 free(path_got);
1305 return NULL;
1308 static const struct got_error *
1309 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1310 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1311 int restoring_missing_file, int reverting_versioned_file,
1312 int path_is_unversioned, struct got_repository *repo,
1313 got_worktree_checkout_cb progress_cb, void *progress_arg)
1315 const struct got_error *err = NULL;
1316 char target_path[PATH_MAX];
1317 size_t len, target_len = 0;
1318 char *path_got = NULL;
1319 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1320 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1322 *is_bad_symlink = 0;
1325 * Blob object content specifies the target path of the link.
1326 * If a symbolic link cannot be installed we instead create
1327 * a regular file which contains the link target path stored
1328 * in the blob object.
1330 do {
1331 err = got_object_blob_read_block(&len, blob);
1332 if (len + target_len >= sizeof(target_path)) {
1333 /* Path too long; install as a regular file. */
1334 *is_bad_symlink = 1;
1335 got_object_blob_rewind(blob);
1336 return install_blob(worktree, ondisk_path, path,
1337 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1338 restoring_missing_file, reverting_versioned_file,
1339 1, path_is_unversioned, repo, progress_cb,
1340 progress_arg);
1342 if (len > 0) {
1343 /* Skip blob object header first time around. */
1344 memcpy(target_path + target_len, buf + hdrlen,
1345 len - hdrlen);
1346 target_len += len - hdrlen;
1347 hdrlen = 0;
1349 } while (len != 0);
1350 target_path[target_len] = '\0';
1352 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1353 ondisk_path, worktree->root_path);
1354 if (err)
1355 return err;
1357 if (*is_bad_symlink) {
1358 /* install as a regular file */
1359 *is_bad_symlink = 1;
1360 got_object_blob_rewind(blob);
1361 err = install_blob(worktree, ondisk_path, path,
1362 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1363 restoring_missing_file, reverting_versioned_file, 1,
1364 path_is_unversioned, repo, progress_cb, progress_arg);
1365 goto done;
1368 if (symlink(target_path, ondisk_path) == -1) {
1369 if (errno == EEXIST) {
1370 if (path_is_unversioned) {
1371 err = (*progress_cb)(progress_arg,
1372 GOT_STATUS_UNVERSIONED, path);
1373 goto done;
1375 err = replace_existing_symlink(ondisk_path,
1376 target_path, target_len);
1377 if (err)
1378 goto done;
1379 if (progress_cb) {
1380 err = (*progress_cb)(progress_arg,
1381 reverting_versioned_file ?
1382 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1383 path);
1385 goto done; /* Nothing else to do. */
1388 if (errno == ENOENT) {
1389 char *parent = dirname(ondisk_path);
1390 if (parent == NULL) {
1391 err = got_error_from_errno2("dirname",
1392 ondisk_path);
1393 goto done;
1395 err = add_dir_on_disk(worktree, parent);
1396 if (err)
1397 goto done;
1399 * Retry, and fall through to error handling
1400 * below if this second attempt fails.
1402 if (symlink(target_path, ondisk_path) != -1) {
1403 err = NULL; /* success */
1404 goto done;
1408 /* Handle errors from first or second creation attempt. */
1409 if (errno == ENAMETOOLONG) {
1410 /* bad target path; install as a regular file */
1411 *is_bad_symlink = 1;
1412 got_object_blob_rewind(blob);
1413 err = install_blob(worktree, ondisk_path, path,
1414 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1415 restoring_missing_file, reverting_versioned_file, 1,
1416 path_is_unversioned, repo,
1417 progress_cb, progress_arg);
1418 } else if (errno == ENOTDIR) {
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1421 } else {
1422 err = got_error_from_errno3("symlink",
1423 target_path, ondisk_path);
1425 } else if (progress_cb)
1426 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1427 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1428 done:
1429 free(path_got);
1430 return err;
1433 static const struct got_error *
1434 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1435 const char *path, mode_t te_mode, mode_t st_mode,
1436 struct got_blob_object *blob, int restoring_missing_file,
1437 int reverting_versioned_file, int installing_bad_symlink,
1438 int path_is_unversioned, struct got_repository *repo,
1439 got_worktree_checkout_cb progress_cb, void *progress_arg)
1441 const struct got_error *err = NULL;
1442 int fd = -1;
1443 size_t len, hdrlen;
1444 int update = 0;
1445 char *tmppath = NULL;
1447 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1448 GOT_DEFAULT_FILE_MODE);
1449 if (fd == -1) {
1450 if (errno == ENOENT) {
1451 char *parent = dirname(path);
1452 if (parent == NULL)
1453 return got_error_from_errno2("dirname", path);
1454 err = add_dir_on_disk(worktree, parent);
1455 if (err)
1456 return err;
1457 fd = open(ondisk_path,
1458 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1459 GOT_DEFAULT_FILE_MODE);
1460 if (fd == -1)
1461 return got_error_from_errno2("open",
1462 ondisk_path);
1463 } else if (errno == EEXIST) {
1464 if (path_is_unversioned) {
1465 err = (*progress_cb)(progress_arg,
1466 GOT_STATUS_UNVERSIONED, path);
1467 goto done;
1469 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1470 /* TODO file is obstructed; do something */
1471 err = got_error_path(ondisk_path,
1472 GOT_ERR_FILE_OBSTRUCTED);
1473 goto done;
1474 } else {
1475 err = got_opentemp_named_fd(&tmppath, &fd,
1476 ondisk_path);
1477 if (err)
1478 goto done;
1479 update = 1;
1481 } else
1482 return got_error_from_errno2("open", ondisk_path);
1485 if (progress_cb) {
1486 if (restoring_missing_file)
1487 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1488 path);
1489 else if (reverting_versioned_file)
1490 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1491 path);
1492 else
1493 err = (*progress_cb)(progress_arg,
1494 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1495 if (err)
1496 goto done;
1499 hdrlen = got_object_blob_get_hdrlen(blob);
1500 do {
1501 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1502 err = got_object_blob_read_block(&len, blob);
1503 if (err)
1504 break;
1505 if (len > 0) {
1506 /* Skip blob object header first time around. */
1507 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1508 if (outlen == -1) {
1509 err = got_error_from_errno("write");
1510 goto done;
1511 } else if (outlen != len - hdrlen) {
1512 err = got_error(GOT_ERR_IO);
1513 goto done;
1515 hdrlen = 0;
1517 } while (len != 0);
1519 if (fsync(fd) != 0) {
1520 err = got_error_from_errno("fsync");
1521 goto done;
1524 if (update) {
1525 if (rename(tmppath, ondisk_path) != 0) {
1526 err = got_error_from_errno3("rename", tmppath,
1527 ondisk_path);
1528 unlink(tmppath);
1529 goto done;
1533 if (chmod(ondisk_path,
1534 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1535 err = got_error_from_errno2("chmod", ondisk_path);
1536 goto done;
1539 done:
1540 if (fd != -1 && close(fd) != 0 && err == NULL)
1541 err = got_error_from_errno("close");
1542 free(tmppath);
1543 return err;
1546 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1547 static const struct got_error *
1548 get_modified_file_content_status(unsigned char *status, FILE *f)
1550 const struct got_error *err = NULL;
1551 const char *markers[3] = {
1552 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1553 GOT_DIFF_CONFLICT_MARKER_SEP,
1554 GOT_DIFF_CONFLICT_MARKER_END
1556 int i = 0;
1557 char *line;
1558 size_t len;
1559 const char delim[3] = {'\0', '\0', '\0'};
1561 while (*status == GOT_STATUS_MODIFY) {
1562 line = fparseln(f, &len, NULL, delim, 0);
1563 if (line == NULL) {
1564 if (feof(f))
1565 break;
1566 err = got_ferror(f, GOT_ERR_IO);
1567 break;
1570 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1571 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1572 == 0)
1573 *status = GOT_STATUS_CONFLICT;
1574 else
1575 i++;
1579 return err;
1582 static int
1583 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1585 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1586 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1589 static int
1590 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1592 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1593 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1594 ie->mtime_sec == sb->st_mtim.tv_sec &&
1595 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1596 ie->size == (sb->st_size & 0xffffffff) &&
1597 !xbit_differs(ie, sb->st_mode));
1600 static unsigned char
1601 get_staged_status(struct got_fileindex_entry *ie)
1603 switch (got_fileindex_entry_stage_get(ie)) {
1604 case GOT_FILEIDX_STAGE_ADD:
1605 return GOT_STATUS_ADD;
1606 case GOT_FILEIDX_STAGE_DELETE:
1607 return GOT_STATUS_DELETE;
1608 case GOT_FILEIDX_STAGE_MODIFY:
1609 return GOT_STATUS_MODIFY;
1610 default:
1611 return GOT_STATUS_NO_CHANGE;
1615 static const struct got_error *
1616 get_symlink_modification_status(unsigned char *status,
1617 struct got_fileindex_entry *ie, const char *abspath,
1618 int dirfd, const char *de_name, struct got_blob_object *blob)
1620 const struct got_error *err = NULL;
1621 char target_path[PATH_MAX];
1622 char etarget[PATH_MAX];
1623 ssize_t elen;
1624 size_t len, target_len = 0;
1625 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1626 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1628 *status = GOT_STATUS_NO_CHANGE;
1630 /* Blob object content specifies the target path of the link. */
1631 do {
1632 err = got_object_blob_read_block(&len, blob);
1633 if (err)
1634 return err;
1635 if (len + target_len >= sizeof(target_path)) {
1637 * Should not happen. The blob contents were OK
1638 * when this symlink was installed.
1640 return got_error(GOT_ERR_NO_SPACE);
1642 if (len > 0) {
1643 /* Skip blob object header first time around. */
1644 memcpy(target_path + target_len, buf + hdrlen,
1645 len - hdrlen);
1646 target_len += len - hdrlen;
1647 hdrlen = 0;
1649 } while (len != 0);
1650 target_path[target_len] = '\0';
1652 if (dirfd != -1) {
1653 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1654 if (elen == -1)
1655 return got_error_from_errno2("readlinkat", abspath);
1656 } else {
1657 elen = readlink(abspath, etarget, sizeof(etarget));
1658 if (elen == -1)
1659 return got_error_from_errno2("readlink", abspath);
1662 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1663 *status = GOT_STATUS_MODIFY;
1665 return NULL;
1668 static const struct got_error *
1669 get_file_status(unsigned char *status, struct stat *sb,
1670 struct got_fileindex_entry *ie, const char *abspath,
1671 int dirfd, const char *de_name, struct got_repository *repo)
1673 const struct got_error *err = NULL;
1674 struct got_object_id id;
1675 size_t hdrlen;
1676 int fd = -1;
1677 FILE *f = NULL;
1678 uint8_t fbuf[8192];
1679 struct got_blob_object *blob = NULL;
1680 size_t flen, blen;
1681 unsigned char staged_status = get_staged_status(ie);
1683 *status = GOT_STATUS_NO_CHANGE;
1686 * Whenever the caller provides a directory descriptor and a
1687 * directory entry name for the file, use them! This prevents
1688 * race conditions if filesystem paths change beneath our feet.
1690 if (dirfd != -1) {
1691 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1692 if (errno == ENOENT) {
1693 if (got_fileindex_entry_has_file_on_disk(ie))
1694 *status = GOT_STATUS_MISSING;
1695 else
1696 *status = GOT_STATUS_DELETE;
1697 goto done;
1699 err = got_error_from_errno2("fstatat", abspath);
1700 goto done;
1702 } else {
1703 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1704 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1705 return got_error_from_errno2("open", abspath);
1706 else if (fd == -1 && errno == ELOOP) {
1707 if (lstat(abspath, sb) == -1)
1708 return got_error_from_errno2("lstat", abspath);
1709 } else if (fd == -1 || fstat(fd, sb) == -1) {
1710 if (errno == ENOENT) {
1711 if (got_fileindex_entry_has_file_on_disk(ie))
1712 *status = GOT_STATUS_MISSING;
1713 else
1714 *status = GOT_STATUS_DELETE;
1715 goto done;
1717 err = got_error_from_errno2("fstat", abspath);
1718 goto done;
1722 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1723 *status = GOT_STATUS_OBSTRUCTED;
1724 goto done;
1727 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1728 *status = GOT_STATUS_DELETE;
1729 goto done;
1730 } else if (!got_fileindex_entry_has_blob(ie) &&
1731 staged_status != GOT_STATUS_ADD) {
1732 *status = GOT_STATUS_ADD;
1733 goto done;
1736 if (!stat_info_differs(ie, sb))
1737 goto done;
1739 if (S_ISLNK(sb->st_mode) &&
1740 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1741 *status = GOT_STATUS_MODIFY;
1742 goto done;
1745 if (staged_status == GOT_STATUS_MODIFY ||
1746 staged_status == GOT_STATUS_ADD)
1747 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1748 else
1749 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1751 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1752 if (err)
1753 goto done;
1755 if (S_ISLNK(sb->st_mode)) {
1756 err = get_symlink_modification_status(status, ie,
1757 abspath, dirfd, de_name, blob);
1758 goto done;
1761 if (dirfd != -1) {
1762 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1763 if (fd == -1) {
1764 err = got_error_from_errno2("openat", abspath);
1765 goto done;
1769 f = fdopen(fd, "r");
1770 if (f == NULL) {
1771 err = got_error_from_errno2("fdopen", abspath);
1772 goto done;
1774 fd = -1;
1775 hdrlen = got_object_blob_get_hdrlen(blob);
1776 for (;;) {
1777 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1778 err = got_object_blob_read_block(&blen, blob);
1779 if (err)
1780 goto done;
1781 /* Skip length of blob object header first time around. */
1782 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1783 if (flen == 0 && ferror(f)) {
1784 err = got_error_from_errno("fread");
1785 goto done;
1787 if (blen == 0) {
1788 if (flen != 0)
1789 *status = GOT_STATUS_MODIFY;
1790 break;
1791 } else if (flen == 0) {
1792 if (blen != 0)
1793 *status = GOT_STATUS_MODIFY;
1794 break;
1795 } else if (blen - hdrlen == flen) {
1796 /* Skip blob object header first time around. */
1797 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1798 *status = GOT_STATUS_MODIFY;
1799 break;
1801 } else {
1802 *status = GOT_STATUS_MODIFY;
1803 break;
1805 hdrlen = 0;
1808 if (*status == GOT_STATUS_MODIFY) {
1809 rewind(f);
1810 err = get_modified_file_content_status(status, f);
1811 } else if (xbit_differs(ie, sb->st_mode))
1812 *status = GOT_STATUS_MODE_CHANGE;
1813 done:
1814 if (blob)
1815 got_object_blob_close(blob);
1816 if (f != NULL && fclose(f) == EOF && err == NULL)
1817 err = got_error_from_errno2("fclose", abspath);
1818 if (fd != -1 && close(fd) == -1 && err == NULL)
1819 err = got_error_from_errno2("close", abspath);
1820 return err;
1824 * Update timestamps in the file index if a file is unmodified and
1825 * we had to run a full content comparison to find out.
1827 static const struct got_error *
1828 sync_timestamps(char *ondisk_path, unsigned char status,
1829 struct got_fileindex_entry *ie, struct stat *sb)
1831 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1832 return got_fileindex_entry_update(ie, ondisk_path,
1833 ie->blob_sha1, ie->commit_sha1, 1);
1835 return NULL;
1838 static const struct got_error *
1839 update_blob(struct got_worktree *worktree,
1840 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1841 struct got_tree_entry *te, const char *path,
1842 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1843 void *progress_arg)
1845 const struct got_error *err = NULL;
1846 struct got_blob_object *blob = NULL;
1847 char *ondisk_path;
1848 unsigned char status = GOT_STATUS_NO_CHANGE;
1849 struct stat sb;
1851 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1852 return got_error_from_errno("asprintf");
1854 if (ie) {
1855 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1856 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1857 goto done;
1859 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1860 repo);
1861 if (err)
1862 goto done;
1863 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1864 sb.st_mode = got_fileindex_perms_to_st(ie);
1865 } else {
1866 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1867 status = GOT_STATUS_UNVERSIONED;
1870 if (status == GOT_STATUS_OBSTRUCTED) {
1871 err = (*progress_cb)(progress_arg, status, path);
1872 goto done;
1874 if (status == GOT_STATUS_CONFLICT) {
1875 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1876 path);
1877 goto done;
1880 if (ie && status != GOT_STATUS_MISSING &&
1881 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1882 if (got_fileindex_entry_has_commit(ie) &&
1883 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1884 SHA1_DIGEST_LENGTH) == 0) {
1885 err = sync_timestamps(ondisk_path, status, ie, &sb);
1886 if (err)
1887 goto done;
1888 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1889 path);
1890 goto done;
1892 if (got_fileindex_entry_has_blob(ie) &&
1893 memcmp(ie->blob_sha1, te->id.sha1,
1894 SHA1_DIGEST_LENGTH) == 0) {
1895 err = sync_timestamps(ondisk_path, status, ie, &sb);
1896 goto done;
1900 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1901 if (err)
1902 goto done;
1904 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1905 int update_timestamps;
1906 struct got_blob_object *blob2 = NULL;
1907 char *label_orig = NULL;
1908 if (got_fileindex_entry_has_blob(ie)) {
1909 struct got_object_id id2;
1910 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1911 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1912 if (err)
1913 goto done;
1915 if (got_fileindex_entry_has_commit(ie)) {
1916 char id_str[SHA1_DIGEST_STRING_LENGTH];
1917 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1918 sizeof(id_str)) == NULL) {
1919 err = got_error_path(id_str,
1920 GOT_ERR_BAD_OBJ_ID_STR);
1921 goto done;
1923 if (asprintf(&label_orig, "%s: commit %s",
1924 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1925 err = got_error_from_errno("asprintf");
1926 goto done;
1929 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1930 char *link_target;
1931 err = got_object_blob_read_to_str(&link_target, blob);
1932 if (err)
1933 goto done;
1934 err = merge_symlink(worktree, blob2, ondisk_path, path,
1935 label_orig, link_target, worktree->base_commit_id,
1936 repo, progress_cb, progress_arg);
1937 free(link_target);
1938 } else {
1939 err = merge_blob(&update_timestamps, worktree, blob2,
1940 ondisk_path, path, sb.st_mode, label_orig, blob,
1941 worktree->base_commit_id, repo,
1942 progress_cb, progress_arg);
1944 free(label_orig);
1945 if (blob2)
1946 got_object_blob_close(blob2);
1947 if (err)
1948 goto done;
1950 * Do not update timestamps of files with local changes.
1951 * Otherwise, a future status walk would treat them as
1952 * unmodified files again.
1954 err = got_fileindex_entry_update(ie, ondisk_path,
1955 blob->id.sha1, worktree->base_commit_id->sha1,
1956 update_timestamps);
1957 } else if (status == GOT_STATUS_MODE_CHANGE) {
1958 err = got_fileindex_entry_update(ie, ondisk_path,
1959 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1960 } else if (status == GOT_STATUS_DELETE) {
1961 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1962 if (err)
1963 goto done;
1964 err = got_fileindex_entry_update(ie, ondisk_path,
1965 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1966 if (err)
1967 goto done;
1968 } else {
1969 int is_bad_symlink = 0;
1970 if (S_ISLNK(te->mode)) {
1971 err = install_symlink(&is_bad_symlink, worktree,
1972 ondisk_path, path, blob,
1973 status == GOT_STATUS_MISSING, 0,
1974 status == GOT_STATUS_UNVERSIONED, repo,
1975 progress_cb, progress_arg);
1976 } else {
1977 err = install_blob(worktree, ondisk_path, path,
1978 te->mode, sb.st_mode, blob,
1979 status == GOT_STATUS_MISSING, 0, 0,
1980 status == GOT_STATUS_UNVERSIONED, repo,
1981 progress_cb, progress_arg);
1983 if (err)
1984 goto done;
1986 if (ie) {
1987 err = got_fileindex_entry_update(ie, ondisk_path,
1988 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1989 } else {
1990 err = create_fileindex_entry(&ie, fileindex,
1991 worktree->base_commit_id, ondisk_path, path,
1992 &blob->id);
1994 if (err)
1995 goto done;
1997 if (is_bad_symlink) {
1998 got_fileindex_entry_filetype_set(ie,
1999 GOT_FILEIDX_MODE_BAD_SYMLINK);
2002 got_object_blob_close(blob);
2003 done:
2004 free(ondisk_path);
2005 return err;
2008 static const struct got_error *
2009 remove_ondisk_file(const char *root_path, const char *path)
2011 const struct got_error *err = NULL;
2012 char *ondisk_path = NULL;
2014 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2015 return got_error_from_errno("asprintf");
2017 if (unlink(ondisk_path) == -1) {
2018 if (errno != ENOENT)
2019 err = got_error_from_errno2("unlink", ondisk_path);
2020 } else {
2021 char *parent = dirname(ondisk_path);
2022 while (parent && strcmp(parent, root_path) != 0) {
2023 if (rmdir(parent) == -1) {
2024 if (errno != ENOTEMPTY)
2025 err = got_error_from_errno2("rmdir",
2026 parent);
2027 break;
2029 parent = dirname(parent);
2032 free(ondisk_path);
2033 return err;
2036 static const struct got_error *
2037 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2038 struct got_fileindex_entry *ie, struct got_repository *repo,
2039 got_worktree_checkout_cb progress_cb, void *progress_arg)
2041 const struct got_error *err = NULL;
2042 unsigned char status;
2043 struct stat sb;
2044 char *ondisk_path;
2046 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2047 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2049 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2050 == -1)
2051 return got_error_from_errno("asprintf");
2053 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2054 if (err)
2055 goto done;
2057 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2058 char ondisk_target[PATH_MAX];
2059 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2060 sizeof(ondisk_target));
2061 if (ondisk_len == -1) {
2062 err = got_error_from_errno2("readlink", ondisk_path);
2063 goto done;
2065 ondisk_target[ondisk_len] = '\0';
2066 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2067 NULL, NULL, /* XXX pass common ancestor info? */
2068 ondisk_target, ondisk_path);
2069 if (err)
2070 goto done;
2071 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2072 ie->path);
2073 goto done;
2076 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2077 status == GOT_STATUS_ADD) {
2078 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2079 if (err)
2080 goto done;
2082 * Preserve the working file and change the deleted blob's
2083 * entry into a schedule-add entry.
2085 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2086 0);
2087 } else {
2088 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2089 if (err)
2090 goto done;
2091 if (status == GOT_STATUS_NO_CHANGE) {
2092 err = remove_ondisk_file(worktree->root_path, ie->path);
2093 if (err)
2094 goto done;
2096 got_fileindex_entry_remove(fileindex, ie);
2098 done:
2099 free(ondisk_path);
2100 return err;
2103 struct diff_cb_arg {
2104 struct got_fileindex *fileindex;
2105 struct got_worktree *worktree;
2106 struct got_repository *repo;
2107 got_worktree_checkout_cb progress_cb;
2108 void *progress_arg;
2109 got_cancel_cb cancel_cb;
2110 void *cancel_arg;
2113 static const struct got_error *
2114 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2115 struct got_tree_entry *te, const char *parent_path)
2117 struct diff_cb_arg *a = arg;
2119 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2120 return got_error(GOT_ERR_CANCELLED);
2122 return update_blob(a->worktree, a->fileindex, ie, te,
2123 ie->path, a->repo, a->progress_cb, a->progress_arg);
2126 static const struct got_error *
2127 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2129 struct diff_cb_arg *a = arg;
2131 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2132 return got_error(GOT_ERR_CANCELLED);
2134 return delete_blob(a->worktree, a->fileindex, ie,
2135 a->repo, a->progress_cb, a->progress_arg);
2138 static const struct got_error *
2139 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2141 struct diff_cb_arg *a = arg;
2142 const struct got_error *err;
2143 char *path;
2145 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2146 return got_error(GOT_ERR_CANCELLED);
2148 if (got_object_tree_entry_is_submodule(te))
2149 return NULL;
2151 if (asprintf(&path, "%s%s%s", parent_path,
2152 parent_path[0] ? "/" : "", te->name)
2153 == -1)
2154 return got_error_from_errno("asprintf");
2156 if (S_ISDIR(te->mode))
2157 err = add_dir_on_disk(a->worktree, path);
2158 else
2159 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2160 a->repo, a->progress_cb, a->progress_arg);
2162 free(path);
2163 return err;
2166 const struct got_error *
2167 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2169 uint32_t uuid_status;
2171 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2172 if (uuid_status != uuid_s_ok) {
2173 *uuidstr = NULL;
2174 return got_error_uuid(uuid_status, "uuid_to_string");
2177 return NULL;
2180 static const struct got_error *
2181 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2183 const struct got_error *err = NULL;
2184 char *uuidstr = NULL;
2186 *refname = NULL;
2188 err = got_worktree_get_uuid(&uuidstr, worktree);
2189 if (err)
2190 return err;
2192 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2193 err = got_error_from_errno("asprintf");
2194 *refname = NULL;
2196 free(uuidstr);
2197 return err;
2200 const struct got_error *
2201 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2203 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2206 static const struct got_error *
2207 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2209 return get_ref_name(refname, worktree,
2210 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2213 static const struct got_error *
2214 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2216 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2219 static const struct got_error *
2220 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2222 return get_ref_name(refname, worktree,
2223 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2226 static const struct got_error *
2227 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2229 return get_ref_name(refname, worktree,
2230 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2233 static const struct got_error *
2234 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2236 return get_ref_name(refname, worktree,
2237 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2240 static const struct got_error *
2241 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2243 return get_ref_name(refname, worktree,
2244 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2247 static const struct got_error *
2248 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2250 return get_ref_name(refname, worktree,
2251 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2254 static const struct got_error *
2255 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2257 return get_ref_name(refname, worktree,
2258 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2261 const struct got_error *
2262 got_worktree_get_histedit_script_path(char **path,
2263 struct got_worktree *worktree)
2265 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2266 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2267 *path = NULL;
2268 return got_error_from_errno("asprintf");
2270 return NULL;
2274 * Prevent Git's garbage collector from deleting our base commit by
2275 * setting a reference to our base commit's ID.
2277 static const struct got_error *
2278 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2280 const struct got_error *err = NULL;
2281 struct got_reference *ref = NULL;
2282 char *refname;
2284 err = got_worktree_get_base_ref_name(&refname, worktree);
2285 if (err)
2286 return err;
2288 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2289 if (err)
2290 goto done;
2292 err = got_ref_write(ref, repo);
2293 done:
2294 free(refname);
2295 if (ref)
2296 got_ref_close(ref);
2297 return err;
2300 static const struct got_error *
2301 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2303 const struct got_error *err = NULL;
2305 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2306 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2307 err = got_error_from_errno("asprintf");
2308 *fileindex_path = NULL;
2310 return err;
2314 static const struct got_error *
2315 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2316 struct got_worktree *worktree)
2318 const struct got_error *err = NULL;
2319 FILE *index = NULL;
2321 *fileindex_path = NULL;
2322 *fileindex = got_fileindex_alloc();
2323 if (*fileindex == NULL)
2324 return got_error_from_errno("got_fileindex_alloc");
2326 err = get_fileindex_path(fileindex_path, worktree);
2327 if (err)
2328 goto done;
2330 index = fopen(*fileindex_path, "rb");
2331 if (index == NULL) {
2332 if (errno != ENOENT)
2333 err = got_error_from_errno2("fopen", *fileindex_path);
2334 } else {
2335 err = got_fileindex_read(*fileindex, index);
2336 if (fclose(index) != 0 && err == NULL)
2337 err = got_error_from_errno("fclose");
2339 done:
2340 if (err) {
2341 free(*fileindex_path);
2342 *fileindex_path = NULL;
2343 got_fileindex_free(*fileindex);
2344 *fileindex = NULL;
2346 return err;
2349 struct bump_base_commit_id_arg {
2350 struct got_object_id *base_commit_id;
2351 const char *path;
2352 size_t path_len;
2353 const char *entry_name;
2354 got_worktree_checkout_cb progress_cb;
2355 void *progress_arg;
2358 /* Bump base commit ID of all files within an updated part of the work tree. */
2359 static const struct got_error *
2360 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2362 const struct got_error *err;
2363 struct bump_base_commit_id_arg *a = arg;
2365 if (a->entry_name) {
2366 if (strcmp(ie->path, a->path) != 0)
2367 return NULL;
2368 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2369 return NULL;
2371 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2372 SHA1_DIGEST_LENGTH) == 0)
2373 return NULL;
2375 if (a->progress_cb) {
2376 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2377 ie->path);
2378 if (err)
2379 return err;
2381 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2382 return NULL;
2385 static const struct got_error *
2386 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2388 const struct got_error *err = NULL;
2389 char *new_fileindex_path = NULL;
2390 FILE *new_index = NULL;
2391 struct timespec timeout;
2393 err = got_opentemp_named(&new_fileindex_path, &new_index,
2394 fileindex_path);
2395 if (err)
2396 goto done;
2398 err = got_fileindex_write(fileindex, new_index);
2399 if (err)
2400 goto done;
2402 if (rename(new_fileindex_path, fileindex_path) != 0) {
2403 err = got_error_from_errno3("rename", new_fileindex_path,
2404 fileindex_path);
2405 unlink(new_fileindex_path);
2409 * Sleep for a short amount of time to ensure that files modified after
2410 * this program exits have a different time stamp from the one which
2411 * was recorded in the file index.
2413 timeout.tv_sec = 0;
2414 timeout.tv_nsec = 1;
2415 nanosleep(&timeout, NULL);
2416 done:
2417 if (new_index)
2418 fclose(new_index);
2419 free(new_fileindex_path);
2420 return err;
2423 static const struct got_error *
2424 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2425 struct got_object_id **tree_id, const char *wt_relpath,
2426 struct got_worktree *worktree, struct got_repository *repo)
2428 const struct got_error *err = NULL;
2429 struct got_object_id *id = NULL;
2430 char *in_repo_path = NULL;
2431 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2433 *entry_type = GOT_OBJ_TYPE_ANY;
2434 *tree_relpath = NULL;
2435 *tree_id = NULL;
2437 if (wt_relpath[0] == '\0') {
2438 /* Check out all files within the work tree. */
2439 *entry_type = GOT_OBJ_TYPE_TREE;
2440 *tree_relpath = strdup("");
2441 if (*tree_relpath == NULL) {
2442 err = got_error_from_errno("strdup");
2443 goto done;
2445 err = got_object_id_by_path(tree_id, repo,
2446 worktree->base_commit_id, worktree->path_prefix);
2447 if (err)
2448 goto done;
2449 return NULL;
2452 /* Check out a subset of files in the work tree. */
2454 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2455 is_root_wt ? "" : "/", wt_relpath) == -1) {
2456 err = got_error_from_errno("asprintf");
2457 goto done;
2460 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2461 in_repo_path);
2462 if (err)
2463 goto done;
2465 free(in_repo_path);
2466 in_repo_path = NULL;
2468 err = got_object_get_type(entry_type, repo, id);
2469 if (err)
2470 goto done;
2472 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2473 /* Check out a single file. */
2474 if (strchr(wt_relpath, '/') == NULL) {
2475 /* Check out a single file in work tree's root dir. */
2476 in_repo_path = strdup(worktree->path_prefix);
2477 if (in_repo_path == NULL) {
2478 err = got_error_from_errno("strdup");
2479 goto done;
2481 *tree_relpath = strdup("");
2482 if (*tree_relpath == NULL) {
2483 err = got_error_from_errno("strdup");
2484 goto done;
2486 } else {
2487 /* Check out a single file in a subdirectory. */
2488 err = got_path_dirname(tree_relpath, wt_relpath);
2489 if (err)
2490 return err;
2491 if (asprintf(&in_repo_path, "%s%s%s",
2492 worktree->path_prefix, is_root_wt ? "" : "/",
2493 *tree_relpath) == -1) {
2494 err = got_error_from_errno("asprintf");
2495 goto done;
2498 err = got_object_id_by_path(tree_id, repo,
2499 worktree->base_commit_id, in_repo_path);
2500 } else {
2501 /* Check out all files within a subdirectory. */
2502 *tree_id = got_object_id_dup(id);
2503 if (*tree_id == NULL) {
2504 err = got_error_from_errno("got_object_id_dup");
2505 goto done;
2507 *tree_relpath = strdup(wt_relpath);
2508 if (*tree_relpath == NULL) {
2509 err = got_error_from_errno("strdup");
2510 goto done;
2513 done:
2514 free(id);
2515 free(in_repo_path);
2516 if (err) {
2517 *entry_type = GOT_OBJ_TYPE_ANY;
2518 free(*tree_relpath);
2519 *tree_relpath = NULL;
2520 free(*tree_id);
2521 *tree_id = NULL;
2523 return err;
2526 static const struct got_error *
2527 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2528 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2529 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2530 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2532 const struct got_error *err = NULL;
2533 struct got_commit_object *commit = NULL;
2534 struct got_tree_object *tree = NULL;
2535 struct got_fileindex_diff_tree_cb diff_cb;
2536 struct diff_cb_arg arg;
2538 err = ref_base_commit(worktree, repo);
2539 if (err) {
2540 if (!(err->code == GOT_ERR_ERRNO &&
2541 (errno == EACCES || errno == EROFS)))
2542 goto done;
2543 err = (*progress_cb)(progress_arg,
2544 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2545 if (err)
2546 return err;
2549 err = got_object_open_as_commit(&commit, repo,
2550 worktree->base_commit_id);
2551 if (err)
2552 goto done;
2554 err = got_object_open_as_tree(&tree, repo, tree_id);
2555 if (err)
2556 goto done;
2558 if (entry_name &&
2559 got_object_tree_find_entry(tree, entry_name) == NULL) {
2560 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2561 goto done;
2564 diff_cb.diff_old_new = diff_old_new;
2565 diff_cb.diff_old = diff_old;
2566 diff_cb.diff_new = diff_new;
2567 arg.fileindex = fileindex;
2568 arg.worktree = worktree;
2569 arg.repo = repo;
2570 arg.progress_cb = progress_cb;
2571 arg.progress_arg = progress_arg;
2572 arg.cancel_cb = cancel_cb;
2573 arg.cancel_arg = cancel_arg;
2574 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2575 entry_name, repo, &diff_cb, &arg);
2576 done:
2577 if (tree)
2578 got_object_tree_close(tree);
2579 if (commit)
2580 got_object_commit_close(commit);
2581 return err;
2584 const struct got_error *
2585 got_worktree_checkout_files(struct got_worktree *worktree,
2586 struct got_pathlist_head *paths, struct got_repository *repo,
2587 got_worktree_checkout_cb progress_cb, void *progress_arg,
2588 got_cancel_cb cancel_cb, void *cancel_arg)
2590 const struct got_error *err = NULL, *sync_err, *unlockerr;
2591 struct got_commit_object *commit = NULL;
2592 struct got_tree_object *tree = NULL;
2593 struct got_fileindex *fileindex = NULL;
2594 char *fileindex_path = NULL;
2595 struct got_pathlist_entry *pe;
2596 struct tree_path_data {
2597 SIMPLEQ_ENTRY(tree_path_data) entry;
2598 struct got_object_id *tree_id;
2599 int entry_type;
2600 char *relpath;
2601 char *entry_name;
2602 } *tpd = NULL;
2603 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2605 SIMPLEQ_INIT(&tree_paths);
2607 err = lock_worktree(worktree, LOCK_EX);
2608 if (err)
2609 return err;
2611 /* Map all specified paths to in-repository trees. */
2612 TAILQ_FOREACH(pe, paths, entry) {
2613 tpd = malloc(sizeof(*tpd));
2614 if (tpd == NULL) {
2615 err = got_error_from_errno("malloc");
2616 goto done;
2619 err = find_tree_entry_for_checkout(&tpd->entry_type,
2620 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2621 if (err) {
2622 free(tpd);
2623 goto done;
2626 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2627 err = got_path_basename(&tpd->entry_name, pe->path);
2628 if (err) {
2629 free(tpd->relpath);
2630 free(tpd->tree_id);
2631 free(tpd);
2632 goto done;
2634 } else
2635 tpd->entry_name = NULL;
2637 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2641 * Read the file index.
2642 * Checking out files is supposed to be an idempotent operation.
2643 * If the on-disk file index is incomplete we will try to complete it.
2645 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2646 if (err)
2647 goto done;
2649 tpd = SIMPLEQ_FIRST(&tree_paths);
2650 TAILQ_FOREACH(pe, paths, entry) {
2651 struct bump_base_commit_id_arg bbc_arg;
2653 err = checkout_files(worktree, fileindex, tpd->relpath,
2654 tpd->tree_id, tpd->entry_name, repo,
2655 progress_cb, progress_arg, cancel_cb, cancel_arg);
2656 if (err)
2657 break;
2659 bbc_arg.base_commit_id = worktree->base_commit_id;
2660 bbc_arg.entry_name = tpd->entry_name;
2661 bbc_arg.path = pe->path;
2662 bbc_arg.path_len = pe->path_len;
2663 bbc_arg.progress_cb = progress_cb;
2664 bbc_arg.progress_arg = progress_arg;
2665 err = got_fileindex_for_each_entry_safe(fileindex,
2666 bump_base_commit_id, &bbc_arg);
2667 if (err)
2668 break;
2670 tpd = SIMPLEQ_NEXT(tpd, entry);
2672 sync_err = sync_fileindex(fileindex, fileindex_path);
2673 if (sync_err && err == NULL)
2674 err = sync_err;
2675 done:
2676 free(fileindex_path);
2677 if (tree)
2678 got_object_tree_close(tree);
2679 if (commit)
2680 got_object_commit_close(commit);
2681 if (fileindex)
2682 got_fileindex_free(fileindex);
2683 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2684 tpd = SIMPLEQ_FIRST(&tree_paths);
2685 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2686 free(tpd->relpath);
2687 free(tpd->tree_id);
2688 free(tpd);
2690 unlockerr = lock_worktree(worktree, LOCK_SH);
2691 if (unlockerr && err == NULL)
2692 err = unlockerr;
2693 return err;
2696 struct merge_file_cb_arg {
2697 struct got_worktree *worktree;
2698 struct got_fileindex *fileindex;
2699 got_worktree_checkout_cb progress_cb;
2700 void *progress_arg;
2701 got_cancel_cb cancel_cb;
2702 void *cancel_arg;
2703 const char *label_orig;
2704 struct got_object_id *commit_id2;
2707 static const struct got_error *
2708 merge_file_cb(void *arg, struct got_blob_object *blob1,
2709 struct got_blob_object *blob2, struct got_object_id *id1,
2710 struct got_object_id *id2, const char *path1, const char *path2,
2711 mode_t mode1, mode_t mode2, struct got_repository *repo)
2713 static const struct got_error *err = NULL;
2714 struct merge_file_cb_arg *a = arg;
2715 struct got_fileindex_entry *ie;
2716 char *ondisk_path = NULL;
2717 struct stat sb;
2718 unsigned char status;
2719 int local_changes_subsumed;
2721 if (blob1 && blob2) {
2722 ie = got_fileindex_entry_get(a->fileindex, path2,
2723 strlen(path2));
2724 if (ie == NULL)
2725 return (*a->progress_cb)(a->progress_arg,
2726 GOT_STATUS_MISSING, path2);
2728 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2729 path2) == -1)
2730 return got_error_from_errno("asprintf");
2732 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2733 repo);
2734 if (err)
2735 goto done;
2737 if (status == GOT_STATUS_DELETE) {
2738 err = (*a->progress_cb)(a->progress_arg,
2739 GOT_STATUS_MERGE, path2);
2740 goto done;
2742 if (status != GOT_STATUS_NO_CHANGE &&
2743 status != GOT_STATUS_MODIFY &&
2744 status != GOT_STATUS_CONFLICT &&
2745 status != GOT_STATUS_ADD) {
2746 err = (*a->progress_cb)(a->progress_arg, status, path2);
2747 goto done;
2750 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2751 char *link_target2;
2752 err = got_object_blob_read_to_str(&link_target2, blob2);
2753 if (err)
2754 goto done;
2755 err = merge_symlink(a->worktree, blob1, ondisk_path,
2756 path2, a->label_orig, link_target2, a->commit_id2,
2757 repo, a->progress_cb, a->progress_arg);
2758 free(link_target2);
2759 } else {
2760 err = merge_blob(&local_changes_subsumed, a->worktree,
2761 blob1, ondisk_path, path2, sb.st_mode,
2762 a->label_orig, blob2, a->commit_id2, repo,
2763 a->progress_cb, a->progress_arg);
2765 } else if (blob1) {
2766 ie = got_fileindex_entry_get(a->fileindex, path1,
2767 strlen(path1));
2768 if (ie == NULL)
2769 return (*a->progress_cb)(a->progress_arg,
2770 GOT_STATUS_MISSING, path1);
2772 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2773 path1) == -1)
2774 return got_error_from_errno("asprintf");
2776 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2777 repo);
2778 if (err)
2779 goto done;
2781 switch (status) {
2782 case GOT_STATUS_NO_CHANGE:
2783 err = (*a->progress_cb)(a->progress_arg,
2784 GOT_STATUS_DELETE, path1);
2785 if (err)
2786 goto done;
2787 err = remove_ondisk_file(a->worktree->root_path, path1);
2788 if (err)
2789 goto done;
2790 if (ie)
2791 got_fileindex_entry_mark_deleted_from_disk(ie);
2792 break;
2793 case GOT_STATUS_DELETE:
2794 case GOT_STATUS_MISSING:
2795 err = (*a->progress_cb)(a->progress_arg,
2796 GOT_STATUS_DELETE, path1);
2797 if (err)
2798 goto done;
2799 if (ie)
2800 got_fileindex_entry_mark_deleted_from_disk(ie);
2801 break;
2802 case GOT_STATUS_ADD:
2803 case GOT_STATUS_MODIFY:
2804 case GOT_STATUS_CONFLICT:
2805 err = (*a->progress_cb)(a->progress_arg,
2806 GOT_STATUS_CANNOT_DELETE, path1);
2807 if (err)
2808 goto done;
2809 break;
2810 case GOT_STATUS_OBSTRUCTED:
2811 err = (*a->progress_cb)(a->progress_arg, status, path1);
2812 if (err)
2813 goto done;
2814 break;
2815 default:
2816 break;
2818 } else if (blob2) {
2819 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2820 path2) == -1)
2821 return got_error_from_errno("asprintf");
2822 ie = got_fileindex_entry_get(a->fileindex, path2,
2823 strlen(path2));
2824 if (ie) {
2825 err = get_file_status(&status, &sb, ie, ondisk_path,
2826 -1, NULL, repo);
2827 if (err)
2828 goto done;
2829 if (status != GOT_STATUS_NO_CHANGE &&
2830 status != GOT_STATUS_MODIFY &&
2831 status != GOT_STATUS_CONFLICT &&
2832 status != GOT_STATUS_ADD) {
2833 err = (*a->progress_cb)(a->progress_arg,
2834 status, path2);
2835 goto done;
2837 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2838 char *link_target2;
2839 err = got_object_blob_read_to_str(&link_target2,
2840 blob2);
2841 if (err)
2842 goto done;
2843 err = merge_symlink(a->worktree, NULL,
2844 ondisk_path, path2, a->label_orig,
2845 link_target2, a->commit_id2, repo,
2846 a->progress_cb, a->progress_arg);
2847 free(link_target2);
2848 } else if (S_ISREG(sb.st_mode)) {
2849 err = merge_blob(&local_changes_subsumed,
2850 a->worktree, NULL, ondisk_path, path2,
2851 sb.st_mode, a->label_orig, blob2,
2852 a->commit_id2, repo, a->progress_cb,
2853 a->progress_arg);
2854 } else {
2855 err = got_error_path(ondisk_path,
2856 GOT_ERR_FILE_OBSTRUCTED);
2858 if (err)
2859 goto done;
2860 if (status == GOT_STATUS_DELETE) {
2861 err = got_fileindex_entry_update(ie,
2862 ondisk_path, blob2->id.sha1,
2863 a->worktree->base_commit_id->sha1, 0);
2864 if (err)
2865 goto done;
2867 } else {
2868 int is_bad_symlink = 0;
2869 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2870 if (S_ISLNK(mode2)) {
2871 err = install_symlink(&is_bad_symlink,
2872 a->worktree, ondisk_path, path2, blob2, 0,
2873 0, 1, repo, a->progress_cb, a->progress_arg);
2874 } else {
2875 err = install_blob(a->worktree, ondisk_path, path2,
2876 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2877 a->progress_cb, a->progress_arg);
2879 if (err)
2880 goto done;
2881 err = got_fileindex_entry_alloc(&ie, path2);
2882 if (err)
2883 goto done;
2884 err = got_fileindex_entry_update(ie, ondisk_path,
2885 NULL, NULL, 1);
2886 if (err) {
2887 got_fileindex_entry_free(ie);
2888 goto done;
2890 err = got_fileindex_entry_add(a->fileindex, ie);
2891 if (err) {
2892 got_fileindex_entry_free(ie);
2893 goto done;
2895 if (is_bad_symlink) {
2896 got_fileindex_entry_filetype_set(ie,
2897 GOT_FILEIDX_MODE_BAD_SYMLINK);
2901 done:
2902 free(ondisk_path);
2903 return err;
2906 struct check_merge_ok_arg {
2907 struct got_worktree *worktree;
2908 struct got_repository *repo;
2911 static const struct got_error *
2912 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2914 const struct got_error *err = NULL;
2915 struct check_merge_ok_arg *a = arg;
2916 unsigned char status;
2917 struct stat sb;
2918 char *ondisk_path;
2920 /* Reject merges into a work tree with mixed base commits. */
2921 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2922 SHA1_DIGEST_LENGTH))
2923 return got_error(GOT_ERR_MIXED_COMMITS);
2925 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2926 == -1)
2927 return got_error_from_errno("asprintf");
2929 /* Reject merges into a work tree with conflicted files. */
2930 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2931 if (err)
2932 return err;
2933 if (status == GOT_STATUS_CONFLICT)
2934 return got_error(GOT_ERR_CONFLICTS);
2936 return NULL;
2939 static const struct got_error *
2940 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2941 const char *fileindex_path, struct got_object_id *commit_id1,
2942 struct got_object_id *commit_id2, struct got_repository *repo,
2943 got_worktree_checkout_cb progress_cb, void *progress_arg,
2944 got_cancel_cb cancel_cb, void *cancel_arg)
2946 const struct got_error *err = NULL, *sync_err;
2947 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2948 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2949 struct merge_file_cb_arg arg;
2950 char *label_orig = NULL;
2952 if (commit_id1) {
2953 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2954 worktree->path_prefix);
2955 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2956 goto done;
2958 if (tree_id1) {
2959 char *id_str;
2961 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2962 if (err)
2963 goto done;
2965 err = got_object_id_str(&id_str, commit_id1);
2966 if (err)
2967 goto done;
2969 if (asprintf(&label_orig, "%s: commit %s",
2970 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2971 err = got_error_from_errno("asprintf");
2972 free(id_str);
2973 goto done;
2975 free(id_str);
2978 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2979 worktree->path_prefix);
2980 if (err)
2981 goto done;
2983 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2984 if (err)
2985 goto done;
2987 arg.worktree = worktree;
2988 arg.fileindex = fileindex;
2989 arg.progress_cb = progress_cb;
2990 arg.progress_arg = progress_arg;
2991 arg.cancel_cb = cancel_cb;
2992 arg.cancel_arg = cancel_arg;
2993 arg.label_orig = label_orig;
2994 arg.commit_id2 = commit_id2;
2995 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2996 sync_err = sync_fileindex(fileindex, fileindex_path);
2997 if (sync_err && err == NULL)
2998 err = sync_err;
2999 done:
3000 if (tree1)
3001 got_object_tree_close(tree1);
3002 if (tree2)
3003 got_object_tree_close(tree2);
3004 free(label_orig);
3005 return err;
3008 const struct got_error *
3009 got_worktree_merge_files(struct got_worktree *worktree,
3010 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3011 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3012 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3014 const struct got_error *err, *unlockerr;
3015 char *fileindex_path = NULL;
3016 struct got_fileindex *fileindex = NULL;
3017 struct check_merge_ok_arg mok_arg;
3019 err = lock_worktree(worktree, LOCK_EX);
3020 if (err)
3021 return err;
3023 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3024 if (err)
3025 goto done;
3027 mok_arg.worktree = worktree;
3028 mok_arg.repo = repo;
3029 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3030 &mok_arg);
3031 if (err)
3032 goto done;
3034 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3035 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3036 done:
3037 if (fileindex)
3038 got_fileindex_free(fileindex);
3039 free(fileindex_path);
3040 unlockerr = lock_worktree(worktree, LOCK_SH);
3041 if (unlockerr && err == NULL)
3042 err = unlockerr;
3043 return err;
3046 struct diff_dir_cb_arg {
3047 struct got_fileindex *fileindex;
3048 struct got_worktree *worktree;
3049 const char *status_path;
3050 size_t status_path_len;
3051 struct got_repository *repo;
3052 got_worktree_status_cb status_cb;
3053 void *status_arg;
3054 got_cancel_cb cancel_cb;
3055 void *cancel_arg;
3056 /* A pathlist containing per-directory pathlists of ignore patterns. */
3057 struct got_pathlist_head ignores;
3058 int report_unchanged;
3059 int no_ignores;
3062 static const struct got_error *
3063 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3064 int dirfd, const char *de_name,
3065 got_worktree_status_cb status_cb, void *status_arg,
3066 struct got_repository *repo, int report_unchanged)
3068 const struct got_error *err = NULL;
3069 unsigned char status = GOT_STATUS_NO_CHANGE;
3070 unsigned char staged_status = get_staged_status(ie);
3071 struct stat sb;
3072 struct got_object_id blob_id, commit_id, staged_blob_id;
3073 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3074 struct got_object_id *staged_blob_idp = NULL;
3076 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3077 if (err)
3078 return err;
3080 if (status == GOT_STATUS_NO_CHANGE &&
3081 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3082 return NULL;
3084 if (got_fileindex_entry_has_blob(ie)) {
3085 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3086 blob_idp = &blob_id;
3088 if (got_fileindex_entry_has_commit(ie)) {
3089 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3090 commit_idp = &commit_id;
3092 if (staged_status == GOT_STATUS_ADD ||
3093 staged_status == GOT_STATUS_MODIFY) {
3094 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3095 SHA1_DIGEST_LENGTH);
3096 staged_blob_idp = &staged_blob_id;
3099 return (*status_cb)(status_arg, status, staged_status,
3100 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3103 static const struct got_error *
3104 status_old_new(void *arg, struct got_fileindex_entry *ie,
3105 struct dirent *de, const char *parent_path, int dirfd)
3107 const struct got_error *err = NULL;
3108 struct diff_dir_cb_arg *a = arg;
3109 char *abspath;
3111 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3112 return got_error(GOT_ERR_CANCELLED);
3114 if (got_path_cmp(parent_path, a->status_path,
3115 strlen(parent_path), a->status_path_len) != 0 &&
3116 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3117 return NULL;
3119 if (parent_path[0]) {
3120 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3121 parent_path, de->d_name) == -1)
3122 return got_error_from_errno("asprintf");
3123 } else {
3124 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3125 de->d_name) == -1)
3126 return got_error_from_errno("asprintf");
3129 err = report_file_status(ie, abspath, dirfd, de->d_name,
3130 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3131 free(abspath);
3132 return err;
3135 static const struct got_error *
3136 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3138 struct diff_dir_cb_arg *a = arg;
3139 struct got_object_id blob_id, commit_id;
3140 unsigned char status;
3142 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3143 return got_error(GOT_ERR_CANCELLED);
3145 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3146 return NULL;
3148 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3149 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3150 if (got_fileindex_entry_has_file_on_disk(ie))
3151 status = GOT_STATUS_MISSING;
3152 else
3153 status = GOT_STATUS_DELETE;
3154 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3155 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3158 void
3159 free_ignorelist(struct got_pathlist_head *ignorelist)
3161 struct got_pathlist_entry *pe;
3163 TAILQ_FOREACH(pe, ignorelist, entry)
3164 free((char *)pe->path);
3165 got_pathlist_free(ignorelist);
3168 void
3169 free_ignores(struct got_pathlist_head *ignores)
3171 struct got_pathlist_entry *pe;
3173 TAILQ_FOREACH(pe, ignores, entry) {
3174 struct got_pathlist_head *ignorelist = pe->data;
3175 free_ignorelist(ignorelist);
3176 free((char *)pe->path);
3178 got_pathlist_free(ignores);
3181 static const struct got_error *
3182 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3184 const struct got_error *err = NULL;
3185 struct got_pathlist_entry *pe = NULL;
3186 struct got_pathlist_head *ignorelist;
3187 char *line = NULL, *pattern, *dirpath = NULL;
3188 size_t linesize = 0;
3189 ssize_t linelen;
3191 ignorelist = calloc(1, sizeof(*ignorelist));
3192 if (ignorelist == NULL)
3193 return got_error_from_errno("calloc");
3194 TAILQ_INIT(ignorelist);
3196 while ((linelen = getline(&line, &linesize, f)) != -1) {
3197 if (linelen > 0 && line[linelen - 1] == '\n')
3198 line[linelen - 1] = '\0';
3200 /* Git's ignores may contain comments. */
3201 if (line[0] == '#')
3202 continue;
3204 /* Git's negated patterns are not (yet?) supported. */
3205 if (line[0] == '!')
3206 continue;
3208 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3209 line) == -1) {
3210 err = got_error_from_errno("asprintf");
3211 goto done;
3213 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3214 if (err)
3215 goto done;
3217 if (ferror(f)) {
3218 err = got_error_from_errno("getline");
3219 goto done;
3222 dirpath = strdup(path);
3223 if (dirpath == NULL) {
3224 err = got_error_from_errno("strdup");
3225 goto done;
3227 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3228 done:
3229 free(line);
3230 if (err || pe == NULL) {
3231 free(dirpath);
3232 free_ignorelist(ignorelist);
3234 return err;
3237 int
3238 match_ignores(struct got_pathlist_head *ignores, const char *path)
3240 struct got_pathlist_entry *pe;
3242 /* Handle patterns which match in all directories. */
3243 TAILQ_FOREACH(pe, ignores, entry) {
3244 struct got_pathlist_head *ignorelist = pe->data;
3245 struct got_pathlist_entry *pi;
3247 TAILQ_FOREACH(pi, ignorelist, entry) {
3248 const char *p, *pattern = pi->path;
3250 if (strncmp(pattern, "**/", 3) != 0)
3251 continue;
3252 pattern += 3;
3253 p = path;
3254 while (*p) {
3255 if (fnmatch(pattern, p,
3256 FNM_PATHNAME | FNM_LEADING_DIR)) {
3257 /* Retry in next directory. */
3258 while (*p && *p != '/')
3259 p++;
3260 while (*p == '/')
3261 p++;
3262 continue;
3264 return 1;
3270 * The ignores pathlist contains ignore lists from children before
3271 * parents, so we can find the most specific ignorelist by walking
3272 * ignores backwards.
3274 pe = TAILQ_LAST(ignores, got_pathlist_head);
3275 while (pe) {
3276 if (got_path_is_child(path, pe->path, pe->path_len)) {
3277 struct got_pathlist_head *ignorelist = pe->data;
3278 struct got_pathlist_entry *pi;
3279 TAILQ_FOREACH(pi, ignorelist, entry) {
3280 const char *pattern = pi->path;
3281 int flags = FNM_LEADING_DIR;
3282 if (strstr(pattern, "/**/") == NULL)
3283 flags |= FNM_PATHNAME;
3284 if (fnmatch(pattern, path, flags))
3285 continue;
3286 return 1;
3289 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3292 return 0;
3295 static const struct got_error *
3296 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3297 const char *path, int dirfd, const char *ignores_filename)
3299 const struct got_error *err = NULL;
3300 char *ignorespath;
3301 int fd = -1;
3302 FILE *ignoresfile = NULL;
3304 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3305 path[0] ? "/" : "", ignores_filename) == -1)
3306 return got_error_from_errno("asprintf");
3308 if (dirfd != -1) {
3309 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3310 if (fd == -1) {
3311 if (errno != ENOENT && errno != EACCES)
3312 err = got_error_from_errno2("openat",
3313 ignorespath);
3314 } else {
3315 ignoresfile = fdopen(fd, "r");
3316 if (ignoresfile == NULL)
3317 err = got_error_from_errno2("fdopen",
3318 ignorespath);
3319 else {
3320 fd = -1;
3321 err = read_ignores(ignores, path, ignoresfile);
3324 } else {
3325 ignoresfile = fopen(ignorespath, "r");
3326 if (ignoresfile == NULL) {
3327 if (errno != ENOENT && errno != EACCES)
3328 err = got_error_from_errno2("fopen",
3329 ignorespath);
3330 } else
3331 err = read_ignores(ignores, path, ignoresfile);
3334 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3335 err = got_error_from_errno2("fclose", path);
3336 if (fd != -1 && close(fd) == -1 && err == NULL)
3337 err = got_error_from_errno2("close", path);
3338 free(ignorespath);
3339 return err;
3342 static const struct got_error *
3343 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3345 const struct got_error *err = NULL;
3346 struct diff_dir_cb_arg *a = arg;
3347 char *path = NULL;
3349 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3350 return got_error(GOT_ERR_CANCELLED);
3352 if (parent_path[0]) {
3353 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3354 return got_error_from_errno("asprintf");
3355 } else {
3356 path = de->d_name;
3359 if (de->d_type != DT_DIR &&
3360 got_path_is_child(path, a->status_path, a->status_path_len)
3361 && !match_ignores(&a->ignores, path))
3362 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3363 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3364 if (parent_path[0])
3365 free(path);
3366 return err;
3369 static const struct got_error *
3370 status_traverse(void *arg, const char *path, int dirfd)
3372 const struct got_error *err = NULL;
3373 struct diff_dir_cb_arg *a = arg;
3375 if (a->no_ignores)
3376 return NULL;
3378 err = add_ignores(&a->ignores, a->worktree->root_path,
3379 path, dirfd, ".cvsignore");
3380 if (err)
3381 return err;
3383 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3384 dirfd, ".gitignore");
3386 return err;
3389 static const struct got_error *
3390 report_single_file_status(const char *path, const char *ondisk_path,
3391 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3392 void *status_arg, struct got_repository *repo, int report_unchanged)
3394 struct got_fileindex_entry *ie;
3395 struct stat sb;
3397 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3398 if (ie)
3399 return report_file_status(ie, ondisk_path, -1, NULL,
3400 status_cb, status_arg, repo, report_unchanged);
3402 if (lstat(ondisk_path, &sb) == -1) {
3403 if (errno != ENOENT)
3404 return got_error_from_errno2("lstat", ondisk_path);
3405 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3406 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3407 return NULL;
3410 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3411 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3412 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3414 return NULL;
3417 static const struct got_error *
3418 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3419 const char *root_path, const char *path)
3421 const struct got_error *err;
3422 char *parent_path, *next_parent_path = NULL;
3424 err = add_ignores(ignores, root_path, "", -1,
3425 ".cvsignore");
3426 if (err)
3427 return err;
3429 err = add_ignores(ignores, root_path, "", -1,
3430 ".gitignore");
3431 if (err)
3432 return err;
3434 err = got_path_dirname(&parent_path, path);
3435 if (err) {
3436 if (err->code == GOT_ERR_BAD_PATH)
3437 return NULL; /* cannot traverse parent */
3438 return err;
3440 for (;;) {
3441 err = add_ignores(ignores, root_path, parent_path, -1,
3442 ".cvsignore");
3443 if (err)
3444 break;
3445 err = add_ignores(ignores, root_path, parent_path, -1,
3446 ".gitignore");
3447 if (err)
3448 break;
3449 err = got_path_dirname(&next_parent_path, parent_path);
3450 if (err) {
3451 if (err->code == GOT_ERR_BAD_PATH)
3452 err = NULL; /* traversed everything */
3453 break;
3455 free(parent_path);
3456 parent_path = next_parent_path;
3457 next_parent_path = NULL;
3460 free(parent_path);
3461 free(next_parent_path);
3462 return err;
3465 static const struct got_error *
3466 worktree_status(struct got_worktree *worktree, const char *path,
3467 struct got_fileindex *fileindex, struct got_repository *repo,
3468 got_worktree_status_cb status_cb, void *status_arg,
3469 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3470 int report_unchanged)
3472 const struct got_error *err = NULL;
3473 int fd = -1;
3474 struct got_fileindex_diff_dir_cb fdiff_cb;
3475 struct diff_dir_cb_arg arg;
3476 char *ondisk_path = NULL;
3478 TAILQ_INIT(&arg.ignores);
3480 if (asprintf(&ondisk_path, "%s%s%s",
3481 worktree->root_path, path[0] ? "/" : "", path) == -1)
3482 return got_error_from_errno("asprintf");
3484 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3485 if (fd == -1) {
3486 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3487 errno != ELOOP)
3488 err = got_error_from_errno2("open", ondisk_path);
3489 else
3490 err = report_single_file_status(path, ondisk_path,
3491 fileindex, status_cb, status_arg, repo,
3492 report_unchanged);
3493 } else {
3494 fdiff_cb.diff_old_new = status_old_new;
3495 fdiff_cb.diff_old = status_old;
3496 fdiff_cb.diff_new = status_new;
3497 fdiff_cb.diff_traverse = status_traverse;
3498 arg.fileindex = fileindex;
3499 arg.worktree = worktree;
3500 arg.status_path = path;
3501 arg.status_path_len = strlen(path);
3502 arg.repo = repo;
3503 arg.status_cb = status_cb;
3504 arg.status_arg = status_arg;
3505 arg.cancel_cb = cancel_cb;
3506 arg.cancel_arg = cancel_arg;
3507 arg.report_unchanged = report_unchanged;
3508 arg.no_ignores = no_ignores;
3509 if (!no_ignores) {
3510 err = add_ignores_from_parent_paths(&arg.ignores,
3511 worktree->root_path, path);
3512 if (err)
3513 goto done;
3515 err = got_fileindex_diff_dir(fileindex, fd,
3516 worktree->root_path, path, repo, &fdiff_cb, &arg);
3518 done:
3519 free_ignores(&arg.ignores);
3520 if (fd != -1 && close(fd) != 0 && err == NULL)
3521 err = got_error_from_errno("close");
3522 free(ondisk_path);
3523 return err;
3526 const struct got_error *
3527 got_worktree_status(struct got_worktree *worktree,
3528 struct got_pathlist_head *paths, struct got_repository *repo,
3529 got_worktree_status_cb status_cb, void *status_arg,
3530 got_cancel_cb cancel_cb, void *cancel_arg)
3532 const struct got_error *err = NULL;
3533 char *fileindex_path = NULL;
3534 struct got_fileindex *fileindex = NULL;
3535 struct got_pathlist_entry *pe;
3537 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3538 if (err)
3539 return err;
3541 TAILQ_FOREACH(pe, paths, entry) {
3542 err = worktree_status(worktree, pe->path, fileindex, repo,
3543 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3544 if (err)
3545 break;
3547 free(fileindex_path);
3548 got_fileindex_free(fileindex);
3549 return err;
3552 const struct got_error *
3553 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3554 const char *arg)
3556 const struct got_error *err = NULL;
3557 char *resolved = NULL, *cwd = NULL, *path = NULL;
3558 size_t len;
3559 struct stat sb;
3561 *wt_path = NULL;
3563 cwd = getcwd(NULL, 0);
3564 if (cwd == NULL)
3565 return got_error_from_errno("getcwd");
3567 if (lstat(arg, &sb) == -1) {
3568 if (errno != ENOENT) {
3569 err = got_error_from_errno2("lstat", arg);
3570 goto done;
3573 if (S_ISLNK(sb.st_mode)) {
3575 * We cannot use realpath(3) with symlinks since we want to
3576 * operate on the symlink itself.
3577 * But we can make the path absolute, assuming it is relative
3578 * to the current working directory, and then canonicalize it.
3580 char *abspath = NULL;
3581 char canonpath[PATH_MAX];
3582 if (!got_path_is_absolute(arg)) {
3583 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3584 err = got_error_from_errno("asprintf");
3585 goto done;
3589 err = got_canonpath(abspath ? abspath : arg, canonpath,
3590 sizeof(canonpath));
3591 if (err)
3592 goto done;
3593 resolved = strdup(canonpath);
3594 if (resolved == NULL) {
3595 err = got_error_from_errno("strdup");
3596 goto done;
3598 } else {
3599 resolved = realpath(arg, NULL);
3600 if (resolved == NULL) {
3601 if (errno != ENOENT) {
3602 err = got_error_from_errno2("realpath", arg);
3603 goto done;
3605 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3606 err = got_error_from_errno("asprintf");
3607 goto done;
3612 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3613 strlen(got_worktree_get_root_path(worktree)))) {
3614 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3615 goto done;
3618 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3619 err = got_path_skip_common_ancestor(&path,
3620 got_worktree_get_root_path(worktree), resolved);
3621 if (err)
3622 goto done;
3623 } else {
3624 path = strdup("");
3625 if (path == NULL) {
3626 err = got_error_from_errno("strdup");
3627 goto done;
3631 /* XXX status walk can't deal with trailing slash! */
3632 len = strlen(path);
3633 while (len > 0 && path[len - 1] == '/') {
3634 path[len - 1] = '\0';
3635 len--;
3637 done:
3638 free(resolved);
3639 free(cwd);
3640 if (err == NULL)
3641 *wt_path = path;
3642 else
3643 free(path);
3644 return err;
3647 struct schedule_addition_args {
3648 struct got_worktree *worktree;
3649 struct got_fileindex *fileindex;
3650 got_worktree_checkout_cb progress_cb;
3651 void *progress_arg;
3652 struct got_repository *repo;
3655 static const struct got_error *
3656 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3657 const char *relpath, struct got_object_id *blob_id,
3658 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3659 int dirfd, const char *de_name)
3661 struct schedule_addition_args *a = arg;
3662 const struct got_error *err = NULL;
3663 struct got_fileindex_entry *ie;
3664 struct stat sb;
3665 char *ondisk_path;
3667 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3668 relpath) == -1)
3669 return got_error_from_errno("asprintf");
3671 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3672 if (ie) {
3673 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3674 de_name, a->repo);
3675 if (err)
3676 goto done;
3677 /* Re-adding an existing entry is a no-op. */
3678 if (status == GOT_STATUS_ADD)
3679 goto done;
3680 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3681 if (err)
3682 goto done;
3685 if (status != GOT_STATUS_UNVERSIONED) {
3686 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3687 goto done;
3690 err = got_fileindex_entry_alloc(&ie, relpath);
3691 if (err)
3692 goto done;
3693 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3694 if (err) {
3695 got_fileindex_entry_free(ie);
3696 goto done;
3698 err = got_fileindex_entry_add(a->fileindex, ie);
3699 if (err) {
3700 got_fileindex_entry_free(ie);
3701 goto done;
3703 done:
3704 free(ondisk_path);
3705 if (err)
3706 return err;
3707 if (status == GOT_STATUS_ADD)
3708 return NULL;
3709 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3712 const struct got_error *
3713 got_worktree_schedule_add(struct got_worktree *worktree,
3714 struct got_pathlist_head *paths,
3715 got_worktree_checkout_cb progress_cb, void *progress_arg,
3716 struct got_repository *repo, int no_ignores)
3718 struct got_fileindex *fileindex = NULL;
3719 char *fileindex_path = NULL;
3720 const struct got_error *err = NULL, *sync_err, *unlockerr;
3721 struct got_pathlist_entry *pe;
3722 struct schedule_addition_args saa;
3724 err = lock_worktree(worktree, LOCK_EX);
3725 if (err)
3726 return err;
3728 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3729 if (err)
3730 goto done;
3732 saa.worktree = worktree;
3733 saa.fileindex = fileindex;
3734 saa.progress_cb = progress_cb;
3735 saa.progress_arg = progress_arg;
3736 saa.repo = repo;
3738 TAILQ_FOREACH(pe, paths, entry) {
3739 err = worktree_status(worktree, pe->path, fileindex, repo,
3740 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3741 if (err)
3742 break;
3744 sync_err = sync_fileindex(fileindex, fileindex_path);
3745 if (sync_err && err == NULL)
3746 err = sync_err;
3747 done:
3748 free(fileindex_path);
3749 if (fileindex)
3750 got_fileindex_free(fileindex);
3751 unlockerr = lock_worktree(worktree, LOCK_SH);
3752 if (unlockerr && err == NULL)
3753 err = unlockerr;
3754 return err;
3757 struct schedule_deletion_args {
3758 struct got_worktree *worktree;
3759 struct got_fileindex *fileindex;
3760 got_worktree_delete_cb progress_cb;
3761 void *progress_arg;
3762 struct got_repository *repo;
3763 int delete_local_mods;
3764 int keep_on_disk;
3765 const char *status_codes;
3768 static const struct got_error *
3769 schedule_for_deletion(void *arg, unsigned char status,
3770 unsigned char staged_status, const char *relpath,
3771 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3772 struct got_object_id *commit_id, int dirfd, const char *de_name)
3774 struct schedule_deletion_args *a = arg;
3775 const struct got_error *err = NULL;
3776 struct got_fileindex_entry *ie = NULL;
3777 struct stat sb;
3778 char *ondisk_path, *parent = NULL;
3780 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3781 if (ie == NULL)
3782 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3784 staged_status = get_staged_status(ie);
3785 if (staged_status != GOT_STATUS_NO_CHANGE) {
3786 if (staged_status == GOT_STATUS_DELETE)
3787 return NULL;
3788 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3791 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3792 relpath) == -1)
3793 return got_error_from_errno("asprintf");
3795 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3796 a->repo);
3797 if (err)
3798 goto done;
3800 if (a->status_codes) {
3801 size_t ncodes = strlen(a->status_codes);
3802 int i;
3803 for (i = 0; i < ncodes ; i++) {
3804 if (status == a->status_codes[i])
3805 break;
3807 if (i == ncodes) {
3808 /* Do not delete files in non-matching status. */
3809 free(ondisk_path);
3810 return NULL;
3812 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3813 a->status_codes[i] != GOT_STATUS_MISSING) {
3814 static char msg[64];
3815 snprintf(msg, sizeof(msg),
3816 "invalid status code '%c'", a->status_codes[i]);
3817 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3818 goto done;
3822 if (status != GOT_STATUS_NO_CHANGE) {
3823 if (status == GOT_STATUS_DELETE)
3824 goto done;
3825 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3826 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3827 goto done;
3829 if (status != GOT_STATUS_MODIFY &&
3830 status != GOT_STATUS_MISSING) {
3831 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3832 goto done;
3836 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3837 if (dirfd != -1) {
3838 if (unlinkat(dirfd, de_name, 0) != 0) {
3839 err = got_error_from_errno2("unlinkat",
3840 ondisk_path);
3841 goto done;
3843 } else if (unlink(ondisk_path) != 0) {
3844 err = got_error_from_errno2("unlink", ondisk_path);
3845 goto done;
3848 parent = dirname(ondisk_path);
3850 if (parent == NULL) {
3851 err = got_error_from_errno2("dirname", ondisk_path);
3852 goto done;
3854 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3855 if (rmdir(parent) == -1) {
3856 if (errno != ENOTEMPTY)
3857 err = got_error_from_errno2("rmdir",
3858 parent);
3859 break;
3861 parent = dirname(parent);
3862 if (parent == NULL) {
3863 err = got_error_from_errno2("dirname", parent);
3864 goto done;
3869 got_fileindex_entry_mark_deleted_from_disk(ie);
3870 done:
3871 free(ondisk_path);
3872 if (err)
3873 return err;
3874 if (status == GOT_STATUS_DELETE)
3875 return NULL;
3876 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3877 staged_status, relpath);
3880 const struct got_error *
3881 got_worktree_schedule_delete(struct got_worktree *worktree,
3882 struct got_pathlist_head *paths, int delete_local_mods,
3883 const char *status_codes,
3884 got_worktree_delete_cb progress_cb, void *progress_arg,
3885 struct got_repository *repo, int keep_on_disk)
3887 struct got_fileindex *fileindex = NULL;
3888 char *fileindex_path = NULL;
3889 const struct got_error *err = NULL, *sync_err, *unlockerr;
3890 struct got_pathlist_entry *pe;
3891 struct schedule_deletion_args sda;
3893 err = lock_worktree(worktree, LOCK_EX);
3894 if (err)
3895 return err;
3897 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3898 if (err)
3899 goto done;
3901 sda.worktree = worktree;
3902 sda.fileindex = fileindex;
3903 sda.progress_cb = progress_cb;
3904 sda.progress_arg = progress_arg;
3905 sda.repo = repo;
3906 sda.delete_local_mods = delete_local_mods;
3907 sda.keep_on_disk = keep_on_disk;
3908 sda.status_codes = status_codes;
3910 TAILQ_FOREACH(pe, paths, entry) {
3911 err = worktree_status(worktree, pe->path, fileindex, repo,
3912 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3913 if (err)
3914 break;
3916 sync_err = sync_fileindex(fileindex, fileindex_path);
3917 if (sync_err && err == NULL)
3918 err = sync_err;
3919 done:
3920 free(fileindex_path);
3921 if (fileindex)
3922 got_fileindex_free(fileindex);
3923 unlockerr = lock_worktree(worktree, LOCK_SH);
3924 if (unlockerr && err == NULL)
3925 err = unlockerr;
3926 return err;
3929 static const struct got_error *
3930 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3932 const struct got_error *err = NULL;
3933 char *line = NULL;
3934 size_t linesize = 0, n;
3935 ssize_t linelen;
3937 linelen = getline(&line, &linesize, infile);
3938 if (linelen == -1) {
3939 if (ferror(infile)) {
3940 err = got_error_from_errno("getline");
3941 goto done;
3943 return NULL;
3945 if (outfile) {
3946 n = fwrite(line, 1, linelen, outfile);
3947 if (n != linelen) {
3948 err = got_ferror(outfile, GOT_ERR_IO);
3949 goto done;
3952 if (rejectfile) {
3953 n = fwrite(line, 1, linelen, rejectfile);
3954 if (n != linelen)
3955 err = got_ferror(outfile, GOT_ERR_IO);
3957 done:
3958 free(line);
3959 return err;
3962 static const struct got_error *
3963 skip_one_line(FILE *f)
3965 char *line = NULL;
3966 size_t linesize = 0;
3967 ssize_t linelen;
3969 linelen = getline(&line, &linesize, f);
3970 if (linelen == -1) {
3971 if (ferror(f))
3972 return got_error_from_errno("getline");
3973 return NULL;
3975 free(line);
3976 return NULL;
3979 static const struct got_error *
3980 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3981 int start_old, int end_old, int start_new, int end_new,
3982 FILE *outfile, FILE *rejectfile)
3984 const struct got_error *err;
3986 /* Copy old file's lines leading up to patch. */
3987 while (!feof(f1) && *line_cur1 < start_old) {
3988 err = copy_one_line(f1, outfile, NULL);
3989 if (err)
3990 return err;
3991 (*line_cur1)++;
3993 /* Skip new file's lines leading up to patch. */
3994 while (!feof(f2) && *line_cur2 < start_new) {
3995 if (rejectfile)
3996 err = copy_one_line(f2, NULL, rejectfile);
3997 else
3998 err = skip_one_line(f2);
3999 if (err)
4000 return err;
4001 (*line_cur2)++;
4003 /* Copy patched lines. */
4004 while (!feof(f2) && *line_cur2 <= end_new) {
4005 err = copy_one_line(f2, outfile, NULL);
4006 if (err)
4007 return err;
4008 (*line_cur2)++;
4010 /* Skip over old file's replaced lines. */
4011 while (!feof(f1) && *line_cur1 <= end_old) {
4012 if (rejectfile)
4013 err = copy_one_line(f1, NULL, rejectfile);
4014 else
4015 err = skip_one_line(f1);
4016 if (err)
4017 return err;
4018 (*line_cur1)++;
4021 return NULL;
4024 static const struct got_error *
4025 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4026 FILE *outfile, FILE *rejectfile)
4028 const struct got_error *err;
4030 if (outfile) {
4031 /* Copy old file's lines until EOF. */
4032 while (!feof(f1)) {
4033 err = copy_one_line(f1, outfile, NULL);
4034 if (err)
4035 return err;
4036 (*line_cur1)++;
4039 if (rejectfile) {
4040 /* Copy new file's lines until EOF. */
4041 while (!feof(f2)) {
4042 err = copy_one_line(f2, NULL, rejectfile);
4043 if (err)
4044 return err;
4045 (*line_cur2)++;
4049 return NULL;
4052 static const struct got_error *
4053 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4054 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4055 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4056 int *line_cur2, FILE *outfile, FILE *rejectfile,
4057 got_worktree_patch_cb patch_cb, void *patch_arg)
4059 const struct got_error *err = NULL;
4060 int start_old = change->cv.a;
4061 int end_old = change->cv.b;
4062 int start_new = change->cv.c;
4063 int end_new = change->cv.d;
4064 long pos1, pos2;
4065 FILE *hunkfile;
4067 *choice = GOT_PATCH_CHOICE_NONE;
4069 hunkfile = got_opentemp();
4070 if (hunkfile == NULL)
4071 return got_error_from_errno("got_opentemp");
4073 pos1 = ftell(f1);
4074 pos2 = ftell(f2);
4076 /* XXX TODO needs error checking */
4077 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4079 if (fseek(f1, pos1, SEEK_SET) == -1) {
4080 err = got_ferror(f1, GOT_ERR_IO);
4081 goto done;
4083 if (fseek(f2, pos2, SEEK_SET) == -1) {
4084 err = got_ferror(f1, GOT_ERR_IO);
4085 goto done;
4087 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4088 err = got_ferror(hunkfile, GOT_ERR_IO);
4089 goto done;
4092 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4093 hunkfile, n, nchanges);
4094 if (err)
4095 goto done;
4097 switch (*choice) {
4098 case GOT_PATCH_CHOICE_YES:
4099 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4100 end_old, start_new, end_new, outfile, rejectfile);
4101 break;
4102 case GOT_PATCH_CHOICE_NO:
4103 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4104 end_old, start_new, end_new, rejectfile, outfile);
4105 break;
4106 case GOT_PATCH_CHOICE_QUIT:
4107 break;
4108 default:
4109 err = got_error(GOT_ERR_PATCH_CHOICE);
4110 break;
4112 done:
4113 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4114 err = got_error_from_errno("fclose");
4115 return err;
4118 struct revert_file_args {
4119 struct got_worktree *worktree;
4120 struct got_fileindex *fileindex;
4121 got_worktree_checkout_cb progress_cb;
4122 void *progress_arg;
4123 got_worktree_patch_cb patch_cb;
4124 void *patch_arg;
4125 struct got_repository *repo;
4128 static const struct got_error *
4129 create_patched_content(char **path_outfile, int reverse_patch,
4130 struct got_object_id *blob_id, const char *path2,
4131 int dirfd2, const char *de_name2,
4132 const char *relpath, struct got_repository *repo,
4133 got_worktree_patch_cb patch_cb, void *patch_arg)
4135 const struct got_error *err;
4136 struct got_blob_object *blob = NULL;
4137 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4138 int fd2 = -1;
4139 char link_target[PATH_MAX];
4140 ssize_t link_len = 0;
4141 char *path1 = NULL, *id_str = NULL;
4142 struct stat sb1, sb2;
4143 struct got_diff_changes *changes = NULL;
4144 struct got_diff_state *ds = NULL;
4145 struct got_diff_args *args = NULL;
4146 struct got_diff_change *change;
4147 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4148 int n = 0;
4150 *path_outfile = NULL;
4152 err = got_object_id_str(&id_str, blob_id);
4153 if (err)
4154 return err;
4156 if (dirfd2 != -1) {
4157 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4158 if (fd2 == -1) {
4159 if (errno != ELOOP) {
4160 err = got_error_from_errno2("openat", path2);
4161 goto done;
4163 link_len = readlinkat(dirfd2, de_name2,
4164 link_target, sizeof(link_target));
4165 if (link_len == -1)
4166 return got_error_from_errno2("readlinkat", path2);
4167 sb2.st_mode = S_IFLNK;
4168 sb2.st_size = link_len;
4170 } else {
4171 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4172 if (fd2 == -1) {
4173 if (errno != ELOOP) {
4174 err = got_error_from_errno2("open", path2);
4175 goto done;
4177 link_len = readlink(path2, link_target,
4178 sizeof(link_target));
4179 if (link_len == -1)
4180 return got_error_from_errno2("readlink", path2);
4181 sb2.st_mode = S_IFLNK;
4182 sb2.st_size = link_len;
4185 if (fd2 != -1) {
4186 if (fstat(fd2, &sb2) == -1) {
4187 err = got_error_from_errno2("fstat", path2);
4188 goto done;
4191 f2 = fdopen(fd2, "r");
4192 if (f2 == NULL) {
4193 err = got_error_from_errno2("fdopen", path2);
4194 goto done;
4196 fd2 = -1;
4197 } else {
4198 size_t n;
4199 f2 = got_opentemp();
4200 if (f2 == NULL) {
4201 err = got_error_from_errno2("got_opentemp", path2);
4202 goto done;
4204 n = fwrite(link_target, 1, link_len, f2);
4205 if (n != link_len) {
4206 err = got_ferror(f2, GOT_ERR_IO);
4207 goto done;
4209 if (fflush(f2) == EOF) {
4210 err = got_error_from_errno("fflush");
4211 goto done;
4213 rewind(f2);
4216 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4217 if (err)
4218 goto done;
4220 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4221 if (err)
4222 goto done;
4224 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4225 if (err)
4226 goto done;
4228 if (stat(path1, &sb1) == -1) {
4229 err = got_error_from_errno2("stat", path1);
4230 goto done;
4233 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4234 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4235 if (err)
4236 goto done;
4238 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4239 if (err)
4240 goto done;
4242 if (fseek(f1, 0L, SEEK_SET) == -1)
4243 return got_ferror(f1, GOT_ERR_IO);
4244 if (fseek(f2, 0L, SEEK_SET) == -1)
4245 return got_ferror(f2, GOT_ERR_IO);
4246 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4247 int choice;
4248 err = apply_or_reject_change(&choice, change, ++n,
4249 changes->nchanges, ds, args, diff_flags, relpath,
4250 f1, f2, &line_cur1, &line_cur2,
4251 reverse_patch ? NULL : outfile,
4252 reverse_patch ? outfile : NULL,
4253 patch_cb, patch_arg);
4254 if (err)
4255 goto done;
4256 if (choice == GOT_PATCH_CHOICE_YES)
4257 have_content = 1;
4258 else if (choice == GOT_PATCH_CHOICE_QUIT)
4259 break;
4261 if (have_content) {
4262 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4263 reverse_patch ? NULL : outfile,
4264 reverse_patch ? outfile : NULL);
4265 if (err)
4266 goto done;
4268 if (!S_ISLNK(sb2.st_mode)) {
4269 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4270 err = got_error_from_errno2("chmod", path2);
4271 goto done;
4275 done:
4276 free(id_str);
4277 if (blob)
4278 got_object_blob_close(blob);
4279 if (f1 && fclose(f1) == EOF && err == NULL)
4280 err = got_error_from_errno2("fclose", path1);
4281 if (f2 && fclose(f2) == EOF && err == NULL)
4282 err = got_error_from_errno2("fclose", path2);
4283 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4284 err = got_error_from_errno2("close", path2);
4285 if (outfile && fclose(outfile) == EOF && err == NULL)
4286 err = got_error_from_errno2("fclose", *path_outfile);
4287 if (path1 && unlink(path1) == -1 && err == NULL)
4288 err = got_error_from_errno2("unlink", path1);
4289 if (err || !have_content) {
4290 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4291 err = got_error_from_errno2("unlink", *path_outfile);
4292 free(*path_outfile);
4293 *path_outfile = NULL;
4295 free(args);
4296 if (ds) {
4297 got_diff_state_free(ds);
4298 free(ds);
4300 if (changes)
4301 got_diff_free_changes(changes);
4302 free(path1);
4303 return err;
4306 static const struct got_error *
4307 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4308 const char *relpath, struct got_object_id *blob_id,
4309 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4310 int dirfd, const char *de_name)
4312 struct revert_file_args *a = arg;
4313 const struct got_error *err = NULL;
4314 char *parent_path = NULL;
4315 struct got_fileindex_entry *ie;
4316 struct got_tree_object *tree = NULL;
4317 struct got_object_id *tree_id = NULL;
4318 const struct got_tree_entry *te = NULL;
4319 char *tree_path = NULL, *te_name;
4320 char *ondisk_path = NULL, *path_content = NULL;
4321 struct got_blob_object *blob = NULL;
4323 /* Reverting a staged deletion is a no-op. */
4324 if (status == GOT_STATUS_DELETE &&
4325 staged_status != GOT_STATUS_NO_CHANGE)
4326 return NULL;
4328 if (status == GOT_STATUS_UNVERSIONED)
4329 return (*a->progress_cb)(a->progress_arg,
4330 GOT_STATUS_UNVERSIONED, relpath);
4332 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4333 if (ie == NULL)
4334 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4336 /* Construct in-repository path of tree which contains this blob. */
4337 err = got_path_dirname(&parent_path, ie->path);
4338 if (err) {
4339 if (err->code != GOT_ERR_BAD_PATH)
4340 goto done;
4341 parent_path = strdup("/");
4342 if (parent_path == NULL) {
4343 err = got_error_from_errno("strdup");
4344 goto done;
4347 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4348 tree_path = strdup(parent_path);
4349 if (tree_path == NULL) {
4350 err = got_error_from_errno("strdup");
4351 goto done;
4353 } else {
4354 if (got_path_is_root_dir(parent_path)) {
4355 tree_path = strdup(a->worktree->path_prefix);
4356 if (tree_path == NULL) {
4357 err = got_error_from_errno("strdup");
4358 goto done;
4360 } else {
4361 if (asprintf(&tree_path, "%s/%s",
4362 a->worktree->path_prefix, parent_path) == -1) {
4363 err = got_error_from_errno("asprintf");
4364 goto done;
4369 err = got_object_id_by_path(&tree_id, a->repo,
4370 a->worktree->base_commit_id, tree_path);
4371 if (err) {
4372 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4373 (status == GOT_STATUS_ADD ||
4374 staged_status == GOT_STATUS_ADD)))
4375 goto done;
4376 } else {
4377 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4378 if (err)
4379 goto done;
4381 te_name = basename(ie->path);
4382 if (te_name == NULL) {
4383 err = got_error_from_errno2("basename", ie->path);
4384 goto done;
4387 te = got_object_tree_find_entry(tree, te_name);
4388 if (te == NULL && status != GOT_STATUS_ADD &&
4389 staged_status != GOT_STATUS_ADD) {
4390 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4391 goto done;
4395 switch (status) {
4396 case GOT_STATUS_ADD:
4397 if (a->patch_cb) {
4398 int choice = GOT_PATCH_CHOICE_NONE;
4399 err = (*a->patch_cb)(&choice, a->patch_arg,
4400 status, ie->path, NULL, 1, 1);
4401 if (err)
4402 goto done;
4403 if (choice != GOT_PATCH_CHOICE_YES)
4404 break;
4406 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4407 ie->path);
4408 if (err)
4409 goto done;
4410 got_fileindex_entry_remove(a->fileindex, ie);
4411 break;
4412 case GOT_STATUS_DELETE:
4413 if (a->patch_cb) {
4414 int choice = GOT_PATCH_CHOICE_NONE;
4415 err = (*a->patch_cb)(&choice, a->patch_arg,
4416 status, ie->path, NULL, 1, 1);
4417 if (err)
4418 goto done;
4419 if (choice != GOT_PATCH_CHOICE_YES)
4420 break;
4422 /* fall through */
4423 case GOT_STATUS_MODIFY:
4424 case GOT_STATUS_MODE_CHANGE:
4425 case GOT_STATUS_CONFLICT:
4426 case GOT_STATUS_MISSING: {
4427 struct got_object_id id;
4428 if (staged_status == GOT_STATUS_ADD ||
4429 staged_status == GOT_STATUS_MODIFY) {
4430 memcpy(id.sha1, ie->staged_blob_sha1,
4431 SHA1_DIGEST_LENGTH);
4432 } else
4433 memcpy(id.sha1, ie->blob_sha1,
4434 SHA1_DIGEST_LENGTH);
4435 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4436 if (err)
4437 goto done;
4439 if (asprintf(&ondisk_path, "%s/%s",
4440 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4441 err = got_error_from_errno("asprintf");
4442 goto done;
4445 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4446 status == GOT_STATUS_CONFLICT)) {
4447 int is_bad_symlink = 0;
4448 err = create_patched_content(&path_content, 1, &id,
4449 ondisk_path, dirfd, de_name, ie->path, a->repo,
4450 a->patch_cb, a->patch_arg);
4451 if (err || path_content == NULL)
4452 break;
4453 if (te && S_ISLNK(te->mode)) {
4454 if (unlink(path_content) == -1) {
4455 err = got_error_from_errno2("unlink",
4456 path_content);
4457 break;
4459 err = install_symlink(&is_bad_symlink,
4460 a->worktree, ondisk_path, ie->path,
4461 blob, 0, 1, 0, a->repo,
4462 a->progress_cb, a->progress_arg);
4463 } else {
4464 if (rename(path_content, ondisk_path) == -1) {
4465 err = got_error_from_errno3("rename",
4466 path_content, ondisk_path);
4467 goto done;
4470 } else {
4471 int is_bad_symlink = 0;
4472 if (te && S_ISLNK(te->mode)) {
4473 err = install_symlink(&is_bad_symlink,
4474 a->worktree, ondisk_path, ie->path,
4475 blob, 0, 1, 0, a->repo,
4476 a->progress_cb, a->progress_arg);
4477 } else {
4478 err = install_blob(a->worktree, ondisk_path,
4479 ie->path,
4480 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4481 got_fileindex_perms_to_st(ie), blob,
4482 0, 1, 0, 0, a->repo,
4483 a->progress_cb, a->progress_arg);
4485 if (err)
4486 goto done;
4487 if (status == GOT_STATUS_DELETE ||
4488 status == GOT_STATUS_MODE_CHANGE) {
4489 err = got_fileindex_entry_update(ie,
4490 ondisk_path, blob->id.sha1,
4491 a->worktree->base_commit_id->sha1, 1);
4492 if (err)
4493 goto done;
4495 if (is_bad_symlink) {
4496 got_fileindex_entry_filetype_set(ie,
4497 GOT_FILEIDX_MODE_BAD_SYMLINK);
4500 break;
4502 default:
4503 break;
4505 done:
4506 free(ondisk_path);
4507 free(path_content);
4508 free(parent_path);
4509 free(tree_path);
4510 if (blob)
4511 got_object_blob_close(blob);
4512 if (tree)
4513 got_object_tree_close(tree);
4514 free(tree_id);
4515 return err;
4518 const struct got_error *
4519 got_worktree_revert(struct got_worktree *worktree,
4520 struct got_pathlist_head *paths,
4521 got_worktree_checkout_cb progress_cb, void *progress_arg,
4522 got_worktree_patch_cb patch_cb, void *patch_arg,
4523 struct got_repository *repo)
4525 struct got_fileindex *fileindex = NULL;
4526 char *fileindex_path = NULL;
4527 const struct got_error *err = NULL, *unlockerr = NULL;
4528 const struct got_error *sync_err = NULL;
4529 struct got_pathlist_entry *pe;
4530 struct revert_file_args rfa;
4532 err = lock_worktree(worktree, LOCK_EX);
4533 if (err)
4534 return err;
4536 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4537 if (err)
4538 goto done;
4540 rfa.worktree = worktree;
4541 rfa.fileindex = fileindex;
4542 rfa.progress_cb = progress_cb;
4543 rfa.progress_arg = progress_arg;
4544 rfa.patch_cb = patch_cb;
4545 rfa.patch_arg = patch_arg;
4546 rfa.repo = repo;
4547 TAILQ_FOREACH(pe, paths, entry) {
4548 err = worktree_status(worktree, pe->path, fileindex, repo,
4549 revert_file, &rfa, NULL, NULL, 0, 0);
4550 if (err)
4551 break;
4553 sync_err = sync_fileindex(fileindex, fileindex_path);
4554 if (sync_err && err == NULL)
4555 err = sync_err;
4556 done:
4557 free(fileindex_path);
4558 if (fileindex)
4559 got_fileindex_free(fileindex);
4560 unlockerr = lock_worktree(worktree, LOCK_SH);
4561 if (unlockerr && err == NULL)
4562 err = unlockerr;
4563 return err;
4566 static void
4567 free_commitable(struct got_commitable *ct)
4569 free(ct->path);
4570 free(ct->in_repo_path);
4571 free(ct->ondisk_path);
4572 free(ct->blob_id);
4573 free(ct->base_blob_id);
4574 free(ct->staged_blob_id);
4575 free(ct->base_commit_id);
4576 free(ct);
4579 struct collect_commitables_arg {
4580 struct got_pathlist_head *commitable_paths;
4581 struct got_repository *repo;
4582 struct got_worktree *worktree;
4583 struct got_fileindex *fileindex;
4584 int have_staged_files;
4585 int allow_bad_symlinks;
4588 static const struct got_error *
4589 collect_commitables(void *arg, unsigned char status,
4590 unsigned char staged_status, const char *relpath,
4591 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4592 struct got_object_id *commit_id, int dirfd, const char *de_name)
4594 struct collect_commitables_arg *a = arg;
4595 const struct got_error *err = NULL;
4596 struct got_commitable *ct = NULL;
4597 struct got_pathlist_entry *new = NULL;
4598 char *parent_path = NULL, *path = NULL;
4599 struct stat sb;
4601 if (a->have_staged_files) {
4602 if (staged_status != GOT_STATUS_MODIFY &&
4603 staged_status != GOT_STATUS_ADD &&
4604 staged_status != GOT_STATUS_DELETE)
4605 return NULL;
4606 } else {
4607 if (status == GOT_STATUS_CONFLICT)
4608 return got_error(GOT_ERR_COMMIT_CONFLICT);
4610 if (status != GOT_STATUS_MODIFY &&
4611 status != GOT_STATUS_MODE_CHANGE &&
4612 status != GOT_STATUS_ADD &&
4613 status != GOT_STATUS_DELETE)
4614 return NULL;
4617 if (asprintf(&path, "/%s", relpath) == -1) {
4618 err = got_error_from_errno("asprintf");
4619 goto done;
4621 if (strcmp(path, "/") == 0) {
4622 parent_path = strdup("");
4623 if (parent_path == NULL)
4624 return got_error_from_errno("strdup");
4625 } else {
4626 err = got_path_dirname(&parent_path, path);
4627 if (err)
4628 return err;
4631 ct = calloc(1, sizeof(*ct));
4632 if (ct == NULL) {
4633 err = got_error_from_errno("calloc");
4634 goto done;
4637 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4638 relpath) == -1) {
4639 err = got_error_from_errno("asprintf");
4640 goto done;
4643 if (staged_status == GOT_STATUS_ADD ||
4644 staged_status == GOT_STATUS_MODIFY) {
4645 struct got_fileindex_entry *ie;
4646 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4647 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4648 case GOT_FILEIDX_MODE_REGULAR_FILE:
4649 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4650 ct->mode = S_IFREG;
4651 break;
4652 case GOT_FILEIDX_MODE_SYMLINK:
4653 ct->mode = S_IFLNK;
4654 break;
4655 default:
4656 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4657 goto done;
4659 ct->mode |= got_fileindex_entry_perms_get(ie);
4660 } else if (status != GOT_STATUS_DELETE &&
4661 staged_status != GOT_STATUS_DELETE) {
4662 if (dirfd != -1) {
4663 if (fstatat(dirfd, de_name, &sb,
4664 AT_SYMLINK_NOFOLLOW) == -1) {
4665 err = got_error_from_errno2("fstatat",
4666 ct->ondisk_path);
4667 goto done;
4669 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4670 err = got_error_from_errno2("lstat", ct->ondisk_path);
4671 goto done;
4673 ct->mode = sb.st_mode;
4676 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4677 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4678 relpath) == -1) {
4679 err = got_error_from_errno("asprintf");
4680 goto done;
4683 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4684 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4685 int is_bad_symlink;
4686 char target_path[PATH_MAX];
4687 ssize_t target_len;
4688 target_len = readlink(ct->ondisk_path, target_path,
4689 sizeof(target_path));
4690 if (target_len == -1) {
4691 err = got_error_from_errno2("readlink",
4692 ct->ondisk_path);
4693 goto done;
4695 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4696 target_len, ct->ondisk_path, a->worktree->root_path);
4697 if (err)
4698 goto done;
4699 if (is_bad_symlink) {
4700 err = got_error_path(ct->ondisk_path,
4701 GOT_ERR_BAD_SYMLINK);
4702 goto done;
4707 ct->status = status;
4708 ct->staged_status = staged_status;
4709 ct->blob_id = NULL; /* will be filled in when blob gets created */
4710 if (ct->status != GOT_STATUS_ADD &&
4711 ct->staged_status != GOT_STATUS_ADD) {
4712 ct->base_blob_id = got_object_id_dup(blob_id);
4713 if (ct->base_blob_id == NULL) {
4714 err = got_error_from_errno("got_object_id_dup");
4715 goto done;
4717 ct->base_commit_id = got_object_id_dup(commit_id);
4718 if (ct->base_commit_id == NULL) {
4719 err = got_error_from_errno("got_object_id_dup");
4720 goto done;
4723 if (ct->staged_status == GOT_STATUS_ADD ||
4724 ct->staged_status == GOT_STATUS_MODIFY) {
4725 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4726 if (ct->staged_blob_id == NULL) {
4727 err = got_error_from_errno("got_object_id_dup");
4728 goto done;
4731 ct->path = strdup(path);
4732 if (ct->path == NULL) {
4733 err = got_error_from_errno("strdup");
4734 goto done;
4736 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4737 done:
4738 if (ct && (err || new == NULL))
4739 free_commitable(ct);
4740 free(parent_path);
4741 free(path);
4742 return err;
4745 static const struct got_error *write_tree(struct got_object_id **, int *,
4746 struct got_tree_object *, const char *, struct got_pathlist_head *,
4747 got_worktree_status_cb status_cb, void *status_arg,
4748 struct got_repository *);
4750 static const struct got_error *
4751 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4752 struct got_tree_entry *te, const char *parent_path,
4753 struct got_pathlist_head *commitable_paths,
4754 got_worktree_status_cb status_cb, void *status_arg,
4755 struct got_repository *repo)
4757 const struct got_error *err = NULL;
4758 struct got_tree_object *subtree;
4759 char *subpath;
4761 if (asprintf(&subpath, "%s%s%s", parent_path,
4762 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4763 return got_error_from_errno("asprintf");
4765 err = got_object_open_as_tree(&subtree, repo, &te->id);
4766 if (err)
4767 return err;
4769 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4770 commitable_paths, status_cb, status_arg, repo);
4771 got_object_tree_close(subtree);
4772 free(subpath);
4773 return err;
4776 static const struct got_error *
4777 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4779 const struct got_error *err = NULL;
4780 char *ct_parent_path = NULL;
4782 *match = 0;
4784 if (strchr(ct->in_repo_path, '/') == NULL) {
4785 *match = got_path_is_root_dir(path);
4786 return NULL;
4789 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4790 if (err)
4791 return err;
4792 *match = (strcmp(path, ct_parent_path) == 0);
4793 free(ct_parent_path);
4794 return err;
4797 static mode_t
4798 get_ct_file_mode(struct got_commitable *ct)
4800 if (S_ISLNK(ct->mode))
4801 return S_IFLNK;
4803 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4806 static const struct got_error *
4807 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4808 struct got_tree_entry *te, struct got_commitable *ct)
4810 const struct got_error *err = NULL;
4812 *new_te = NULL;
4814 err = got_object_tree_entry_dup(new_te, te);
4815 if (err)
4816 goto done;
4818 (*new_te)->mode = get_ct_file_mode(ct);
4820 if (ct->staged_status == GOT_STATUS_MODIFY)
4821 memcpy(&(*new_te)->id, ct->staged_blob_id,
4822 sizeof((*new_te)->id));
4823 else
4824 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4825 done:
4826 if (err && *new_te) {
4827 free(*new_te);
4828 *new_te = NULL;
4830 return err;
4833 static const struct got_error *
4834 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4835 struct got_commitable *ct)
4837 const struct got_error *err = NULL;
4838 char *ct_name;
4840 *new_te = NULL;
4842 *new_te = calloc(1, sizeof(**new_te));
4843 if (*new_te == NULL)
4844 return got_error_from_errno("calloc");
4846 ct_name = basename(ct->path);
4847 if (ct_name == NULL) {
4848 err = got_error_from_errno2("basename", ct->path);
4849 goto done;
4851 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4852 sizeof((*new_te)->name)) {
4853 err = got_error(GOT_ERR_NO_SPACE);
4854 goto done;
4857 (*new_te)->mode = get_ct_file_mode(ct);
4859 if (ct->staged_status == GOT_STATUS_ADD)
4860 memcpy(&(*new_te)->id, ct->staged_blob_id,
4861 sizeof((*new_te)->id));
4862 else
4863 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4864 done:
4865 if (err && *new_te) {
4866 free(*new_te);
4867 *new_te = NULL;
4869 return err;
4872 static const struct got_error *
4873 insert_tree_entry(struct got_tree_entry *new_te,
4874 struct got_pathlist_head *paths)
4876 const struct got_error *err = NULL;
4877 struct got_pathlist_entry *new_pe;
4879 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4880 if (err)
4881 return err;
4882 if (new_pe == NULL)
4883 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4884 return NULL;
4887 static const struct got_error *
4888 report_ct_status(struct got_commitable *ct,
4889 got_worktree_status_cb status_cb, void *status_arg)
4891 const char *ct_path = ct->path;
4892 unsigned char status;
4894 while (ct_path[0] == '/')
4895 ct_path++;
4897 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4898 status = ct->staged_status;
4899 else
4900 status = ct->status;
4902 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4903 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4906 static const struct got_error *
4907 match_modified_subtree(int *modified, struct got_tree_entry *te,
4908 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4910 const struct got_error *err = NULL;
4911 struct got_pathlist_entry *pe;
4912 char *te_path;
4914 *modified = 0;
4916 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4917 got_path_is_root_dir(base_tree_path) ? "" : "/",
4918 te->name) == -1)
4919 return got_error_from_errno("asprintf");
4921 TAILQ_FOREACH(pe, commitable_paths, entry) {
4922 struct got_commitable *ct = pe->data;
4923 *modified = got_path_is_child(ct->in_repo_path, te_path,
4924 strlen(te_path));
4925 if (*modified)
4926 break;
4929 free(te_path);
4930 return err;
4933 static const struct got_error *
4934 match_deleted_or_modified_ct(struct got_commitable **ctp,
4935 struct got_tree_entry *te, const char *base_tree_path,
4936 struct got_pathlist_head *commitable_paths)
4938 const struct got_error *err = NULL;
4939 struct got_pathlist_entry *pe;
4941 *ctp = NULL;
4943 TAILQ_FOREACH(pe, commitable_paths, entry) {
4944 struct got_commitable *ct = pe->data;
4945 char *ct_name = NULL;
4946 int path_matches;
4948 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4949 if (ct->status != GOT_STATUS_MODIFY &&
4950 ct->status != GOT_STATUS_MODE_CHANGE &&
4951 ct->status != GOT_STATUS_DELETE)
4952 continue;
4953 } else {
4954 if (ct->staged_status != GOT_STATUS_MODIFY &&
4955 ct->staged_status != GOT_STATUS_DELETE)
4956 continue;
4959 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4960 continue;
4962 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4963 if (err)
4964 return err;
4965 if (!path_matches)
4966 continue;
4968 ct_name = basename(pe->path);
4969 if (ct_name == NULL)
4970 return got_error_from_errno2("basename", pe->path);
4972 if (strcmp(te->name, ct_name) != 0)
4973 continue;
4975 *ctp = ct;
4976 break;
4979 return err;
4982 static const struct got_error *
4983 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4984 const char *child_path, const char *path_base_tree,
4985 struct got_pathlist_head *commitable_paths,
4986 got_worktree_status_cb status_cb, void *status_arg,
4987 struct got_repository *repo)
4989 const struct got_error *err = NULL;
4990 struct got_tree_entry *new_te;
4991 char *subtree_path;
4992 struct got_object_id *id = NULL;
4993 int nentries;
4995 *new_tep = NULL;
4997 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4998 got_path_is_root_dir(path_base_tree) ? "" : "/",
4999 child_path) == -1)
5000 return got_error_from_errno("asprintf");
5002 new_te = calloc(1, sizeof(*new_te));
5003 if (new_te == NULL)
5004 return got_error_from_errno("calloc");
5005 new_te->mode = S_IFDIR;
5007 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5008 sizeof(new_te->name)) {
5009 err = got_error(GOT_ERR_NO_SPACE);
5010 goto done;
5012 err = write_tree(&id, &nentries, NULL, subtree_path,
5013 commitable_paths, status_cb, status_arg, repo);
5014 if (err) {
5015 free(new_te);
5016 goto done;
5018 memcpy(&new_te->id, id, sizeof(new_te->id));
5019 done:
5020 free(id);
5021 free(subtree_path);
5022 if (err == NULL)
5023 *new_tep = new_te;
5024 return err;
5027 static const struct got_error *
5028 write_tree(struct got_object_id **new_tree_id, int *nentries,
5029 struct got_tree_object *base_tree, const char *path_base_tree,
5030 struct got_pathlist_head *commitable_paths,
5031 got_worktree_status_cb status_cb, void *status_arg,
5032 struct got_repository *repo)
5034 const struct got_error *err = NULL;
5035 struct got_pathlist_head paths;
5036 struct got_tree_entry *te, *new_te = NULL;
5037 struct got_pathlist_entry *pe;
5039 TAILQ_INIT(&paths);
5040 *nentries = 0;
5042 /* Insert, and recurse into, newly added entries first. */
5043 TAILQ_FOREACH(pe, commitable_paths, entry) {
5044 struct got_commitable *ct = pe->data;
5045 char *child_path = NULL, *slash;
5047 if ((ct->status != GOT_STATUS_ADD &&
5048 ct->staged_status != GOT_STATUS_ADD) ||
5049 (ct->flags & GOT_COMMITABLE_ADDED))
5050 continue;
5052 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5053 strlen(path_base_tree)))
5054 continue;
5056 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5057 ct->in_repo_path);
5058 if (err)
5059 goto done;
5061 slash = strchr(child_path, '/');
5062 if (slash == NULL) {
5063 err = alloc_added_blob_tree_entry(&new_te, ct);
5064 if (err)
5065 goto done;
5066 err = report_ct_status(ct, status_cb, status_arg);
5067 if (err)
5068 goto done;
5069 ct->flags |= GOT_COMMITABLE_ADDED;
5070 err = insert_tree_entry(new_te, &paths);
5071 if (err)
5072 goto done;
5073 (*nentries)++;
5074 } else {
5075 *slash = '\0'; /* trim trailing path components */
5076 if (base_tree == NULL ||
5077 got_object_tree_find_entry(base_tree, child_path)
5078 == NULL) {
5079 err = make_subtree_for_added_blob(&new_te,
5080 child_path, path_base_tree,
5081 commitable_paths, status_cb, status_arg,
5082 repo);
5083 if (err)
5084 goto done;
5085 err = insert_tree_entry(new_te, &paths);
5086 if (err)
5087 goto done;
5088 (*nentries)++;
5093 if (base_tree) {
5094 int i, nbase_entries;
5095 /* Handle modified and deleted entries. */
5096 nbase_entries = got_object_tree_get_nentries(base_tree);
5097 for (i = 0; i < nbase_entries; i++) {
5098 struct got_commitable *ct = NULL;
5100 te = got_object_tree_get_entry(base_tree, i);
5101 if (got_object_tree_entry_is_submodule(te)) {
5102 /* Entry is a submodule; just copy it. */
5103 err = got_object_tree_entry_dup(&new_te, te);
5104 if (err)
5105 goto done;
5106 err = insert_tree_entry(new_te, &paths);
5107 if (err)
5108 goto done;
5109 (*nentries)++;
5110 continue;
5113 if (S_ISDIR(te->mode)) {
5114 int modified;
5115 err = got_object_tree_entry_dup(&new_te, te);
5116 if (err)
5117 goto done;
5118 err = match_modified_subtree(&modified, te,
5119 path_base_tree, commitable_paths);
5120 if (err)
5121 goto done;
5122 /* Avoid recursion into unmodified subtrees. */
5123 if (modified) {
5124 struct got_object_id *new_id;
5125 int nsubentries;
5126 err = write_subtree(&new_id,
5127 &nsubentries, te,
5128 path_base_tree, commitable_paths,
5129 status_cb, status_arg, repo);
5130 if (err)
5131 goto done;
5132 if (nsubentries == 0) {
5133 /* All entries were deleted. */
5134 free(new_id);
5135 continue;
5137 memcpy(&new_te->id, new_id,
5138 sizeof(new_te->id));
5139 free(new_id);
5141 err = insert_tree_entry(new_te, &paths);
5142 if (err)
5143 goto done;
5144 (*nentries)++;
5145 continue;
5148 err = match_deleted_or_modified_ct(&ct, te,
5149 path_base_tree, commitable_paths);
5150 if (err)
5151 goto done;
5152 if (ct) {
5153 /* NB: Deleted entries get dropped here. */
5154 if (ct->status == GOT_STATUS_MODIFY ||
5155 ct->status == GOT_STATUS_MODE_CHANGE ||
5156 ct->staged_status == GOT_STATUS_MODIFY) {
5157 err = alloc_modified_blob_tree_entry(
5158 &new_te, te, ct);
5159 if (err)
5160 goto done;
5161 err = insert_tree_entry(new_te, &paths);
5162 if (err)
5163 goto done;
5164 (*nentries)++;
5166 err = report_ct_status(ct, status_cb,
5167 status_arg);
5168 if (err)
5169 goto done;
5170 } else {
5171 /* Entry is unchanged; just copy it. */
5172 err = got_object_tree_entry_dup(&new_te, te);
5173 if (err)
5174 goto done;
5175 err = insert_tree_entry(new_te, &paths);
5176 if (err)
5177 goto done;
5178 (*nentries)++;
5183 /* Write new list of entries; deleted entries have been dropped. */
5184 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5185 done:
5186 got_pathlist_free(&paths);
5187 return err;
5190 static const struct got_error *
5191 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5192 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5193 int have_staged_files)
5195 const struct got_error *err = NULL;
5196 struct got_pathlist_entry *pe;
5198 TAILQ_FOREACH(pe, commitable_paths, entry) {
5199 struct got_fileindex_entry *ie;
5200 struct got_commitable *ct = pe->data;
5202 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5203 if (ie) {
5204 if (ct->status == GOT_STATUS_DELETE ||
5205 ct->staged_status == GOT_STATUS_DELETE) {
5206 got_fileindex_entry_remove(fileindex, ie);
5207 } else if (ct->staged_status == GOT_STATUS_ADD ||
5208 ct->staged_status == GOT_STATUS_MODIFY) {
5209 got_fileindex_entry_stage_set(ie,
5210 GOT_FILEIDX_STAGE_NONE);
5211 err = got_fileindex_entry_update(ie,
5212 ct->ondisk_path, ct->staged_blob_id->sha1,
5213 new_base_commit_id->sha1,
5214 !have_staged_files);
5215 } else
5216 err = got_fileindex_entry_update(ie,
5217 ct->ondisk_path, ct->blob_id->sha1,
5218 new_base_commit_id->sha1,
5219 !have_staged_files);
5220 } else {
5221 err = got_fileindex_entry_alloc(&ie, pe->path);
5222 if (err)
5223 break;
5224 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5225 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5226 if (err) {
5227 got_fileindex_entry_free(ie);
5228 break;
5230 err = got_fileindex_entry_add(fileindex, ie);
5231 if (err) {
5232 got_fileindex_entry_free(ie);
5233 break;
5237 return err;
5241 static const struct got_error *
5242 check_out_of_date(const char *in_repo_path, unsigned char status,
5243 unsigned char staged_status, struct got_object_id *base_blob_id,
5244 struct got_object_id *base_commit_id,
5245 struct got_object_id *head_commit_id, struct got_repository *repo,
5246 int ood_errcode)
5248 const struct got_error *err = NULL;
5249 struct got_object_id *id = NULL;
5251 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5252 /* Trivial case: base commit == head commit */
5253 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5254 return NULL;
5256 * Ensure file content which local changes were based
5257 * on matches file content in the branch head.
5259 err = got_object_id_by_path(&id, repo, head_commit_id,
5260 in_repo_path);
5261 if (err) {
5262 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5263 err = got_error(ood_errcode);
5264 goto done;
5265 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5266 err = got_error(ood_errcode);
5267 } else {
5268 /* Require that added files don't exist in the branch head. */
5269 err = got_object_id_by_path(&id, repo, head_commit_id,
5270 in_repo_path);
5271 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5272 goto done;
5273 err = id ? got_error(ood_errcode) : NULL;
5275 done:
5276 free(id);
5277 return err;
5280 const struct got_error *
5281 commit_worktree(struct got_object_id **new_commit_id,
5282 struct got_pathlist_head *commitable_paths,
5283 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5284 const char *author, const char *committer,
5285 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5286 got_worktree_status_cb status_cb, void *status_arg,
5287 struct got_repository *repo)
5289 const struct got_error *err = NULL, *unlockerr = NULL;
5290 struct got_pathlist_entry *pe;
5291 const char *head_ref_name = NULL;
5292 struct got_commit_object *head_commit = NULL;
5293 struct got_reference *head_ref2 = NULL;
5294 struct got_object_id *head_commit_id2 = NULL;
5295 struct got_tree_object *head_tree = NULL;
5296 struct got_object_id *new_tree_id = NULL;
5297 int nentries;
5298 struct got_object_id_queue parent_ids;
5299 struct got_object_qid *pid = NULL;
5300 char *logmsg = NULL;
5302 *new_commit_id = NULL;
5304 SIMPLEQ_INIT(&parent_ids);
5306 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5307 if (err)
5308 goto done;
5310 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5311 if (err)
5312 goto done;
5314 if (commit_msg_cb != NULL) {
5315 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5316 if (err)
5317 goto done;
5320 if (logmsg == NULL || strlen(logmsg) == 0) {
5321 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5322 goto done;
5325 /* Create blobs from added and modified files and record their IDs. */
5326 TAILQ_FOREACH(pe, commitable_paths, entry) {
5327 struct got_commitable *ct = pe->data;
5328 char *ondisk_path;
5330 /* Blobs for staged files already exist. */
5331 if (ct->staged_status == GOT_STATUS_ADD ||
5332 ct->staged_status == GOT_STATUS_MODIFY)
5333 continue;
5335 if (ct->status != GOT_STATUS_ADD &&
5336 ct->status != GOT_STATUS_MODIFY &&
5337 ct->status != GOT_STATUS_MODE_CHANGE)
5338 continue;
5340 if (asprintf(&ondisk_path, "%s/%s",
5341 worktree->root_path, pe->path) == -1) {
5342 err = got_error_from_errno("asprintf");
5343 goto done;
5345 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5346 free(ondisk_path);
5347 if (err)
5348 goto done;
5351 /* Recursively write new tree objects. */
5352 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5353 commitable_paths, status_cb, status_arg, repo);
5354 if (err)
5355 goto done;
5357 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5358 if (err)
5359 goto done;
5360 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5361 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5362 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5363 got_object_qid_free(pid);
5364 if (logmsg != NULL)
5365 free(logmsg);
5366 if (err)
5367 goto done;
5369 /* Check if a concurrent commit to our branch has occurred. */
5370 head_ref_name = got_worktree_get_head_ref_name(worktree);
5371 if (head_ref_name == NULL) {
5372 err = got_error_from_errno("got_worktree_get_head_ref_name");
5373 goto done;
5375 /* Lock the reference here to prevent concurrent modification. */
5376 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5377 if (err)
5378 goto done;
5379 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5380 if (err)
5381 goto done;
5382 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5383 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5384 goto done;
5386 /* Update branch head in repository. */
5387 err = got_ref_change_ref(head_ref2, *new_commit_id);
5388 if (err)
5389 goto done;
5390 err = got_ref_write(head_ref2, repo);
5391 if (err)
5392 goto done;
5394 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5395 if (err)
5396 goto done;
5398 err = ref_base_commit(worktree, repo);
5399 if (err)
5400 goto done;
5401 done:
5402 if (head_tree)
5403 got_object_tree_close(head_tree);
5404 if (head_commit)
5405 got_object_commit_close(head_commit);
5406 free(head_commit_id2);
5407 if (head_ref2) {
5408 unlockerr = got_ref_unlock(head_ref2);
5409 if (unlockerr && err == NULL)
5410 err = unlockerr;
5411 got_ref_close(head_ref2);
5413 return err;
5416 static const struct got_error *
5417 check_path_is_commitable(const char *path,
5418 struct got_pathlist_head *commitable_paths)
5420 struct got_pathlist_entry *cpe = NULL;
5421 size_t path_len = strlen(path);
5423 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5424 struct got_commitable *ct = cpe->data;
5425 const char *ct_path = ct->path;
5427 while (ct_path[0] == '/')
5428 ct_path++;
5430 if (strcmp(path, ct_path) == 0 ||
5431 got_path_is_child(ct_path, path, path_len))
5432 break;
5435 if (cpe == NULL)
5436 return got_error_path(path, GOT_ERR_BAD_PATH);
5438 return NULL;
5441 static const struct got_error *
5442 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5444 int *have_staged_files = arg;
5446 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5447 *have_staged_files = 1;
5448 return got_error(GOT_ERR_CANCELLED);
5451 return NULL;
5454 static const struct got_error *
5455 check_non_staged_files(struct got_fileindex *fileindex,
5456 struct got_pathlist_head *paths)
5458 struct got_pathlist_entry *pe;
5459 struct got_fileindex_entry *ie;
5461 TAILQ_FOREACH(pe, paths, entry) {
5462 if (pe->path[0] == '\0')
5463 continue;
5464 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5465 if (ie == NULL)
5466 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5467 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5468 return got_error_path(pe->path,
5469 GOT_ERR_FILE_NOT_STAGED);
5472 return NULL;
5475 const struct got_error *
5476 got_worktree_commit(struct got_object_id **new_commit_id,
5477 struct got_worktree *worktree, struct got_pathlist_head *paths,
5478 const char *author, const char *committer, int allow_bad_symlinks,
5479 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5480 got_worktree_status_cb status_cb, void *status_arg,
5481 struct got_repository *repo)
5483 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5484 struct got_fileindex *fileindex = NULL;
5485 char *fileindex_path = NULL;
5486 struct got_pathlist_head commitable_paths;
5487 struct collect_commitables_arg cc_arg;
5488 struct got_pathlist_entry *pe;
5489 struct got_reference *head_ref = NULL;
5490 struct got_object_id *head_commit_id = NULL;
5491 int have_staged_files = 0;
5493 *new_commit_id = NULL;
5495 TAILQ_INIT(&commitable_paths);
5497 err = lock_worktree(worktree, LOCK_EX);
5498 if (err)
5499 goto done;
5501 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5502 if (err)
5503 goto done;
5505 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5506 if (err)
5507 goto done;
5509 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5510 if (err)
5511 goto done;
5513 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5514 &have_staged_files);
5515 if (err && err->code != GOT_ERR_CANCELLED)
5516 goto done;
5517 if (have_staged_files) {
5518 err = check_non_staged_files(fileindex, paths);
5519 if (err)
5520 goto done;
5523 cc_arg.commitable_paths = &commitable_paths;
5524 cc_arg.worktree = worktree;
5525 cc_arg.fileindex = fileindex;
5526 cc_arg.repo = repo;
5527 cc_arg.have_staged_files = have_staged_files;
5528 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5529 TAILQ_FOREACH(pe, paths, entry) {
5530 err = worktree_status(worktree, pe->path, fileindex, repo,
5531 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5532 if (err)
5533 goto done;
5536 if (TAILQ_EMPTY(&commitable_paths)) {
5537 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5538 goto done;
5541 TAILQ_FOREACH(pe, paths, entry) {
5542 err = check_path_is_commitable(pe->path, &commitable_paths);
5543 if (err)
5544 goto done;
5547 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5548 struct got_commitable *ct = pe->data;
5549 const char *ct_path = ct->in_repo_path;
5551 while (ct_path[0] == '/')
5552 ct_path++;
5553 err = check_out_of_date(ct_path, ct->status,
5554 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5555 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5556 if (err)
5557 goto done;
5561 err = commit_worktree(new_commit_id, &commitable_paths,
5562 head_commit_id, worktree, author, committer,
5563 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5564 if (err)
5565 goto done;
5567 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5568 fileindex, have_staged_files);
5569 sync_err = sync_fileindex(fileindex, fileindex_path);
5570 if (sync_err && err == NULL)
5571 err = sync_err;
5572 done:
5573 if (fileindex)
5574 got_fileindex_free(fileindex);
5575 free(fileindex_path);
5576 unlockerr = lock_worktree(worktree, LOCK_SH);
5577 if (unlockerr && err == NULL)
5578 err = unlockerr;
5579 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5580 struct got_commitable *ct = pe->data;
5581 free_commitable(ct);
5583 got_pathlist_free(&commitable_paths);
5584 return err;
5587 const char *
5588 got_commitable_get_path(struct got_commitable *ct)
5590 return ct->path;
5593 unsigned int
5594 got_commitable_get_status(struct got_commitable *ct)
5596 return ct->status;
5599 struct check_rebase_ok_arg {
5600 struct got_worktree *worktree;
5601 struct got_repository *repo;
5604 static const struct got_error *
5605 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5607 const struct got_error *err = NULL;
5608 struct check_rebase_ok_arg *a = arg;
5609 unsigned char status;
5610 struct stat sb;
5611 char *ondisk_path;
5613 /* Reject rebase of a work tree with mixed base commits. */
5614 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5615 SHA1_DIGEST_LENGTH))
5616 return got_error(GOT_ERR_MIXED_COMMITS);
5618 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5619 == -1)
5620 return got_error_from_errno("asprintf");
5622 /* Reject rebase of a work tree with modified or staged files. */
5623 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5624 free(ondisk_path);
5625 if (err)
5626 return err;
5628 if (status != GOT_STATUS_NO_CHANGE)
5629 return got_error(GOT_ERR_MODIFIED);
5630 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5631 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5633 return NULL;
5636 const struct got_error *
5637 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5638 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5639 struct got_worktree *worktree, struct got_reference *branch,
5640 struct got_repository *repo)
5642 const struct got_error *err = NULL;
5643 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5644 char *branch_ref_name = NULL;
5645 char *fileindex_path = NULL;
5646 struct check_rebase_ok_arg ok_arg;
5647 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5648 struct got_object_id *wt_branch_tip = NULL;
5650 *new_base_branch_ref = NULL;
5651 *tmp_branch = NULL;
5652 *fileindex = NULL;
5654 err = lock_worktree(worktree, LOCK_EX);
5655 if (err)
5656 return err;
5658 err = open_fileindex(fileindex, &fileindex_path, worktree);
5659 if (err)
5660 goto done;
5662 ok_arg.worktree = worktree;
5663 ok_arg.repo = repo;
5664 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5665 &ok_arg);
5666 if (err)
5667 goto done;
5669 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5670 if (err)
5671 goto done;
5673 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5674 if (err)
5675 goto done;
5677 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5678 if (err)
5679 goto done;
5681 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5682 0);
5683 if (err)
5684 goto done;
5686 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5687 if (err)
5688 goto done;
5689 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5690 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5691 goto done;
5694 err = got_ref_alloc_symref(new_base_branch_ref,
5695 new_base_branch_ref_name, wt_branch);
5696 if (err)
5697 goto done;
5698 err = got_ref_write(*new_base_branch_ref, repo);
5699 if (err)
5700 goto done;
5702 /* TODO Lock original branch's ref while rebasing? */
5704 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5705 if (err)
5706 goto done;
5708 err = got_ref_write(branch_ref, repo);
5709 if (err)
5710 goto done;
5712 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5713 worktree->base_commit_id);
5714 if (err)
5715 goto done;
5716 err = got_ref_write(*tmp_branch, repo);
5717 if (err)
5718 goto done;
5720 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5721 if (err)
5722 goto done;
5723 done:
5724 free(fileindex_path);
5725 free(tmp_branch_name);
5726 free(new_base_branch_ref_name);
5727 free(branch_ref_name);
5728 if (branch_ref)
5729 got_ref_close(branch_ref);
5730 if (wt_branch)
5731 got_ref_close(wt_branch);
5732 free(wt_branch_tip);
5733 if (err) {
5734 if (*new_base_branch_ref) {
5735 got_ref_close(*new_base_branch_ref);
5736 *new_base_branch_ref = NULL;
5738 if (*tmp_branch) {
5739 got_ref_close(*tmp_branch);
5740 *tmp_branch = NULL;
5742 if (*fileindex) {
5743 got_fileindex_free(*fileindex);
5744 *fileindex = NULL;
5746 lock_worktree(worktree, LOCK_SH);
5748 return err;
5751 const struct got_error *
5752 got_worktree_rebase_continue(struct got_object_id **commit_id,
5753 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5754 struct got_reference **branch, struct got_fileindex **fileindex,
5755 struct got_worktree *worktree, struct got_repository *repo)
5757 const struct got_error *err;
5758 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5759 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5760 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5761 char *fileindex_path = NULL;
5762 int have_staged_files = 0;
5764 *commit_id = NULL;
5765 *new_base_branch = NULL;
5766 *tmp_branch = NULL;
5767 *branch = NULL;
5768 *fileindex = NULL;
5770 err = lock_worktree(worktree, LOCK_EX);
5771 if (err)
5772 return err;
5774 err = open_fileindex(fileindex, &fileindex_path, worktree);
5775 if (err)
5776 goto done;
5778 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5779 &have_staged_files);
5780 if (err && err->code != GOT_ERR_CANCELLED)
5781 goto done;
5782 if (have_staged_files) {
5783 err = got_error(GOT_ERR_STAGED_PATHS);
5784 goto done;
5787 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5788 if (err)
5789 goto done;
5791 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5792 if (err)
5793 goto done;
5795 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5796 if (err)
5797 goto done;
5799 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5800 if (err)
5801 goto done;
5803 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5804 if (err)
5805 goto done;
5807 err = got_ref_open(branch, repo,
5808 got_ref_get_symref_target(branch_ref), 0);
5809 if (err)
5810 goto done;
5812 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5813 if (err)
5814 goto done;
5816 err = got_ref_resolve(commit_id, repo, commit_ref);
5817 if (err)
5818 goto done;
5820 err = got_ref_open(new_base_branch, repo,
5821 new_base_branch_ref_name, 0);
5822 if (err)
5823 goto done;
5825 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5826 if (err)
5827 goto done;
5828 done:
5829 free(commit_ref_name);
5830 free(branch_ref_name);
5831 free(fileindex_path);
5832 if (commit_ref)
5833 got_ref_close(commit_ref);
5834 if (branch_ref)
5835 got_ref_close(branch_ref);
5836 if (err) {
5837 free(*commit_id);
5838 *commit_id = NULL;
5839 if (*tmp_branch) {
5840 got_ref_close(*tmp_branch);
5841 *tmp_branch = NULL;
5843 if (*new_base_branch) {
5844 got_ref_close(*new_base_branch);
5845 *new_base_branch = NULL;
5847 if (*branch) {
5848 got_ref_close(*branch);
5849 *branch = NULL;
5851 if (*fileindex) {
5852 got_fileindex_free(*fileindex);
5853 *fileindex = NULL;
5855 lock_worktree(worktree, LOCK_SH);
5857 return err;
5860 const struct got_error *
5861 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5863 const struct got_error *err;
5864 char *tmp_branch_name = NULL;
5866 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5867 if (err)
5868 return err;
5870 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5871 free(tmp_branch_name);
5872 return NULL;
5875 static const struct got_error *
5876 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5877 char **logmsg, void *arg)
5879 *logmsg = arg;
5880 return NULL;
5883 static const struct got_error *
5884 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5885 const char *path, struct got_object_id *blob_id,
5886 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5887 int dirfd, const char *de_name)
5889 return NULL;
5892 struct collect_merged_paths_arg {
5893 got_worktree_checkout_cb progress_cb;
5894 void *progress_arg;
5895 struct got_pathlist_head *merged_paths;
5898 static const struct got_error *
5899 collect_merged_paths(void *arg, unsigned char status, const char *path)
5901 const struct got_error *err;
5902 struct collect_merged_paths_arg *a = arg;
5903 char *p;
5904 struct got_pathlist_entry *new;
5906 err = (*a->progress_cb)(a->progress_arg, status, path);
5907 if (err)
5908 return err;
5910 if (status != GOT_STATUS_MERGE &&
5911 status != GOT_STATUS_ADD &&
5912 status != GOT_STATUS_DELETE &&
5913 status != GOT_STATUS_CONFLICT)
5914 return NULL;
5916 p = strdup(path);
5917 if (p == NULL)
5918 return got_error_from_errno("strdup");
5920 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5921 if (err || new == NULL)
5922 free(p);
5923 return err;
5926 void
5927 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5929 struct got_pathlist_entry *pe;
5931 TAILQ_FOREACH(pe, merged_paths, entry)
5932 free((char *)pe->path);
5934 got_pathlist_free(merged_paths);
5937 static const struct got_error *
5938 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5939 int is_rebase, struct got_repository *repo)
5941 const struct got_error *err;
5942 struct got_reference *commit_ref = NULL;
5944 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5945 if (err) {
5946 if (err->code != GOT_ERR_NOT_REF)
5947 goto done;
5948 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5949 if (err)
5950 goto done;
5951 err = got_ref_write(commit_ref, repo);
5952 if (err)
5953 goto done;
5954 } else if (is_rebase) {
5955 struct got_object_id *stored_id;
5956 int cmp;
5958 err = got_ref_resolve(&stored_id, repo, commit_ref);
5959 if (err)
5960 goto done;
5961 cmp = got_object_id_cmp(commit_id, stored_id);
5962 free(stored_id);
5963 if (cmp != 0) {
5964 err = got_error(GOT_ERR_REBASE_COMMITID);
5965 goto done;
5968 done:
5969 if (commit_ref)
5970 got_ref_close(commit_ref);
5971 return err;
5974 static const struct got_error *
5975 rebase_merge_files(struct got_pathlist_head *merged_paths,
5976 const char *commit_ref_name, struct got_worktree *worktree,
5977 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5978 struct got_object_id *commit_id, struct got_repository *repo,
5979 got_worktree_checkout_cb progress_cb, void *progress_arg,
5980 got_cancel_cb cancel_cb, void *cancel_arg)
5982 const struct got_error *err;
5983 struct got_reference *commit_ref = NULL;
5984 struct collect_merged_paths_arg cmp_arg;
5985 char *fileindex_path;
5987 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5989 err = get_fileindex_path(&fileindex_path, worktree);
5990 if (err)
5991 return err;
5993 cmp_arg.progress_cb = progress_cb;
5994 cmp_arg.progress_arg = progress_arg;
5995 cmp_arg.merged_paths = merged_paths;
5996 err = merge_files(worktree, fileindex, fileindex_path,
5997 parent_commit_id, commit_id, repo, collect_merged_paths,
5998 &cmp_arg, cancel_cb, cancel_arg);
5999 if (commit_ref)
6000 got_ref_close(commit_ref);
6001 return err;
6004 const struct got_error *
6005 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6006 struct got_worktree *worktree, struct got_fileindex *fileindex,
6007 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6008 struct got_repository *repo,
6009 got_worktree_checkout_cb progress_cb, void *progress_arg,
6010 got_cancel_cb cancel_cb, void *cancel_arg)
6012 const struct got_error *err;
6013 char *commit_ref_name;
6015 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6016 if (err)
6017 return err;
6019 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6020 if (err)
6021 goto done;
6023 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6024 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6025 progress_arg, cancel_cb, cancel_arg);
6026 done:
6027 free(commit_ref_name);
6028 return err;
6031 const struct got_error *
6032 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6033 struct got_worktree *worktree, struct got_fileindex *fileindex,
6034 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6035 struct got_repository *repo,
6036 got_worktree_checkout_cb progress_cb, void *progress_arg,
6037 got_cancel_cb cancel_cb, void *cancel_arg)
6039 const struct got_error *err;
6040 char *commit_ref_name;
6042 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6043 if (err)
6044 return err;
6046 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6047 if (err)
6048 goto done;
6050 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6051 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6052 progress_arg, cancel_cb, cancel_arg);
6053 done:
6054 free(commit_ref_name);
6055 return err;
6058 static const struct got_error *
6059 rebase_commit(struct got_object_id **new_commit_id,
6060 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6061 struct got_worktree *worktree, struct got_fileindex *fileindex,
6062 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6063 const char *new_logmsg, struct got_repository *repo)
6065 const struct got_error *err, *sync_err;
6066 struct got_pathlist_head commitable_paths;
6067 struct collect_commitables_arg cc_arg;
6068 char *fileindex_path = NULL;
6069 struct got_reference *head_ref = NULL;
6070 struct got_object_id *head_commit_id = NULL;
6071 char *logmsg = NULL;
6073 TAILQ_INIT(&commitable_paths);
6074 *new_commit_id = NULL;
6076 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6078 err = get_fileindex_path(&fileindex_path, worktree);
6079 if (err)
6080 return err;
6082 cc_arg.commitable_paths = &commitable_paths;
6083 cc_arg.worktree = worktree;
6084 cc_arg.repo = repo;
6085 cc_arg.have_staged_files = 0;
6087 * If possible get the status of individual files directly to
6088 * avoid crawling the entire work tree once per rebased commit.
6089 * TODO: Ideally, merged_paths would contain a list of commitables
6090 * we could use so we could skip worktree_status() entirely.
6092 if (merged_paths) {
6093 struct got_pathlist_entry *pe;
6094 TAILQ_FOREACH(pe, merged_paths, entry) {
6095 err = worktree_status(worktree, pe->path, fileindex,
6096 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6097 0);
6098 if (err)
6099 goto done;
6101 } else {
6102 err = worktree_status(worktree, "", fileindex, repo,
6103 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6104 if (err)
6105 goto done;
6108 if (TAILQ_EMPTY(&commitable_paths)) {
6109 /* No-op change; commit will be elided. */
6110 err = got_ref_delete(commit_ref, repo);
6111 if (err)
6112 goto done;
6113 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6114 goto done;
6117 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6118 if (err)
6119 goto done;
6121 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6122 if (err)
6123 goto done;
6125 if (new_logmsg) {
6126 logmsg = strdup(new_logmsg);
6127 if (logmsg == NULL) {
6128 err = got_error_from_errno("strdup");
6129 goto done;
6131 } else {
6132 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6133 if (err)
6134 goto done;
6137 /* NB: commit_worktree will call free(logmsg) */
6138 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6139 worktree, got_object_commit_get_author(orig_commit),
6140 got_object_commit_get_committer(orig_commit),
6141 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6142 if (err)
6143 goto done;
6145 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6146 if (err)
6147 goto done;
6149 err = got_ref_delete(commit_ref, repo);
6150 if (err)
6151 goto done;
6153 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6154 fileindex, 0);
6155 sync_err = sync_fileindex(fileindex, fileindex_path);
6156 if (sync_err && err == NULL)
6157 err = sync_err;
6158 done:
6159 free(fileindex_path);
6160 free(head_commit_id);
6161 if (head_ref)
6162 got_ref_close(head_ref);
6163 if (err) {
6164 free(*new_commit_id);
6165 *new_commit_id = NULL;
6167 return err;
6170 const struct got_error *
6171 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6172 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6173 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6174 struct got_commit_object *orig_commit,
6175 struct got_object_id *orig_commit_id, struct got_repository *repo)
6177 const struct got_error *err;
6178 char *commit_ref_name;
6179 struct got_reference *commit_ref = NULL;
6180 struct got_object_id *commit_id = NULL;
6182 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6183 if (err)
6184 return err;
6186 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6187 if (err)
6188 goto done;
6189 err = got_ref_resolve(&commit_id, repo, commit_ref);
6190 if (err)
6191 goto done;
6192 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6193 err = got_error(GOT_ERR_REBASE_COMMITID);
6194 goto done;
6197 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6198 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6199 done:
6200 if (commit_ref)
6201 got_ref_close(commit_ref);
6202 free(commit_ref_name);
6203 free(commit_id);
6204 return err;
6207 const struct got_error *
6208 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6209 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6210 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6211 struct got_commit_object *orig_commit,
6212 struct got_object_id *orig_commit_id, const char *new_logmsg,
6213 struct got_repository *repo)
6215 const struct got_error *err;
6216 char *commit_ref_name;
6217 struct got_reference *commit_ref = NULL;
6219 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6220 if (err)
6221 return err;
6223 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6224 if (err)
6225 goto done;
6227 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6228 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6229 done:
6230 if (commit_ref)
6231 got_ref_close(commit_ref);
6232 free(commit_ref_name);
6233 return err;
6236 const struct got_error *
6237 got_worktree_rebase_postpone(struct got_worktree *worktree,
6238 struct got_fileindex *fileindex)
6240 if (fileindex)
6241 got_fileindex_free(fileindex);
6242 return lock_worktree(worktree, LOCK_SH);
6245 static const struct got_error *
6246 delete_ref(const char *name, struct got_repository *repo)
6248 const struct got_error *err;
6249 struct got_reference *ref;
6251 err = got_ref_open(&ref, repo, name, 0);
6252 if (err) {
6253 if (err->code == GOT_ERR_NOT_REF)
6254 return NULL;
6255 return err;
6258 err = got_ref_delete(ref, repo);
6259 got_ref_close(ref);
6260 return err;
6263 static const struct got_error *
6264 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6266 const struct got_error *err;
6267 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6268 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6270 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6271 if (err)
6272 goto done;
6273 err = delete_ref(tmp_branch_name, repo);
6274 if (err)
6275 goto done;
6277 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6278 if (err)
6279 goto done;
6280 err = delete_ref(new_base_branch_ref_name, repo);
6281 if (err)
6282 goto done;
6284 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6285 if (err)
6286 goto done;
6287 err = delete_ref(branch_ref_name, repo);
6288 if (err)
6289 goto done;
6291 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6292 if (err)
6293 goto done;
6294 err = delete_ref(commit_ref_name, repo);
6295 if (err)
6296 goto done;
6298 done:
6299 free(tmp_branch_name);
6300 free(new_base_branch_ref_name);
6301 free(branch_ref_name);
6302 free(commit_ref_name);
6303 return err;
6306 const struct got_error *
6307 got_worktree_rebase_complete(struct got_worktree *worktree,
6308 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6309 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6310 struct got_repository *repo)
6312 const struct got_error *err, *unlockerr;
6313 struct got_object_id *new_head_commit_id = NULL;
6315 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6316 if (err)
6317 return err;
6319 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6320 if (err)
6321 goto done;
6323 err = got_ref_write(rebased_branch, repo);
6324 if (err)
6325 goto done;
6327 err = got_worktree_set_head_ref(worktree, rebased_branch);
6328 if (err)
6329 goto done;
6331 err = delete_rebase_refs(worktree, repo);
6332 done:
6333 if (fileindex)
6334 got_fileindex_free(fileindex);
6335 free(new_head_commit_id);
6336 unlockerr = lock_worktree(worktree, LOCK_SH);
6337 if (unlockerr && err == NULL)
6338 err = unlockerr;
6339 return err;
6342 const struct got_error *
6343 got_worktree_rebase_abort(struct got_worktree *worktree,
6344 struct got_fileindex *fileindex, struct got_repository *repo,
6345 struct got_reference *new_base_branch,
6346 got_worktree_checkout_cb progress_cb, void *progress_arg)
6348 const struct got_error *err, *unlockerr, *sync_err;
6349 struct got_reference *resolved = NULL;
6350 struct got_object_id *commit_id = NULL;
6351 char *fileindex_path = NULL;
6352 struct revert_file_args rfa;
6353 struct got_object_id *tree_id = NULL;
6355 err = lock_worktree(worktree, LOCK_EX);
6356 if (err)
6357 return err;
6359 err = got_ref_open(&resolved, repo,
6360 got_ref_get_symref_target(new_base_branch), 0);
6361 if (err)
6362 goto done;
6364 err = got_worktree_set_head_ref(worktree, resolved);
6365 if (err)
6366 goto done;
6369 * XXX commits to the base branch could have happened while
6370 * we were busy rebasing; should we store the original commit ID
6371 * when rebase begins and read it back here?
6373 err = got_ref_resolve(&commit_id, repo, resolved);
6374 if (err)
6375 goto done;
6377 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6378 if (err)
6379 goto done;
6381 err = got_object_id_by_path(&tree_id, repo,
6382 worktree->base_commit_id, worktree->path_prefix);
6383 if (err)
6384 goto done;
6386 err = delete_rebase_refs(worktree, repo);
6387 if (err)
6388 goto done;
6390 err = get_fileindex_path(&fileindex_path, worktree);
6391 if (err)
6392 goto done;
6394 rfa.worktree = worktree;
6395 rfa.fileindex = fileindex;
6396 rfa.progress_cb = progress_cb;
6397 rfa.progress_arg = progress_arg;
6398 rfa.patch_cb = NULL;
6399 rfa.patch_arg = NULL;
6400 rfa.repo = repo;
6401 err = worktree_status(worktree, "", fileindex, repo,
6402 revert_file, &rfa, NULL, NULL, 0, 0);
6403 if (err)
6404 goto sync;
6406 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6407 repo, progress_cb, progress_arg, NULL, NULL);
6408 sync:
6409 sync_err = sync_fileindex(fileindex, fileindex_path);
6410 if (sync_err && err == NULL)
6411 err = sync_err;
6412 done:
6413 got_ref_close(resolved);
6414 free(tree_id);
6415 free(commit_id);
6416 if (fileindex)
6417 got_fileindex_free(fileindex);
6418 free(fileindex_path);
6420 unlockerr = lock_worktree(worktree, LOCK_SH);
6421 if (unlockerr && err == NULL)
6422 err = unlockerr;
6423 return err;
6426 const struct got_error *
6427 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6428 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6429 struct got_fileindex **fileindex, struct got_worktree *worktree,
6430 struct got_repository *repo)
6432 const struct got_error *err = NULL;
6433 char *tmp_branch_name = NULL;
6434 char *branch_ref_name = NULL;
6435 char *base_commit_ref_name = NULL;
6436 char *fileindex_path = NULL;
6437 struct check_rebase_ok_arg ok_arg;
6438 struct got_reference *wt_branch = NULL;
6439 struct got_reference *base_commit_ref = NULL;
6441 *tmp_branch = NULL;
6442 *branch_ref = NULL;
6443 *base_commit_id = NULL;
6444 *fileindex = NULL;
6446 err = lock_worktree(worktree, LOCK_EX);
6447 if (err)
6448 return err;
6450 err = open_fileindex(fileindex, &fileindex_path, worktree);
6451 if (err)
6452 goto done;
6454 ok_arg.worktree = worktree;
6455 ok_arg.repo = repo;
6456 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6457 &ok_arg);
6458 if (err)
6459 goto done;
6461 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6462 if (err)
6463 goto done;
6465 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6466 if (err)
6467 goto done;
6469 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6470 worktree);
6471 if (err)
6472 goto done;
6474 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6475 0);
6476 if (err)
6477 goto done;
6479 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6480 if (err)
6481 goto done;
6483 err = got_ref_write(*branch_ref, repo);
6484 if (err)
6485 goto done;
6487 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6488 worktree->base_commit_id);
6489 if (err)
6490 goto done;
6491 err = got_ref_write(base_commit_ref, repo);
6492 if (err)
6493 goto done;
6494 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6495 if (*base_commit_id == NULL) {
6496 err = got_error_from_errno("got_object_id_dup");
6497 goto done;
6500 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6501 worktree->base_commit_id);
6502 if (err)
6503 goto done;
6504 err = got_ref_write(*tmp_branch, repo);
6505 if (err)
6506 goto done;
6508 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6509 if (err)
6510 goto done;
6511 done:
6512 free(fileindex_path);
6513 free(tmp_branch_name);
6514 free(branch_ref_name);
6515 free(base_commit_ref_name);
6516 if (wt_branch)
6517 got_ref_close(wt_branch);
6518 if (err) {
6519 if (*branch_ref) {
6520 got_ref_close(*branch_ref);
6521 *branch_ref = NULL;
6523 if (*tmp_branch) {
6524 got_ref_close(*tmp_branch);
6525 *tmp_branch = NULL;
6527 free(*base_commit_id);
6528 if (*fileindex) {
6529 got_fileindex_free(*fileindex);
6530 *fileindex = NULL;
6532 lock_worktree(worktree, LOCK_SH);
6534 return err;
6537 const struct got_error *
6538 got_worktree_histedit_postpone(struct got_worktree *worktree,
6539 struct got_fileindex *fileindex)
6541 if (fileindex)
6542 got_fileindex_free(fileindex);
6543 return lock_worktree(worktree, LOCK_SH);
6546 const struct got_error *
6547 got_worktree_histedit_in_progress(int *in_progress,
6548 struct got_worktree *worktree)
6550 const struct got_error *err;
6551 char *tmp_branch_name = NULL;
6553 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6554 if (err)
6555 return err;
6557 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6558 free(tmp_branch_name);
6559 return NULL;
6562 const struct got_error *
6563 got_worktree_histedit_continue(struct got_object_id **commit_id,
6564 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6565 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6566 struct got_worktree *worktree, struct got_repository *repo)
6568 const struct got_error *err;
6569 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6570 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6571 struct got_reference *commit_ref = NULL;
6572 struct got_reference *base_commit_ref = NULL;
6573 char *fileindex_path = NULL;
6574 int have_staged_files = 0;
6576 *commit_id = NULL;
6577 *tmp_branch = NULL;
6578 *base_commit_id = NULL;
6579 *fileindex = NULL;
6581 err = lock_worktree(worktree, LOCK_EX);
6582 if (err)
6583 return err;
6585 err = open_fileindex(fileindex, &fileindex_path, worktree);
6586 if (err)
6587 goto done;
6589 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6590 &have_staged_files);
6591 if (err && err->code != GOT_ERR_CANCELLED)
6592 goto done;
6593 if (have_staged_files) {
6594 err = got_error(GOT_ERR_STAGED_PATHS);
6595 goto done;
6598 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6599 if (err)
6600 goto done;
6602 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6603 if (err)
6604 goto done;
6606 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6607 if (err)
6608 goto done;
6610 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6611 worktree);
6612 if (err)
6613 goto done;
6615 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6616 if (err)
6617 goto done;
6619 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6620 if (err)
6621 goto done;
6622 err = got_ref_resolve(commit_id, repo, commit_ref);
6623 if (err)
6624 goto done;
6626 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6627 if (err)
6628 goto done;
6629 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6630 if (err)
6631 goto done;
6633 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6634 if (err)
6635 goto done;
6636 done:
6637 free(commit_ref_name);
6638 free(branch_ref_name);
6639 free(fileindex_path);
6640 if (commit_ref)
6641 got_ref_close(commit_ref);
6642 if (base_commit_ref)
6643 got_ref_close(base_commit_ref);
6644 if (err) {
6645 free(*commit_id);
6646 *commit_id = NULL;
6647 free(*base_commit_id);
6648 *base_commit_id = NULL;
6649 if (*tmp_branch) {
6650 got_ref_close(*tmp_branch);
6651 *tmp_branch = NULL;
6653 if (*fileindex) {
6654 got_fileindex_free(*fileindex);
6655 *fileindex = NULL;
6657 lock_worktree(worktree, LOCK_EX);
6659 return err;
6662 static const struct got_error *
6663 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6665 const struct got_error *err;
6666 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6667 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6669 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6670 if (err)
6671 goto done;
6672 err = delete_ref(tmp_branch_name, repo);
6673 if (err)
6674 goto done;
6676 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6677 worktree);
6678 if (err)
6679 goto done;
6680 err = delete_ref(base_commit_ref_name, repo);
6681 if (err)
6682 goto done;
6684 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6685 if (err)
6686 goto done;
6687 err = delete_ref(branch_ref_name, repo);
6688 if (err)
6689 goto done;
6691 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6692 if (err)
6693 goto done;
6694 err = delete_ref(commit_ref_name, repo);
6695 if (err)
6696 goto done;
6697 done:
6698 free(tmp_branch_name);
6699 free(base_commit_ref_name);
6700 free(branch_ref_name);
6701 free(commit_ref_name);
6702 return err;
6705 const struct got_error *
6706 got_worktree_histedit_abort(struct got_worktree *worktree,
6707 struct got_fileindex *fileindex, struct got_repository *repo,
6708 struct got_reference *branch, struct got_object_id *base_commit_id,
6709 got_worktree_checkout_cb progress_cb, void *progress_arg)
6711 const struct got_error *err, *unlockerr, *sync_err;
6712 struct got_reference *resolved = NULL;
6713 char *fileindex_path = NULL;
6714 struct got_object_id *tree_id = NULL;
6715 struct revert_file_args rfa;
6717 err = lock_worktree(worktree, LOCK_EX);
6718 if (err)
6719 return err;
6721 err = got_ref_open(&resolved, repo,
6722 got_ref_get_symref_target(branch), 0);
6723 if (err)
6724 goto done;
6726 err = got_worktree_set_head_ref(worktree, resolved);
6727 if (err)
6728 goto done;
6730 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6731 if (err)
6732 goto done;
6734 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6735 worktree->path_prefix);
6736 if (err)
6737 goto done;
6739 err = delete_histedit_refs(worktree, repo);
6740 if (err)
6741 goto done;
6743 err = get_fileindex_path(&fileindex_path, worktree);
6744 if (err)
6745 goto done;
6747 rfa.worktree = worktree;
6748 rfa.fileindex = fileindex;
6749 rfa.progress_cb = progress_cb;
6750 rfa.progress_arg = progress_arg;
6751 rfa.patch_cb = NULL;
6752 rfa.patch_arg = NULL;
6753 rfa.repo = repo;
6754 err = worktree_status(worktree, "", fileindex, repo,
6755 revert_file, &rfa, NULL, NULL, 0, 0);
6756 if (err)
6757 goto sync;
6759 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6760 repo, progress_cb, progress_arg, NULL, NULL);
6761 sync:
6762 sync_err = sync_fileindex(fileindex, fileindex_path);
6763 if (sync_err && err == NULL)
6764 err = sync_err;
6765 done:
6766 got_ref_close(resolved);
6767 free(tree_id);
6768 free(fileindex_path);
6770 unlockerr = lock_worktree(worktree, LOCK_SH);
6771 if (unlockerr && err == NULL)
6772 err = unlockerr;
6773 return err;
6776 const struct got_error *
6777 got_worktree_histedit_complete(struct got_worktree *worktree,
6778 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6779 struct got_reference *edited_branch, struct got_repository *repo)
6781 const struct got_error *err, *unlockerr;
6782 struct got_object_id *new_head_commit_id = NULL;
6783 struct got_reference *resolved = NULL;
6785 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6786 if (err)
6787 return err;
6789 err = got_ref_open(&resolved, repo,
6790 got_ref_get_symref_target(edited_branch), 0);
6791 if (err)
6792 goto done;
6794 err = got_ref_change_ref(resolved, new_head_commit_id);
6795 if (err)
6796 goto done;
6798 err = got_ref_write(resolved, repo);
6799 if (err)
6800 goto done;
6802 err = got_worktree_set_head_ref(worktree, resolved);
6803 if (err)
6804 goto done;
6806 err = delete_histedit_refs(worktree, repo);
6807 done:
6808 if (fileindex)
6809 got_fileindex_free(fileindex);
6810 free(new_head_commit_id);
6811 unlockerr = lock_worktree(worktree, LOCK_SH);
6812 if (unlockerr && err == NULL)
6813 err = unlockerr;
6814 return err;
6817 const struct got_error *
6818 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6819 struct got_object_id *commit_id, struct got_repository *repo)
6821 const struct got_error *err;
6822 char *commit_ref_name;
6824 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6825 if (err)
6826 return err;
6828 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6829 if (err)
6830 goto done;
6832 err = delete_ref(commit_ref_name, repo);
6833 done:
6834 free(commit_ref_name);
6835 return err;
6838 const struct got_error *
6839 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6840 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6841 struct got_worktree *worktree, const char *refname,
6842 struct got_repository *repo)
6844 const struct got_error *err = NULL;
6845 char *fileindex_path = NULL;
6846 struct check_rebase_ok_arg ok_arg;
6848 *fileindex = NULL;
6849 *branch_ref = NULL;
6850 *base_branch_ref = NULL;
6852 err = lock_worktree(worktree, LOCK_EX);
6853 if (err)
6854 return err;
6856 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6857 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6858 "cannot integrate a branch into itself; "
6859 "update -b or different branch name required");
6860 goto done;
6863 err = open_fileindex(fileindex, &fileindex_path, worktree);
6864 if (err)
6865 goto done;
6867 /* Preconditions are the same as for rebase. */
6868 ok_arg.worktree = worktree;
6869 ok_arg.repo = repo;
6870 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6871 &ok_arg);
6872 if (err)
6873 goto done;
6875 err = got_ref_open(branch_ref, repo, refname, 1);
6876 if (err)
6877 goto done;
6879 err = got_ref_open(base_branch_ref, repo,
6880 got_worktree_get_head_ref_name(worktree), 1);
6881 done:
6882 if (err) {
6883 if (*branch_ref) {
6884 got_ref_close(*branch_ref);
6885 *branch_ref = NULL;
6887 if (*base_branch_ref) {
6888 got_ref_close(*base_branch_ref);
6889 *base_branch_ref = NULL;
6891 if (*fileindex) {
6892 got_fileindex_free(*fileindex);
6893 *fileindex = NULL;
6895 lock_worktree(worktree, LOCK_SH);
6897 return err;
6900 const struct got_error *
6901 got_worktree_integrate_continue(struct got_worktree *worktree,
6902 struct got_fileindex *fileindex, struct got_repository *repo,
6903 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6904 got_worktree_checkout_cb progress_cb, void *progress_arg,
6905 got_cancel_cb cancel_cb, void *cancel_arg)
6907 const struct got_error *err = NULL, *sync_err, *unlockerr;
6908 char *fileindex_path = NULL;
6909 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6911 err = get_fileindex_path(&fileindex_path, worktree);
6912 if (err)
6913 goto done;
6915 err = got_ref_resolve(&commit_id, repo, branch_ref);
6916 if (err)
6917 goto done;
6919 err = got_object_id_by_path(&tree_id, repo, commit_id,
6920 worktree->path_prefix);
6921 if (err)
6922 goto done;
6924 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6925 if (err)
6926 goto done;
6928 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6929 progress_cb, progress_arg, cancel_cb, cancel_arg);
6930 if (err)
6931 goto sync;
6933 err = got_ref_change_ref(base_branch_ref, commit_id);
6934 if (err)
6935 goto sync;
6937 err = got_ref_write(base_branch_ref, repo);
6938 sync:
6939 sync_err = sync_fileindex(fileindex, fileindex_path);
6940 if (sync_err && err == NULL)
6941 err = sync_err;
6943 done:
6944 unlockerr = got_ref_unlock(branch_ref);
6945 if (unlockerr && err == NULL)
6946 err = unlockerr;
6947 got_ref_close(branch_ref);
6949 unlockerr = got_ref_unlock(base_branch_ref);
6950 if (unlockerr && err == NULL)
6951 err = unlockerr;
6952 got_ref_close(base_branch_ref);
6954 got_fileindex_free(fileindex);
6955 free(fileindex_path);
6956 free(tree_id);
6958 unlockerr = lock_worktree(worktree, LOCK_SH);
6959 if (unlockerr && err == NULL)
6960 err = unlockerr;
6961 return err;
6964 const struct got_error *
6965 got_worktree_integrate_abort(struct got_worktree *worktree,
6966 struct got_fileindex *fileindex, struct got_repository *repo,
6967 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6969 const struct got_error *err = NULL, *unlockerr = NULL;
6971 got_fileindex_free(fileindex);
6973 err = lock_worktree(worktree, LOCK_SH);
6975 unlockerr = got_ref_unlock(branch_ref);
6976 if (unlockerr && err == NULL)
6977 err = unlockerr;
6978 got_ref_close(branch_ref);
6980 unlockerr = got_ref_unlock(base_branch_ref);
6981 if (unlockerr && err == NULL)
6982 err = unlockerr;
6983 got_ref_close(base_branch_ref);
6985 return err;
6988 struct check_stage_ok_arg {
6989 struct got_object_id *head_commit_id;
6990 struct got_worktree *worktree;
6991 struct got_fileindex *fileindex;
6992 struct got_repository *repo;
6993 int have_changes;
6996 const struct got_error *
6997 check_stage_ok(void *arg, unsigned char status,
6998 unsigned char staged_status, const char *relpath,
6999 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7000 struct got_object_id *commit_id, int dirfd, const char *de_name)
7002 struct check_stage_ok_arg *a = arg;
7003 const struct got_error *err = NULL;
7004 struct got_fileindex_entry *ie;
7005 struct got_object_id base_commit_id;
7006 struct got_object_id *base_commit_idp = NULL;
7007 char *in_repo_path = NULL, *p;
7009 if (status == GOT_STATUS_UNVERSIONED ||
7010 status == GOT_STATUS_NO_CHANGE)
7011 return NULL;
7012 if (status == GOT_STATUS_NONEXISTENT)
7013 return got_error_set_errno(ENOENT, relpath);
7015 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7016 if (ie == NULL)
7017 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7019 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7020 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7021 relpath) == -1)
7022 return got_error_from_errno("asprintf");
7024 if (got_fileindex_entry_has_commit(ie)) {
7025 memcpy(base_commit_id.sha1, ie->commit_sha1,
7026 SHA1_DIGEST_LENGTH);
7027 base_commit_idp = &base_commit_id;
7030 if (status == GOT_STATUS_CONFLICT) {
7031 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7032 goto done;
7033 } else if (status != GOT_STATUS_ADD &&
7034 status != GOT_STATUS_MODIFY &&
7035 status != GOT_STATUS_DELETE) {
7036 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7037 goto done;
7040 a->have_changes = 1;
7042 p = in_repo_path;
7043 while (p[0] == '/')
7044 p++;
7045 err = check_out_of_date(p, status, staged_status,
7046 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7047 GOT_ERR_STAGE_OUT_OF_DATE);
7048 done:
7049 free(in_repo_path);
7050 return err;
7053 struct stage_path_arg {
7054 struct got_worktree *worktree;
7055 struct got_fileindex *fileindex;
7056 struct got_repository *repo;
7057 got_worktree_status_cb status_cb;
7058 void *status_arg;
7059 got_worktree_patch_cb patch_cb;
7060 void *patch_arg;
7061 int staged_something;
7062 int allow_bad_symlinks;
7065 static const struct got_error *
7066 stage_path(void *arg, unsigned char status,
7067 unsigned char staged_status, const char *relpath,
7068 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7069 struct got_object_id *commit_id, int dirfd, const char *de_name)
7071 struct stage_path_arg *a = arg;
7072 const struct got_error *err = NULL;
7073 struct got_fileindex_entry *ie;
7074 char *ondisk_path = NULL, *path_content = NULL;
7075 uint32_t stage;
7076 struct got_object_id *new_staged_blob_id = NULL;
7077 struct stat sb;
7079 if (status == GOT_STATUS_UNVERSIONED)
7080 return NULL;
7082 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7083 if (ie == NULL)
7084 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7086 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7087 relpath)== -1)
7088 return got_error_from_errno("asprintf");
7090 switch (status) {
7091 case GOT_STATUS_ADD:
7092 case GOT_STATUS_MODIFY:
7093 /* XXX could sb.st_mode be passed in by our caller? */
7094 if (lstat(ondisk_path, &sb) == -1) {
7095 err = got_error_from_errno2("lstat", ondisk_path);
7096 break;
7098 if (a->patch_cb) {
7099 if (status == GOT_STATUS_ADD) {
7100 int choice = GOT_PATCH_CHOICE_NONE;
7101 err = (*a->patch_cb)(&choice, a->patch_arg,
7102 status, ie->path, NULL, 1, 1);
7103 if (err)
7104 break;
7105 if (choice != GOT_PATCH_CHOICE_YES)
7106 break;
7107 } else {
7108 err = create_patched_content(&path_content, 0,
7109 staged_blob_id ? staged_blob_id : blob_id,
7110 ondisk_path, dirfd, de_name, ie->path,
7111 a->repo, a->patch_cb, a->patch_arg);
7112 if (err || path_content == NULL)
7113 break;
7116 err = got_object_blob_create(&new_staged_blob_id,
7117 path_content ? path_content : ondisk_path, a->repo);
7118 if (err)
7119 break;
7120 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7121 SHA1_DIGEST_LENGTH);
7122 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7123 stage = GOT_FILEIDX_STAGE_ADD;
7124 else
7125 stage = GOT_FILEIDX_STAGE_MODIFY;
7126 got_fileindex_entry_stage_set(ie, stage);
7127 if (S_ISLNK(sb.st_mode)) {
7128 int is_bad_symlink = 0;
7129 if (!a->allow_bad_symlinks) {
7130 char target_path[PATH_MAX];
7131 ssize_t target_len;
7132 target_len = readlink(ondisk_path, target_path,
7133 sizeof(target_path));
7134 if (target_len == -1) {
7135 err = got_error_from_errno2("readlink",
7136 ondisk_path);
7137 break;
7139 err = is_bad_symlink_target(&is_bad_symlink,
7140 target_path, target_len, ondisk_path,
7141 a->worktree->root_path);
7142 if (err)
7143 break;
7144 if (is_bad_symlink) {
7145 err = got_error_path(ondisk_path,
7146 GOT_ERR_BAD_SYMLINK);
7147 break;
7150 if (is_bad_symlink)
7151 got_fileindex_entry_staged_filetype_set(ie,
7152 GOT_FILEIDX_MODE_BAD_SYMLINK);
7153 else
7154 got_fileindex_entry_staged_filetype_set(ie,
7155 GOT_FILEIDX_MODE_SYMLINK);
7156 } else {
7157 got_fileindex_entry_staged_filetype_set(ie,
7158 GOT_FILEIDX_MODE_REGULAR_FILE);
7160 a->staged_something = 1;
7161 if (a->status_cb == NULL)
7162 break;
7163 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7164 get_staged_status(ie), relpath, blob_id,
7165 new_staged_blob_id, NULL, dirfd, de_name);
7166 break;
7167 case GOT_STATUS_DELETE:
7168 if (staged_status == GOT_STATUS_DELETE)
7169 break;
7170 if (a->patch_cb) {
7171 int choice = GOT_PATCH_CHOICE_NONE;
7172 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7173 ie->path, NULL, 1, 1);
7174 if (err)
7175 break;
7176 if (choice == GOT_PATCH_CHOICE_NO)
7177 break;
7178 if (choice != GOT_PATCH_CHOICE_YES) {
7179 err = got_error(GOT_ERR_PATCH_CHOICE);
7180 break;
7183 stage = GOT_FILEIDX_STAGE_DELETE;
7184 got_fileindex_entry_stage_set(ie, stage);
7185 a->staged_something = 1;
7186 if (a->status_cb == NULL)
7187 break;
7188 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7189 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7190 de_name);
7191 break;
7192 case GOT_STATUS_NO_CHANGE:
7193 break;
7194 case GOT_STATUS_CONFLICT:
7195 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7196 break;
7197 case GOT_STATUS_NONEXISTENT:
7198 err = got_error_set_errno(ENOENT, relpath);
7199 break;
7200 default:
7201 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7202 break;
7205 if (path_content && unlink(path_content) == -1 && err == NULL)
7206 err = got_error_from_errno2("unlink", path_content);
7207 free(path_content);
7208 free(ondisk_path);
7209 free(new_staged_blob_id);
7210 return err;
7213 const struct got_error *
7214 got_worktree_stage(struct got_worktree *worktree,
7215 struct got_pathlist_head *paths,
7216 got_worktree_status_cb status_cb, void *status_arg,
7217 got_worktree_patch_cb patch_cb, void *patch_arg,
7218 int allow_bad_symlinks, struct got_repository *repo)
7220 const struct got_error *err = NULL, *sync_err, *unlockerr;
7221 struct got_pathlist_entry *pe;
7222 struct got_fileindex *fileindex = NULL;
7223 char *fileindex_path = NULL;
7224 struct got_reference *head_ref = NULL;
7225 struct got_object_id *head_commit_id = NULL;
7226 struct check_stage_ok_arg oka;
7227 struct stage_path_arg spa;
7229 err = lock_worktree(worktree, LOCK_EX);
7230 if (err)
7231 return err;
7233 err = got_ref_open(&head_ref, repo,
7234 got_worktree_get_head_ref_name(worktree), 0);
7235 if (err)
7236 goto done;
7237 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7238 if (err)
7239 goto done;
7240 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7241 if (err)
7242 goto done;
7244 /* Check pre-conditions before staging anything. */
7245 oka.head_commit_id = head_commit_id;
7246 oka.worktree = worktree;
7247 oka.fileindex = fileindex;
7248 oka.repo = repo;
7249 oka.have_changes = 0;
7250 TAILQ_FOREACH(pe, paths, entry) {
7251 err = worktree_status(worktree, pe->path, fileindex, repo,
7252 check_stage_ok, &oka, NULL, NULL, 0, 0);
7253 if (err)
7254 goto done;
7256 if (!oka.have_changes) {
7257 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7258 goto done;
7261 spa.worktree = worktree;
7262 spa.fileindex = fileindex;
7263 spa.repo = repo;
7264 spa.patch_cb = patch_cb;
7265 spa.patch_arg = patch_arg;
7266 spa.status_cb = status_cb;
7267 spa.status_arg = status_arg;
7268 spa.staged_something = 0;
7269 spa.allow_bad_symlinks = allow_bad_symlinks;
7270 TAILQ_FOREACH(pe, paths, entry) {
7271 err = worktree_status(worktree, pe->path, fileindex, repo,
7272 stage_path, &spa, NULL, NULL, 0, 0);
7273 if (err)
7274 goto done;
7276 if (!spa.staged_something) {
7277 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7278 goto done;
7281 sync_err = sync_fileindex(fileindex, fileindex_path);
7282 if (sync_err && err == NULL)
7283 err = sync_err;
7284 done:
7285 if (head_ref)
7286 got_ref_close(head_ref);
7287 free(head_commit_id);
7288 free(fileindex_path);
7289 if (fileindex)
7290 got_fileindex_free(fileindex);
7291 unlockerr = lock_worktree(worktree, LOCK_SH);
7292 if (unlockerr && err == NULL)
7293 err = unlockerr;
7294 return err;
7297 struct unstage_path_arg {
7298 struct got_worktree *worktree;
7299 struct got_fileindex *fileindex;
7300 struct got_repository *repo;
7301 got_worktree_checkout_cb progress_cb;
7302 void *progress_arg;
7303 got_worktree_patch_cb patch_cb;
7304 void *patch_arg;
7307 static const struct got_error *
7308 create_unstaged_content(char **path_unstaged_content,
7309 char **path_new_staged_content, struct got_object_id *blob_id,
7310 struct got_object_id *staged_blob_id, const char *relpath,
7311 struct got_repository *repo,
7312 got_worktree_patch_cb patch_cb, void *patch_arg)
7314 const struct got_error *err;
7315 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7316 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7317 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7318 struct stat sb1, sb2;
7319 struct got_diff_changes *changes = NULL;
7320 struct got_diff_state *ds = NULL;
7321 struct got_diff_args *args = NULL;
7322 struct got_diff_change *change;
7323 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7324 int have_content = 0, have_rejected_content = 0;
7326 *path_unstaged_content = NULL;
7327 *path_new_staged_content = NULL;
7329 err = got_object_id_str(&label1, blob_id);
7330 if (err)
7331 return err;
7332 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7333 if (err)
7334 goto done;
7336 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7337 if (err)
7338 goto done;
7340 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7341 if (err)
7342 goto done;
7344 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7345 if (err)
7346 goto done;
7348 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7349 if (err)
7350 goto done;
7352 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7353 if (err)
7354 goto done;
7356 if (stat(path1, &sb1) == -1) {
7357 err = got_error_from_errno2("stat", path1);
7358 goto done;
7361 if (stat(path2, &sb2) == -1) {
7362 err = got_error_from_errno2("stat", path2);
7363 goto done;
7366 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7367 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7368 if (err)
7369 goto done;
7371 err = got_opentemp_named(path_unstaged_content, &outfile,
7372 "got-unstaged-content");
7373 if (err)
7374 goto done;
7375 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7376 "got-new-staged-content");
7377 if (err)
7378 goto done;
7380 if (fseek(f1, 0L, SEEK_SET) == -1) {
7381 err = got_ferror(f1, GOT_ERR_IO);
7382 goto done;
7384 if (fseek(f2, 0L, SEEK_SET) == -1) {
7385 err = got_ferror(f2, GOT_ERR_IO);
7386 goto done;
7388 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7389 int choice;
7390 err = apply_or_reject_change(&choice, change, ++n,
7391 changes->nchanges, ds, args, diff_flags, relpath,
7392 f1, f2, &line_cur1, &line_cur2,
7393 outfile, rejectfile, patch_cb, patch_arg);
7394 if (err)
7395 goto done;
7396 if (choice == GOT_PATCH_CHOICE_YES)
7397 have_content = 1;
7398 else
7399 have_rejected_content = 1;
7400 if (choice == GOT_PATCH_CHOICE_QUIT)
7401 break;
7403 if (have_content || have_rejected_content)
7404 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7405 outfile, rejectfile);
7406 done:
7407 free(label1);
7408 if (blob)
7409 got_object_blob_close(blob);
7410 if (staged_blob)
7411 got_object_blob_close(staged_blob);
7412 if (f1 && fclose(f1) == EOF && err == NULL)
7413 err = got_error_from_errno2("fclose", path1);
7414 if (f2 && fclose(f2) == EOF && err == NULL)
7415 err = got_error_from_errno2("fclose", path2);
7416 if (outfile && fclose(outfile) == EOF && err == NULL)
7417 err = got_error_from_errno2("fclose", *path_unstaged_content);
7418 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7419 err = got_error_from_errno2("fclose", *path_new_staged_content);
7420 if (path1 && unlink(path1) == -1 && err == NULL)
7421 err = got_error_from_errno2("unlink", path1);
7422 if (path2 && unlink(path2) == -1 && err == NULL)
7423 err = got_error_from_errno2("unlink", path2);
7424 if (err || !have_content) {
7425 if (*path_unstaged_content &&
7426 unlink(*path_unstaged_content) == -1 && err == NULL)
7427 err = got_error_from_errno2("unlink",
7428 *path_unstaged_content);
7429 free(*path_unstaged_content);
7430 *path_unstaged_content = NULL;
7432 if (err || !have_content || !have_rejected_content) {
7433 if (*path_new_staged_content &&
7434 unlink(*path_new_staged_content) == -1 && err == NULL)
7435 err = got_error_from_errno2("unlink",
7436 *path_new_staged_content);
7437 free(*path_new_staged_content);
7438 *path_new_staged_content = NULL;
7440 free(args);
7441 if (ds) {
7442 got_diff_state_free(ds);
7443 free(ds);
7445 if (changes)
7446 got_diff_free_changes(changes);
7447 free(path1);
7448 free(path2);
7449 return err;
7452 static const struct got_error *
7453 unstage_hunks(struct got_object_id *staged_blob_id,
7454 struct got_blob_object *blob_base,
7455 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7456 const char *ondisk_path, const char *label_orig,
7457 struct got_worktree *worktree, struct got_repository *repo,
7458 got_worktree_patch_cb patch_cb, void *patch_arg,
7459 got_worktree_checkout_cb progress_cb, void *progress_arg)
7461 const struct got_error *err = NULL;
7462 char *path_unstaged_content = NULL;
7463 char *path_new_staged_content = NULL;
7464 struct got_object_id *new_staged_blob_id = NULL;
7465 FILE *f = NULL;
7466 struct stat sb;
7468 err = create_unstaged_content(&path_unstaged_content,
7469 &path_new_staged_content, blob_id, staged_blob_id,
7470 ie->path, repo, patch_cb, patch_arg);
7471 if (err)
7472 return err;
7474 if (path_unstaged_content == NULL)
7475 return NULL;
7477 if (path_new_staged_content) {
7478 err = got_object_blob_create(&new_staged_blob_id,
7479 path_new_staged_content, repo);
7480 if (err)
7481 goto done;
7484 f = fopen(path_unstaged_content, "r");
7485 if (f == NULL) {
7486 err = got_error_from_errno2("fopen",
7487 path_unstaged_content);
7488 goto done;
7490 if (fstat(fileno(f), &sb) == -1) {
7491 err = got_error_from_errno2("fstat", path_unstaged_content);
7492 goto done;
7494 if (got_fileindex_entry_staged_filetype_get(ie) ==
7495 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7496 char link_target[PATH_MAX];
7497 size_t r;
7498 r = fread(link_target, 1, sizeof(link_target), f);
7499 if (r == 0 && ferror(f)) {
7500 err = got_error_from_errno("fread");
7501 goto done;
7503 if (r >= sizeof(link_target)) { /* should not happen */
7504 err = got_error(GOT_ERR_NO_SPACE);
7505 goto done;
7507 link_target[r] = '\0';
7508 err = merge_symlink(worktree, blob_base,
7509 ondisk_path, ie->path, label_orig, link_target,
7510 worktree->base_commit_id, repo, progress_cb,
7511 progress_arg);
7512 } else {
7513 int local_changes_subsumed;
7514 err = merge_file(&local_changes_subsumed, worktree,
7515 blob_base, ondisk_path, ie->path,
7516 got_fileindex_perms_to_st(ie),
7517 path_unstaged_content, label_orig, "unstaged",
7518 repo, progress_cb, progress_arg);
7520 if (err)
7521 goto done;
7523 if (new_staged_blob_id) {
7524 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7525 SHA1_DIGEST_LENGTH);
7526 } else
7527 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7528 done:
7529 free(new_staged_blob_id);
7530 if (path_unstaged_content &&
7531 unlink(path_unstaged_content) == -1 && err == NULL)
7532 err = got_error_from_errno2("unlink", path_unstaged_content);
7533 if (path_new_staged_content &&
7534 unlink(path_new_staged_content) == -1 && err == NULL)
7535 err = got_error_from_errno2("unlink", path_new_staged_content);
7536 if (f && fclose(f) != 0 && err == NULL)
7537 err = got_error_from_errno2("fclose", path_unstaged_content);
7538 free(path_unstaged_content);
7539 free(path_new_staged_content);
7540 return err;
7543 static const struct got_error *
7544 unstage_path(void *arg, unsigned char status,
7545 unsigned char staged_status, const char *relpath,
7546 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7547 struct got_object_id *commit_id, int dirfd, const char *de_name)
7549 const struct got_error *err = NULL;
7550 struct unstage_path_arg *a = arg;
7551 struct got_fileindex_entry *ie;
7552 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7553 char *ondisk_path = NULL;
7554 char *id_str = NULL, *label_orig = NULL;
7555 int local_changes_subsumed;
7556 struct stat sb;
7558 if (staged_status != GOT_STATUS_ADD &&
7559 staged_status != GOT_STATUS_MODIFY &&
7560 staged_status != GOT_STATUS_DELETE)
7561 return NULL;
7563 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7564 if (ie == NULL)
7565 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7567 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7568 == -1)
7569 return got_error_from_errno("asprintf");
7571 err = got_object_id_str(&id_str,
7572 commit_id ? commit_id : a->worktree->base_commit_id);
7573 if (err)
7574 goto done;
7575 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7576 id_str) == -1) {
7577 err = got_error_from_errno("asprintf");
7578 goto done;
7581 switch (staged_status) {
7582 case GOT_STATUS_MODIFY:
7583 err = got_object_open_as_blob(&blob_base, a->repo,
7584 blob_id, 8192);
7585 if (err)
7586 break;
7587 /* fall through */
7588 case GOT_STATUS_ADD:
7589 if (a->patch_cb) {
7590 if (staged_status == GOT_STATUS_ADD) {
7591 int choice = GOT_PATCH_CHOICE_NONE;
7592 err = (*a->patch_cb)(&choice, a->patch_arg,
7593 staged_status, ie->path, NULL, 1, 1);
7594 if (err)
7595 break;
7596 if (choice != GOT_PATCH_CHOICE_YES)
7597 break;
7598 } else {
7599 err = unstage_hunks(staged_blob_id,
7600 blob_base, blob_id, ie, ondisk_path,
7601 label_orig, a->worktree, a->repo,
7602 a->patch_cb, a->patch_arg,
7603 a->progress_cb, a->progress_arg);
7604 break; /* Done with this file. */
7607 err = got_object_open_as_blob(&blob_staged, a->repo,
7608 staged_blob_id, 8192);
7609 if (err)
7610 break;
7611 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7612 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7613 case GOT_FILEIDX_MODE_REGULAR_FILE:
7614 err = merge_blob(&local_changes_subsumed, a->worktree,
7615 blob_base, ondisk_path, relpath,
7616 got_fileindex_perms_to_st(ie), label_orig,
7617 blob_staged, commit_id ? commit_id :
7618 a->worktree->base_commit_id, a->repo,
7619 a->progress_cb, a->progress_arg);
7620 break;
7621 case GOT_FILEIDX_MODE_SYMLINK:
7622 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7623 char *staged_target;
7624 err = got_object_blob_read_to_str(
7625 &staged_target, blob_staged);
7626 if (err)
7627 goto done;
7628 err = merge_symlink(a->worktree, blob_base,
7629 ondisk_path, relpath, label_orig,
7630 staged_target, commit_id ? commit_id :
7631 a->worktree->base_commit_id,
7632 a->repo, a->progress_cb, a->progress_arg);
7633 free(staged_target);
7634 } else {
7635 err = merge_blob(&local_changes_subsumed,
7636 a->worktree, blob_base, ondisk_path,
7637 relpath, got_fileindex_perms_to_st(ie),
7638 label_orig, blob_staged,
7639 commit_id ? commit_id :
7640 a->worktree->base_commit_id, a->repo,
7641 a->progress_cb, a->progress_arg);
7643 break;
7644 default:
7645 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7646 break;
7648 if (err == NULL)
7649 got_fileindex_entry_stage_set(ie,
7650 GOT_FILEIDX_STAGE_NONE);
7651 break;
7652 case GOT_STATUS_DELETE:
7653 if (a->patch_cb) {
7654 int choice = GOT_PATCH_CHOICE_NONE;
7655 err = (*a->patch_cb)(&choice, a->patch_arg,
7656 staged_status, ie->path, NULL, 1, 1);
7657 if (err)
7658 break;
7659 if (choice == GOT_PATCH_CHOICE_NO)
7660 break;
7661 if (choice != GOT_PATCH_CHOICE_YES) {
7662 err = got_error(GOT_ERR_PATCH_CHOICE);
7663 break;
7666 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7667 err = get_file_status(&status, &sb, ie, ondisk_path,
7668 dirfd, de_name, a->repo);
7669 if (err)
7670 break;
7671 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7672 break;
7674 done:
7675 free(ondisk_path);
7676 if (blob_base)
7677 got_object_blob_close(blob_base);
7678 if (blob_staged)
7679 got_object_blob_close(blob_staged);
7680 free(id_str);
7681 free(label_orig);
7682 return err;
7685 const struct got_error *
7686 got_worktree_unstage(struct got_worktree *worktree,
7687 struct got_pathlist_head *paths,
7688 got_worktree_checkout_cb progress_cb, void *progress_arg,
7689 got_worktree_patch_cb patch_cb, void *patch_arg,
7690 struct got_repository *repo)
7692 const struct got_error *err = NULL, *sync_err, *unlockerr;
7693 struct got_pathlist_entry *pe;
7694 struct got_fileindex *fileindex = NULL;
7695 char *fileindex_path = NULL;
7696 struct unstage_path_arg upa;
7698 err = lock_worktree(worktree, LOCK_EX);
7699 if (err)
7700 return err;
7702 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7703 if (err)
7704 goto done;
7706 upa.worktree = worktree;
7707 upa.fileindex = fileindex;
7708 upa.repo = repo;
7709 upa.progress_cb = progress_cb;
7710 upa.progress_arg = progress_arg;
7711 upa.patch_cb = patch_cb;
7712 upa.patch_arg = patch_arg;
7713 TAILQ_FOREACH(pe, paths, entry) {
7714 err = worktree_status(worktree, pe->path, fileindex, repo,
7715 unstage_path, &upa, NULL, NULL, 0, 0);
7716 if (err)
7717 goto done;
7720 sync_err = sync_fileindex(fileindex, fileindex_path);
7721 if (sync_err && err == NULL)
7722 err = sync_err;
7723 done:
7724 free(fileindex_path);
7725 if (fileindex)
7726 got_fileindex_free(fileindex);
7727 unlockerr = lock_worktree(worktree, LOCK_SH);
7728 if (unlockerr && err == NULL)
7729 err = unlockerr;
7730 return err;
7733 struct report_file_info_arg {
7734 struct got_worktree *worktree;
7735 got_worktree_path_info_cb info_cb;
7736 void *info_arg;
7737 struct got_pathlist_head *paths;
7738 got_cancel_cb cancel_cb;
7739 void *cancel_arg;
7742 static const struct got_error *
7743 report_file_info(void *arg, struct got_fileindex_entry *ie)
7745 struct report_file_info_arg *a = arg;
7746 struct got_pathlist_entry *pe;
7747 struct got_object_id blob_id, staged_blob_id, commit_id;
7748 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7749 struct got_object_id *commit_idp = NULL;
7750 int stage;
7752 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7753 return got_error(GOT_ERR_CANCELLED);
7755 TAILQ_FOREACH(pe, a->paths, entry) {
7756 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7757 got_path_is_child(ie->path, pe->path, pe->path_len))
7758 break;
7760 if (pe == NULL) /* not found */
7761 return NULL;
7763 if (got_fileindex_entry_has_blob(ie)) {
7764 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7765 blob_idp = &blob_id;
7767 stage = got_fileindex_entry_stage_get(ie);
7768 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7769 stage == GOT_FILEIDX_STAGE_ADD) {
7770 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7771 SHA1_DIGEST_LENGTH);
7772 staged_blob_idp = &staged_blob_id;
7775 if (got_fileindex_entry_has_commit(ie)) {
7776 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7777 commit_idp = &commit_id;
7780 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7781 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7784 const struct got_error *
7785 got_worktree_path_info(struct got_worktree *worktree,
7786 struct got_pathlist_head *paths,
7787 got_worktree_path_info_cb info_cb, void *info_arg,
7788 got_cancel_cb cancel_cb, void *cancel_arg)
7791 const struct got_error *err = NULL, *unlockerr;
7792 struct got_fileindex *fileindex = NULL;
7793 char *fileindex_path = NULL;
7794 struct report_file_info_arg arg;
7796 err = lock_worktree(worktree, LOCK_SH);
7797 if (err)
7798 return err;
7800 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7801 if (err)
7802 goto done;
7804 arg.worktree = worktree;
7805 arg.info_cb = info_cb;
7806 arg.info_arg = info_arg;
7807 arg.paths = paths;
7808 arg.cancel_cb = cancel_cb;
7809 arg.cancel_arg = cancel_arg;
7810 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7811 &arg);
7812 done:
7813 free(fileindex_path);
7814 if (fileindex)
7815 got_fileindex_free(fileindex);
7816 unlockerr = lock_worktree(worktree, LOCK_UN);
7817 if (unlockerr && err == NULL)
7818 err = unlockerr;
7819 return err;