checkout: introduce git_checkout_tree()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
diff --git a/include/git2/checkout.h b/include/git2/checkout.h
index deb8287..21b68e3 100644
--- a/include/git2/checkout.h
+++ b/include/git2/checkout.h
@@ -32,10 +32,16 @@ typedef struct git_checkout_opts {
int dir_mode; /* default is 0755 */
int file_mode; /* default is 0644 */
int file_open_flags; /* default is O_CREAT | O_TRUNC | O_WRONLY */
+
+ /* when not NULL, arrays of fnmatch pattern specifying
+ * which paths should be taken into account
+ */
+ git_strarray *paths;
} git_checkout_opts;
/**
- * Updates files in the working tree to match the commit pointed to by HEAD.
+ * Updates files in the index and the working tree to match the content of the
+ * commit pointed at by HEAD.
*
* @param repo repository to check out (must be non-bare)
* @param opts specifies checkout options (may be NULL)
@@ -49,7 +55,9 @@ GIT_EXTERN(int) git_checkout_head(
git_indexer_stats *stats);
/**
- * Updates files in the working tree to match a commit pointed to by a ref.
+ * Updates files in the index and the working tree to match the content of the
+ * commit pointed at by the reference.
+ *
*
* @param ref reference to follow to a commit
* @param opts specifies checkout options (may be NULL)
@@ -62,6 +70,23 @@ GIT_EXTERN(int) git_checkout_reference(
git_checkout_opts *opts,
git_indexer_stats *stats);
+/**
+ * Updates files in the index and working tree to match the content of the
+ * tree pointed at by the treeish.
+ *
+ * @param repo repository to check out (must be non-bare)
+ * @param treeish a commit, tag or tree which content will be used to update
+ * the working directory
+ * @param opts specifies checkout options (may be NULL)
+ * @param stats structure through which progress information is reported
+ * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information
+ * about the error)
+ */
+GIT_EXTERN(int) git_checkout_tree(
+ git_repository *repo,
+ git_object *treeish,
+ git_checkout_opts *opts,
+ git_indexer_stats *stats);
/** @} */
GIT_END_DECL
diff --git a/src/checkout.c b/src/checkout.c
index d1720fc..663a362 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -11,9 +11,9 @@
#include "git2/repository.h"
#include "git2/refs.h"
#include "git2/tree.h"
-#include "git2/commit.h"
#include "git2/blob.h"
#include "git2/config.h"
+#include "git2/diff.h"
#include "common.h"
#include "refs.h"
@@ -22,204 +22,321 @@
#include "filter.h"
#include "blob.h"
-typedef struct tree_walk_data
+struct checkout_diff_data
{
+ git_buf *path;
+ int workdir_len;
+ git_checkout_opts *checkout_opts;
git_indexer_stats *stats;
- git_checkout_opts *opts;
- git_repository *repo;
- git_odb *odb;
- bool no_symlinks;
-} tree_walk_data;
+ git_repository *owner;
+ bool can_symlink;
+};
+
+static int buffer_to_file(
+ git_buf *buffer,
+ const char *path,
+ int dir_mode,
+ int file_open_flags,
+ mode_t file_mode)
+{
+ int fd, error_write, error_close;
+ if (git_futils_mkpath2file(path, dir_mode) < 0)
+ return -1;
-static int blob_contents_to_link(tree_walk_data *data, git_buf *fnbuf,
- const git_oid *id)
-{
- int retcode = GIT_ERROR;
- git_blob *blob;
+ if ((fd = p_open(path, file_open_flags, file_mode)) < 0)
+ return -1;
- /* Get the link target */
- if (!(retcode = git_blob_lookup(&blob, data->repo, id))) {
- git_buf linktarget = GIT_BUF_INIT;
- if (!(retcode = git_blob__getbuf(&linktarget, blob))) {
- /* Create the link */
- const char *new = git_buf_cstr(&linktarget),
- *old = git_buf_cstr(fnbuf);
- retcode = data->no_symlinks
- ? git_futils_fake_symlink(new, old)
- : p_symlink(new, old);
- }
- git_buf_free(&linktarget);
- git_blob_free(blob);
- }
+ error_write = p_write(fd, git_buf_cstr(buffer), git_buf_len(buffer));
+ error_close = p_close(fd);
- return retcode;
+ return error_write ? error_write : error_close;
}
-
-static int blob_contents_to_file(git_repository *repo, git_buf *fnbuf,
- const git_tree_entry *entry, tree_walk_data *data)
+static int blob_content_to_file(
+ git_blob *blob,
+ const char *path,
+ unsigned int entry_filemode,
+ git_checkout_opts *opts)
{
- int retcode = GIT_ERROR;
- int fd = -1;
- git_buf contents = GIT_BUF_INIT;
- const git_oid *id = git_tree_entry_id(entry);
- int file_mode = data->opts->file_mode;
-
- /* Deal with pre-existing files */
- if (git_path_exists(git_buf_cstr(fnbuf)) &&
- data->opts->existing_file_action == GIT_CHECKOUT_SKIP_EXISTING)
- return 0;
+ int retcode;
+ git_buf content = GIT_BUF_INIT;
+ int file_mode = opts->file_mode;
/* Allow disabling of filters */
- if (data->opts->disable_filters) {
- git_blob *blob;
- if (!(retcode = git_blob_lookup(&blob, repo, id))) {
- retcode = git_blob__getbuf(&contents, blob);
- git_blob_free(blob);
- }
- } else {
- retcode = git_filter_blob_contents(&contents, repo, id, git_buf_cstr(fnbuf));
- }
- if (retcode < 0) goto bctf_cleanup;
+ if (opts->disable_filters)
+ retcode = git_blob__getbuf(&content, blob);
+ else
+ retcode = git_filter_blob_content(&content, blob, path);
+
+ if (retcode < 0)
+ goto cleanup;
/* Allow overriding of file mode */
if (!file_mode)
- file_mode = git_tree_entry_filemode(entry);
+ file_mode = entry_filemode;
- if ((retcode = git_futils_mkpath2file(git_buf_cstr(fnbuf), data->opts->dir_mode)) < 0)
- goto bctf_cleanup;
+ retcode = buffer_to_file(&content, path, opts->dir_mode, opts->file_open_flags, file_mode);
- fd = p_open(git_buf_cstr(fnbuf), data->opts->file_open_flags, file_mode);
- if (fd < 0) goto bctf_cleanup;
+cleanup:
+ git_buf_free(&content);
+ return retcode;
+}
+
+static int blob_content_to_link(git_blob *blob, const char *path, bool can_symlink)
+{
+ git_buf linktarget = GIT_BUF_INIT;
+ int error;
- if (!p_write(fd, git_buf_cstr(&contents), git_buf_len(&contents)))
- retcode = 0;
+ if (git_blob__getbuf(&linktarget, blob) < 0)
+ return -1;
+
+ if (can_symlink)
+ error = p_symlink(git_buf_cstr(&linktarget), path);
else
- retcode = GIT_ERROR;
- p_close(fd);
+ error = git_futils_fake_symlink(git_buf_cstr(&linktarget), path);
-bctf_cleanup:
- git_buf_free(&contents);
- return retcode;
+ git_buf_free(&linktarget);
+
+ return error;
}
-static int checkout_walker(const char *path, const git_tree_entry *entry, void *payload)
+static int checkout_blob(
+ git_repository *repo,
+ git_oid *blob_oid,
+ const char *path,
+ unsigned int filemode,
+ bool can_symlink,
+ git_checkout_opts *opts)
{
- int retcode = 0;
- tree_walk_data *data = (tree_walk_data*)payload;
- int attr = git_tree_entry_filemode(entry);
- git_buf fnbuf = GIT_BUF_INIT;
- git_buf_join_n(&fnbuf, '/', 3,
- git_repository_workdir(data->repo),
- path,
- git_tree_entry_name(entry));
-
- switch(git_tree_entry_type(entry))
- {
- case GIT_OBJ_TREE:
- /* Nothing to do; the blob handling creates necessary directories. */
- break;
+ git_blob *blob;
+ int error;
- case GIT_OBJ_COMMIT:
- /* Submodule */
- git_futils_mkpath2file(git_buf_cstr(&fnbuf), data->opts->dir_mode);
- retcode = p_mkdir(git_buf_cstr(&fnbuf), data->opts->dir_mode);
+ if (git_blob_lookup(&blob, repo, blob_oid) < 0)
+ return -1; /* Add an error message */
+
+ if (S_ISLNK(filemode))
+ error = blob_content_to_link(blob, path, can_symlink);
+ else
+ error = blob_content_to_file(blob, path, filemode, opts);
+
+ git_blob_free(blob);
+
+ return error;
+}
+
+static int checkout_diff_fn(
+ void *cb_data,
+ git_diff_delta *delta,
+ float progress)
+{
+ struct checkout_diff_data *data;
+ int error = -1;
+
+ data = (struct checkout_diff_data *)cb_data;
+
+ data->stats->processed = (unsigned int)(data->stats->total * progress);
+
+ git_buf_truncate(data->path, data->workdir_len);
+ if (git_buf_joinpath(data->path, git_buf_cstr(data->path), delta->new_file.path) < 0)
+ return -1;
+
+ switch (delta->status) {
+ case GIT_DELTA_UNTRACKED:
+ if (!git__suffixcmp(delta->new_file.path, "/"))
+ error = git_futils_rmdir_r(git_buf_cstr(data->path), GIT_DIRREMOVAL_FILES_AND_DIRS);
+ else
+ error = p_unlink(git_buf_cstr(data->path));
break;
- case GIT_OBJ_BLOB:
- if (S_ISLNK(attr)) {
- retcode = blob_contents_to_link(data, &fnbuf,
- git_tree_entry_id(entry));
- } else {
- retcode = blob_contents_to_file(data->repo, &fnbuf, entry, data);
- }
+ case GIT_DELTA_MODIFIED:
+ /* Deal with pre-existing files */
+ if (data->checkout_opts->existing_file_action == GIT_CHECKOUT_SKIP_EXISTING)
+ return 0;
+
+ case GIT_DELTA_DELETED:
+ if (checkout_blob(
+ data->owner,
+ &delta->old_file.oid,
+ git_buf_cstr(data->path),
+ delta->old_file.mode,
+ data->can_symlink,
+ data->checkout_opts) < 0)
+ goto cleanup;
+
break;
default:
- retcode = -1;
- break;
+ giterr_set(GITERR_INVALID, "Unexpected status (%d) for path '%s'.", delta->status, delta->new_file.path);
+ goto cleanup;
}
- git_buf_free(&fnbuf);
- data->stats->processed++;
- return retcode;
-}
+ error = 0;
+cleanup:
+ return error;
+}
-int git_checkout_head(git_repository *repo, git_checkout_opts *opts, git_indexer_stats *stats)
+static int retrieve_symlink_capabilities(git_repository *repo, bool *can_symlink)
{
- int retcode = GIT_ERROR;
- git_indexer_stats dummy_stats;
- git_checkout_opts default_opts = {0};
- git_tree *tree;
- tree_walk_data payload;
git_config *cfg;
+ int error;
- assert(repo);
- if (!opts) opts = &default_opts;
- if (!stats) stats = &dummy_stats;
+ if (git_repository_config__weakptr(&cfg, repo) < 0)
+ return -1;
+
+ error = git_config_get_bool((int *)can_symlink, cfg, "core.symlinks");
+
+ /*
+ * When no "core.symlinks" entry is found in any of the configuration
+ * store (local, global or system), default value is "true".
+ */
+ if (error == GIT_ENOTFOUND) {
+ *can_symlink = true;
+ error = 0;
+ }
+
+ return error;
+}
+
+static void normalize_options(git_checkout_opts *normalized, git_checkout_opts *proposed)
+{
+ assert(normalized);
+
+ if (!proposed)
+ memset(normalized, 0, sizeof(git_checkout_opts));
+ else
+ memmove(normalized, proposed, sizeof(git_checkout_opts));
/* Default options */
- if (!opts->existing_file_action)
- opts->existing_file_action = GIT_CHECKOUT_OVERWRITE_EXISTING;
+ if (!normalized->existing_file_action)
+ normalized->existing_file_action = GIT_CHECKOUT_OVERWRITE_EXISTING;
+
/* opts->disable_filters is false by default */
- if (!opts->dir_mode) opts->dir_mode = GIT_DIR_MODE;
- if (!opts->file_open_flags)
- opts->file_open_flags = O_CREAT | O_TRUNC | O_WRONLY;
+ if (!normalized->dir_mode)
+ normalized->dir_mode = GIT_DIR_MODE;
+
+ if (!normalized->file_open_flags)
+ normalized->file_open_flags = O_CREAT | O_TRUNC | O_WRONLY;
+}
+
+int git_checkout_tree(
+ git_repository *repo,
+ git_object *treeish,
+ git_checkout_opts *opts,
+ git_indexer_stats *stats)
+{
+ git_index *index = NULL;
+ git_tree *tree = NULL;
+ git_diff_list *diff = NULL;
+ git_indexer_stats dummy_stats;
+
+ git_diff_options diff_opts = {0};
+ git_checkout_opts checkout_opts;
+
+ struct checkout_diff_data data;
+ git_buf workdir = GIT_BUF_INIT;
+
+ int error;
+
+ assert(repo && treeish);
- if (git_repository_is_bare(repo)) {
- giterr_set(GITERR_INVALID, "Checkout is not allowed for bare repositories");
+ if ((git_repository__ensure_not_bare(repo, "checkout")) < 0)
+ return GIT_EBAREREPO;
+
+ if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) {
+ giterr_set(GITERR_INVALID, "Provided treeish cannot be peeled into a tree.");
return GIT_ERROR;
}
- memset(&payload, 0, sizeof(payload));
+ if ((error = git_repository_index(&index, repo)) < 0)
+ goto cleanup;
- /* Determine if symlinks should be handled */
- if (!git_repository_config__weakptr(&cfg, repo)) {
- int temp = true;
- if (!git_config_get_bool(&temp, cfg, "core.symlinks")) {
- payload.no_symlinks = !temp;
- }
- }
+ if ((error = git_index_read_tree(index, tree, NULL)) < 0)
+ goto cleanup;
+
+ if ((error = git_index_write(index)) < 0)
+ goto cleanup;
- stats->total = stats->processed = 0;
- payload.stats = stats;
- payload.opts = opts;
- payload.repo = repo;
- if (git_repository_odb(&payload.odb, repo) < 0) return GIT_ERROR;
-
- if (!git_repository_head_tree(&tree, repo)) {
- git_index *idx;
- if (!(retcode = git_repository_index(&idx, repo))) {
- if (!(retcode = git_index_read_tree(idx, tree, stats))) {
- git_index_write(idx);
- retcode = git_tree_walk(tree, checkout_walker, GIT_TREEWALK_POST, &payload);
- }
- git_index_free(idx);
- }
- git_tree_free(tree);
+ diff_opts.flags = GIT_DIFF_INCLUDE_UNTRACKED;
+
+ if (opts && opts->paths) {
+ diff_opts.pathspec.strings = opts->paths->strings;
+ diff_opts.pathspec.count = opts->paths->count;
}
- git_odb_free(payload.odb);
- return retcode;
+ if ((error = git_diff_workdir_to_index(repo, &diff_opts, &diff)) < 0)
+ goto cleanup;
+
+ if ((error = git_buf_puts(&workdir, git_repository_workdir(repo))) < 0)
+ goto cleanup;
+
+ normalize_options(&checkout_opts, opts);
+
+ if (!stats)
+ stats = &dummy_stats;
+
+ stats->processed = 0;
+ stats->total = git_index_entrycount(index);
+
+ memset(&data, 0, sizeof(data));
+
+ data.path = &workdir;
+ data.workdir_len = git_buf_len(&workdir);
+ data.checkout_opts = &checkout_opts;
+ data.stats = stats;
+ data.owner = repo;
+
+ if ((error = retrieve_symlink_capabilities(repo, &data.can_symlink)) < 0)
+ goto cleanup;
+
+ error = git_diff_foreach(diff, &data, checkout_diff_fn, NULL, NULL);
+
+cleanup:
+ git_diff_list_free(diff);
+ git_index_free(index);
+ git_tree_free(tree);
+ git_buf_free(&workdir);
+ return error;
}
+int git_checkout_head(
+ git_repository *repo,
+ git_checkout_opts *opts,
+ git_indexer_stats *stats)
+{
+ int error;
+ git_tree *tree = NULL;
+
+ assert(repo);
+
+ if (git_repository_head_tree(&tree, repo) < 0)
+ return -1;
+
+ error = git_checkout_tree(repo, (git_object *)tree, opts, stats);
+
+ git_tree_free(tree);
-int git_checkout_reference(git_reference *ref,
- git_checkout_opts *opts,
- git_indexer_stats *stats)
+ return error;
+}
+
+int git_checkout_reference(
+ git_reference *ref,
+ git_checkout_opts *opts,
+ git_indexer_stats *stats)
{
git_repository *repo= git_reference_owner(ref);
git_reference *head = NULL;
- int retcode = GIT_ERROR;
+ int error;
- if ((retcode = git_reference_create_symbolic(&head, repo, GIT_HEAD_FILE,
- git_reference_name(ref), true)) < 0)
- return retcode;
+ if ((error = git_reference_create_symbolic(
+ &head, repo, GIT_HEAD_FILE, git_reference_name(ref), true)) < 0)
+ return error;
- retcode = git_checkout_head(git_reference_owner(ref), opts, stats);
+ error = git_checkout_head(git_reference_owner(ref), opts, stats);
git_reference_free(head);
- return retcode;
+ return error;
}
+
+
diff --git a/src/clone.c b/src/clone.c
index c4f6ec9..80a13d0 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -235,9 +235,8 @@ int git_clone(git_repository **out,
assert(out && origin_url && workdir_path);
- if (!(retcode = clone_internal(out, origin_url, workdir_path, fetch_stats, 0))) {
+ if (!(retcode = clone_internal(out, origin_url, workdir_path, fetch_stats, 0)))
retcode = git_checkout_head(*out, checkout_opts, checkout_stats);
- }
return retcode;
}
diff --git a/src/filter.c b/src/filter.c
index e9517a2..5b6bb28 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -165,37 +165,21 @@ int git_filters_apply(git_buf *dest, git_buf *source, git_vector *filters)
return 0;
}
-static int unfiltered_blob_contents(git_buf *out, git_repository *repo, const git_oid *blob_id)
+int git_filter_blob_content(git_buf *out, git_blob *blob, const char *hintpath)
{
- int retcode = GIT_ERROR;
- git_blob *blob;
-
- if (!(retcode = git_blob_lookup(&blob, repo, blob_id)))
- {
- retcode = git_blob__getbuf(out, blob);
- git_blob_free(blob);
- }
+ git_buf unfiltered = GIT_BUF_INIT;
+ git_vector filters = GIT_VECTOR_INIT;
+ int retcode;
- return retcode;
-}
+ retcode = git_blob__getbuf(&unfiltered, blob);
-int git_filter_blob_contents(git_buf *out, git_repository *repo, const git_oid *oid, const char *path)
-{
- int retcode = GIT_ERROR;
+ git_buf_clear(out);
- git_buf unfiltered = GIT_BUF_INIT;
- if (!unfiltered_blob_contents(&unfiltered, repo, oid)) {
- git_vector filters = GIT_VECTOR_INIT;
- if (git_filters_load(&filters,
- repo, path, GIT_FILTER_TO_WORKTREE) >= 0) {
- git_buf_clear(out);
+ if (git_filters_load(&filters, git_object_owner((git_object *)blob), hintpath, GIT_FILTER_TO_WORKTREE) >= 0)
retcode = git_filters_apply(out, &unfiltered, &filters);
- }
-
- git_filters_free(&filters);
- }
+ git_filters_free(&filters);
git_buf_free(&unfiltered);
+
return retcode;
}
-
diff --git a/src/filter.h b/src/filter.h
index 5b7a25b..d58e173 100644
--- a/src/filter.h
+++ b/src/filter.h
@@ -124,11 +124,10 @@ extern int git_text_is_binary(git_text_stats *stats);
* Get the content of a blob after all filters have been run.
*
* @param out buffer to receive the contents
- * @param repo repository containing the blob
- * @param oid object id for the blob
- * @param path path to the blob's output file, relative to the workdir root
+ * @param hintpath path to the blob's output file, relative to the workdir root.
+ * Used to determine what git filters should be applied to the content.
* @return 0 on success, an error code otherwise
*/
-extern int git_filter_blob_contents(git_buf *out, git_repository *repo, const git_oid *oid, const char *path);
+extern int git_filter_blob_content(git_buf *out, git_blob *blob, const char *hintpath);
#endif
diff --git a/tests-clar/checkout/checkout.c b/tests-clar/checkout/checkout.c
deleted file mode 100644
index ba14194..0000000
--- a/tests-clar/checkout/checkout.c
+++ /dev/null
@@ -1,206 +0,0 @@
-#include "clar_libgit2.h"
-
-#include "git2/checkout.h"
-#include "repository.h"
-
-
-static git_repository *g_repo;
-
-void test_checkout_checkout__initialize(void)
-{
- const char *attributes = "* text eol=lf\n";
-
- g_repo = cl_git_sandbox_init("testrepo");
- cl_git_mkfile("./testrepo/.gitattributes", attributes);
-}
-
-void test_checkout_checkout__cleanup(void)
-{
- cl_git_sandbox_cleanup();
-}
-
-
-static void test_file_contents(const char *path, const char *expectedcontents)
-{
- int fd;
- char buffer[1024] = {0};
- size_t expectedlen, actuallen;
-
- fd = p_open(path, O_RDONLY);
- cl_assert(fd >= 0);
-
- expectedlen = strlen(expectedcontents);
- actuallen = p_read(fd, buffer, 1024);
- cl_git_pass(p_close(fd));
-
- cl_assert_equal_sz(actuallen, expectedlen);
- cl_assert_equal_s(buffer, expectedcontents);
-}
-
-
-void test_checkout_checkout__bare(void)
-{
- cl_git_sandbox_cleanup();
- g_repo = cl_git_sandbox_init("testrepo.git");
- cl_git_fail(git_checkout_head(g_repo, NULL, NULL));
-}
-
-void test_checkout_checkout__default(void)
-{
- cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
- test_file_contents("./testrepo/README", "hey there\n");
- test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
- test_file_contents("./testrepo/new.txt", "my new file\n");
-}
-
-
-void test_checkout_checkout__crlf(void)
-{
- const char *attributes =
- "branch_file.txt text eol=crlf\n"
- "new.txt text eol=lf\n";
- git_config *cfg;
-
- cl_git_pass(git_repository_config__weakptr(&cfg, g_repo));
- cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", false));
- cl_git_mkfile("./testrepo/.gitattributes", attributes);
-
- cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
- test_file_contents("./testrepo/README", "hey there\n");
- test_file_contents("./testrepo/new.txt", "my new file\n");
- test_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n");
-}
-
-
-void test_checkout_checkout__win32_autocrlf(void)
-{
-#ifdef GIT_WIN32
- git_config *cfg;
- const char *expected_readme_text = "hey there\r\n";
-
- cl_must_pass(p_unlink("./testrepo/.gitattributes"));
- cl_git_pass(git_repository_config__weakptr(&cfg, g_repo));
- cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", true));
-
- cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
- test_file_contents("./testrepo/README", expected_readme_text);
-#endif
-}
-
-
-static void enable_symlinks(bool enable)
-{
- git_config *cfg;
- cl_git_pass(git_repository_config(&cfg, g_repo));
- cl_git_pass(git_config_set_bool(cfg, "core.symlinks", enable));
- git_config_free(cfg);
-}
-
-void test_checkout_checkout__symlinks(void)
-{
- /* First try with symlinks forced on */
- enable_symlinks(true);
- cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
-
-#ifdef GIT_WIN32
- test_file_contents("./testrepo/link_to_new.txt", "new.txt");
-#else
- {
- char link_data[1024];
- size_t link_size = 1024;
-
- link_size = p_readlink("./testrepo/link_to_new.txt", link_data, link_size);
- link_data[link_size] = '\0';
- cl_assert_equal_i(link_size, strlen("new.txt"));
- cl_assert_equal_s(link_data, "new.txt");
- test_file_contents("./testrepo/link_to_new.txt", "my new file\n");
- }
-#endif
-
- /* Now with symlinks forced off */
- cl_git_sandbox_cleanup();
- g_repo = cl_git_sandbox_init("testrepo");
- enable_symlinks(false);
- cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
-
- test_file_contents("./testrepo/link_to_new.txt", "new.txt");
-}
-
-void test_checkout_checkout__existing_file_skip(void)
-{
- git_checkout_opts opts = {0};
- cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
- opts.existing_file_action = GIT_CHECKOUT_SKIP_EXISTING;
- cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
- test_file_contents("./testrepo/new.txt", "This isn't what's stored!");
-}
-
-void test_checkout_checkout__existing_file_overwrite(void)
-{
- git_checkout_opts opts = {0};
- cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
- opts.existing_file_action = GIT_CHECKOUT_OVERWRITE_EXISTING;
- cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
- test_file_contents("./testrepo/new.txt", "my new file\n");
-}
-
-void test_checkout_checkout__disable_filters(void)
-{
- git_checkout_opts opts = {0};
- cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n");
- /* TODO cl_git_pass(git_checkout_head(g_repo, &opts, NULL));*/
- /* TODO test_file_contents("./testrepo/new.txt", "my new file\r\n");*/
- opts.disable_filters = true;
- cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
- test_file_contents("./testrepo/new.txt", "my new file\n");
-}
-
-void test_checkout_checkout__dir_modes(void)
-{
-#ifndef GIT_WIN32
- git_checkout_opts opts = {0};
- struct stat st;
- git_reference *ref;
-
- cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/dir"));
-
- opts.dir_mode = 0701;
- cl_git_pass(git_checkout_reference(ref, &opts, NULL));
- cl_git_pass(p_stat("./testrepo/a", &st));
- cl_assert_equal_i(st.st_mode & 0777, 0701);
-
- /* File-mode test, since we're on the 'dir' branch */
- cl_git_pass(p_stat("./testrepo/a/b.txt", &st));
- cl_assert_equal_i(st.st_mode & 0777, 0755);
-
- git_reference_free(ref);
-#endif
-}
-
-void test_checkout_checkout__override_file_modes(void)
-{
-#ifndef GIT_WIN32
- git_checkout_opts opts = {0};
- struct stat st;
-
- opts.file_mode = 0700;
- cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
- cl_git_pass(p_stat("./testrepo/new.txt", &st));
- cl_assert_equal_i(st.st_mode & 0777, 0700);
-#endif
-}
-
-void test_checkout_checkout__open_flags(void)
-{
- git_checkout_opts opts = {0};
-
- cl_git_mkfile("./testrepo/new.txt", "hi\n");
- opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
- cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
- test_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
-}
-
-void test_checkout_checkout__detached_head(void)
-{
- /* TODO: write this when git_checkout_commit is implemented. */
-}
diff --git a/tests-clar/checkout/tree.c b/tests-clar/checkout/tree.c
new file mode 100644
index 0000000..d04bba0
--- /dev/null
+++ b/tests-clar/checkout/tree.c
@@ -0,0 +1,268 @@
+#include "clar_libgit2.h"
+
+#include "git2/checkout.h"
+#include "repository.h"
+
+
+static git_repository *g_repo;
+static git_object *g_treeish;
+static git_checkout_opts g_opts;
+
+void test_checkout_tree__initialize(void)
+{
+ memset(&g_opts, 0, sizeof(g_opts));
+
+ g_repo = cl_git_sandbox_init("testrepo");
+
+ cl_git_rewritefile(
+ "./testrepo/.gitattributes",
+ "* text eol=lf\n");
+
+ cl_git_pass(git_repository_head_tree((git_tree **)&g_treeish, g_repo));
+}
+
+void test_checkout_tree__cleanup(void)
+{
+ git_object_free(g_treeish);
+ cl_git_sandbox_cleanup();
+}
+
+static void test_file_contents(const char *path, const char *expectedcontents)
+{
+ int fd;
+ char buffer[1024] = {0};
+ size_t expectedlen, actuallen;
+
+ fd = p_open(path, O_RDONLY);
+ cl_assert(fd >= 0);
+
+ expectedlen = strlen(expectedcontents);
+ actuallen = p_read(fd, buffer, 1024);
+ cl_git_pass(p_close(fd));
+
+ cl_assert_equal_sz(actuallen, expectedlen);
+ cl_assert_equal_s(buffer, expectedcontents);
+}
+
+void test_checkout_tree__cannot_checkout_a_bare_repository(void)
+{
+ test_checkout_tree__cleanup();
+
+ memset(&g_opts, 0, sizeof(g_opts));
+ g_repo = cl_git_sandbox_init("testrepo.git");
+ cl_git_pass(git_repository_head_tree((git_tree **)&g_treeish, g_repo));
+
+ cl_git_fail(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
+}
+
+void test_checkout_tree__update_the_content_of_workdir_with_missing_files(void)
+{
+ cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
+ cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
+ cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
+
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
+
+ test_file_contents("./testrepo/README", "hey there\n");
+ test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
+ test_file_contents("./testrepo/new.txt", "my new file\n");
+}
+
+void test_checkout_tree__honor_the_specified_pathspecs(void)
+{
+ git_strarray paths;
+ char *entries[] = { "*.txt" };
+
+ paths.strings = entries;
+ paths.count = 1;
+ g_opts.paths = &paths;
+
+ cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
+ cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
+ cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
+
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
+
+ cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
+ test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
+ test_file_contents("./testrepo/new.txt", "my new file\n");
+}
+
+static void set_config_entry_to(const char *entry_name, bool value)
+{
+ git_config *cfg;
+
+ cl_git_pass(git_repository_config(&cfg, g_repo));
+ cl_git_pass(git_config_set_bool(cfg, entry_name, value));
+
+ git_config_free(cfg);
+}
+
+static void set_core_autocrlf_to(bool value)
+{
+ set_config_entry_to("core.autocrlf", value);
+}
+
+void test_checkout_tree__honor_the_gitattributes_directives(void)
+{
+ const char *attributes =
+ "branch_file.txt text eol=crlf\n"
+ "new.txt text eol=lf\n";
+
+ cl_git_mkfile("./testrepo/.gitattributes", attributes);
+ set_core_autocrlf_to(false);
+
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
+
+ test_file_contents("./testrepo/README", "hey there\n");
+ test_file_contents("./testrepo/new.txt", "my new file\n");
+ test_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n");
+}
+
+void test_checkout_tree__honor_coreautocrlf_setting_set_to_true(void)
+{
+#ifdef GIT_WIN32
+ const char *expected_readme_text = "hey there\r\n";
+
+ cl_git_pass(p_unlink("./testrepo/.gitattributes"));
+ set_core_autocrlf_to(true);
+
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
+
+ test_file_contents("./testrepo/README", expected_readme_text);
+#endif
+}
+
+static void set_repo_symlink_handling_cap_to(bool value)
+{
+ set_config_entry_to("core.symlinks", value);
+}
+
+void test_checkout_tree__honor_coresymlinks_setting_set_to_true(void)
+{
+ set_repo_symlink_handling_cap_to(true);
+
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
+
+#ifdef GIT_WIN32
+ test_file_contents("./testrepo/link_to_new.txt", "new.txt");
+#else
+ {
+ char link_data[1024];
+ size_t link_size = 1024;
+
+ link_size = p_readlink("./testrepo/link_to_new.txt", link_data, link_size);
+ link_data[link_size] = '\0';
+ cl_assert_equal_i(link_size, strlen("new.txt"));
+ cl_assert_equal_s(link_data, "new.txt");
+ test_file_contents("./testrepo/link_to_new.txt", "my new file\n");
+ }
+#endif
+}
+
+void test_checkout_tree__honor_coresymlinks_setting_set_to_false(void)
+{
+ set_repo_symlink_handling_cap_to(false);
+
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
+
+ test_file_contents("./testrepo/link_to_new.txt", "new.txt");
+}
+
+void test_checkout_tree__options_skip_existing_file(void)
+{
+ cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
+ g_opts.existing_file_action = GIT_CHECKOUT_SKIP_EXISTING;
+
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
+
+ test_file_contents("./testrepo/new.txt", "This isn't what's stored!");
+}
+
+void test_checkout_tree__options_overwrite_existing_file(void)
+{
+ cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
+ g_opts.existing_file_action = GIT_CHECKOUT_OVERWRITE_EXISTING;
+
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
+
+ test_file_contents("./testrepo/new.txt", "my new file\n");
+}
+
+void test_checkout_tree__options_disable_filters(void)
+{
+ cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n");
+
+ g_opts.disable_filters = false;
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
+
+ test_file_contents("./testrepo/new.txt", "my new file\r\n");
+
+ p_unlink("./testrepo/new.txt");
+
+ g_opts.disable_filters = true;
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
+
+ test_file_contents("./testrepo/new.txt", "my new file\n");
+}
+
+void test_checkout_tree__options_dir_modes(void)
+{
+#ifndef GIT_WIN32
+ struct stat st;
+ git_oid oid;
+ git_commit *commit;
+
+ cl_git_pass(git_reference_name_to_oid(&oid, g_repo, "refs/heads/dir"));
+ cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
+
+ g_opts.dir_mode = 0701;
+ cl_git_pass(git_checkout_tree(g_repo, (git_object *)commit, &g_opts, NULL));
+
+ cl_git_pass(p_stat("./testrepo/a", &st));
+ cl_assert_equal_i(st.st_mode & 0777, 0701);
+
+ /* File-mode test, since we're on the 'dir' branch */
+ cl_git_pass(p_stat("./testrepo/a/b.txt", &st));
+ cl_assert_equal_i(st.st_mode & 0777, 0755);
+
+ git_commit_free(commit);
+#endif
+}
+
+void test_checkout_tree__options_override_file_modes(void)
+{
+#ifndef GIT_WIN32
+ struct stat st;
+
+ g_opts.file_mode = 0700;
+
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
+
+ cl_git_pass(p_stat("./testrepo/new.txt", &st));
+ cl_assert_equal_i(st.st_mode & 0777, 0700);
+#endif
+}
+
+void test_checkout_tree__options_open_flags(void)
+{
+ cl_git_mkfile("./testrepo/new.txt", "hi\n");
+
+ g_opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
+ cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
+
+ test_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
+}
+
+void test_checkout_tree__cannot_checkout_a_non_treeish(void)
+{
+ git_oid oid;
+ git_blob *blob;
+
+ cl_git_pass(git_oid_fromstr(&oid, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
+ cl_git_pass(git_blob_lookup(&blob, g_repo, &oid));
+
+ cl_git_fail(git_checkout_tree(g_repo, (git_object *)blob, NULL, NULL));
+
+ git_blob_free(blob);
+}