diff --git a/src/checkout.c b/src/checkout.c index cca66c34f..b58ef9f44 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -687,19 +687,23 @@ static int blob_content_to_file( git_buf unfiltered = GIT_BUF_INIT, filtered = GIT_BUF_INIT; git_vector filters = GIT_VECTOR_INIT; - if (opts->disable_filters || + /* Create a fake git_buf from the blob raw data... */ + filtered.ptr = blob->odb_object->raw.data; + filtered.size = blob->odb_object->raw.len; + /* ... and make sure it doesn't get unexpectedly freed */ + dont_free_filtered = true; + + if (!opts->disable_filters && + !git_buf_text_is_binary(&filtered) && (nb_filters = git_filters_load( &filters, git_object_owner((git_object *)blob), path, - GIT_FILTER_TO_WORKTREE)) == 0) { - - /* Create a fake git_buf from the blob raw data... */ - filtered.ptr = blob->odb_object->raw.data; - filtered.size = blob->odb_object->raw.len; - - /* ... and make sure it doesn't get unexpectedly freed */ - dont_free_filtered = true; + GIT_FILTER_TO_WORKTREE)) > 0) + { + /* reset 'filtered' so it can be a filter target */ + git_buf_init(&filtered, 0); + dont_free_filtered = false; } if (nb_filters < 0) diff --git a/tests-clar/checkout/binaryunicode.c b/tests-clar/checkout/binaryunicode.c new file mode 100644 index 000000000..5a781740f --- /dev/null +++ b/tests-clar/checkout/binaryunicode.c @@ -0,0 +1,68 @@ +#include "clar_libgit2.h" +#include "refs.h" +#include "repo/repo_helpers.h" +#include "path.h" +#include "fileops.h" + +static git_repository *g_repo; + +void test_checkout_binaryunicode__initialize(void) +{ + g_repo = cl_git_sandbox_init("binaryunicode"); +} + +void test_checkout_binaryunicode__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +static void execute_test(void) +{ + git_oid oid, check; + git_commit *commit; + git_tree *tree; + git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; + + cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/branch1")); + cl_git_pass(git_commit_lookup(&commit, g_repo, &oid)); + cl_git_pass(git_commit_tree(&tree, commit)); + + opts.checkout_strategy = GIT_CHECKOUT_SAFE; + + cl_git_pass(git_checkout_tree(g_repo, (git_object *)tree, &opts)); + + git_tree_free(tree); + git_commit_free(commit); + + /* Verify that the lenna.jpg file was checked out correctly */ + cl_git_pass(git_oid_fromstr(&check, "8ab005d890fe53f65eda14b23672f60d9f4ec5a1")); + cl_git_pass(git_odb_hashfile(&oid, "binaryunicode/lenna.jpg", GIT_OBJ_BLOB)); + cl_assert(git_oid_equal(&oid, &check)); + + /* Verify that the text file was checked out correctly */ + cl_git_pass(git_oid_fromstr(&check, "965b223880dd4249e2c66a0cc0b4cffe1dc40f5a")); + cl_git_pass(git_odb_hashfile(&oid, "binaryunicode/utf16_withbom_noeol_crlf.txt", GIT_OBJ_BLOB)); + cl_assert(git_oid_equal(&oid, &check)); +} + +void test_checkout_binaryunicode__noautocrlf(void) +{ + git_config *config; + + cl_git_pass(git_repository_config(&config, g_repo)); + cl_git_pass(git_config_set_bool(config, "core.autocrlf", false)); + git_config_free(config); + + execute_test(); +} + +void test_checkout_binaryunicode__autocrlf(void) +{ + git_config *config; + + cl_git_pass(git_repository_config(&config, g_repo)); + cl_git_pass(git_config_set_bool(config, "core.autocrlf", true)); + git_config_free(config); + + execute_test(); +} diff --git a/tests-clar/resources/binaryunicode/.gitted/HEAD b/tests-clar/resources/binaryunicode/.gitted/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/tests-clar/resources/binaryunicode/.gitted/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/tests-clar/resources/binaryunicode/.gitted/config b/tests-clar/resources/binaryunicode/.gitted/config new file mode 100644 index 000000000..f9845fe7e --- /dev/null +++ b/tests-clar/resources/binaryunicode/.gitted/config @@ -0,0 +1,6 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + autocrlf = true + logallrefupdates = true diff --git a/tests-clar/resources/binaryunicode/.gitted/description b/tests-clar/resources/binaryunicode/.gitted/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/tests-clar/resources/binaryunicode/.gitted/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/tests-clar/resources/binaryunicode/.gitted/index b/tests-clar/resources/binaryunicode/.gitted/index new file mode 100644 index 000000000..a216d2219 Binary files /dev/null and b/tests-clar/resources/binaryunicode/.gitted/index differ diff --git a/tests-clar/resources/binaryunicode/.gitted/info/exclude b/tests-clar/resources/binaryunicode/.gitted/info/exclude new file mode 100644 index 000000000..a5196d1be --- /dev/null +++ b/tests-clar/resources/binaryunicode/.gitted/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/tests-clar/resources/binaryunicode/.gitted/info/refs b/tests-clar/resources/binaryunicode/.gitted/info/refs new file mode 100644 index 000000000..128eea7c9 --- /dev/null +++ b/tests-clar/resources/binaryunicode/.gitted/info/refs @@ -0,0 +1,3 @@ +39e046d1416a208265b754124d0d197b4c9c0c47 refs/heads/branch1 +9e7d8bcd4d24dd57e3f1179aaf7afe648ff50e80 refs/heads/branch2 +d2a291469f4c11f387600d189313b927ddfe891c refs/heads/master diff --git a/tests-clar/resources/binaryunicode/.gitted/objects/info/packs b/tests-clar/resources/binaryunicode/.gitted/objects/info/packs new file mode 100644 index 000000000..c2de8f5cb --- /dev/null +++ b/tests-clar/resources/binaryunicode/.gitted/objects/info/packs @@ -0,0 +1,2 @@ +P pack-c5bfca875b4995d7aba6e5abf36241f3c397327d.pack + diff --git a/tests-clar/resources/binaryunicode/.gitted/objects/pack/pack-c5bfca875b4995d7aba6e5abf36241f3c397327d.idx b/tests-clar/resources/binaryunicode/.gitted/objects/pack/pack-c5bfca875b4995d7aba6e5abf36241f3c397327d.idx new file mode 100644 index 000000000..8a05b2beb Binary files /dev/null and b/tests-clar/resources/binaryunicode/.gitted/objects/pack/pack-c5bfca875b4995d7aba6e5abf36241f3c397327d.idx differ diff --git a/tests-clar/resources/binaryunicode/.gitted/objects/pack/pack-c5bfca875b4995d7aba6e5abf36241f3c397327d.pack b/tests-clar/resources/binaryunicode/.gitted/objects/pack/pack-c5bfca875b4995d7aba6e5abf36241f3c397327d.pack new file mode 100644 index 000000000..6b5ddc414 Binary files /dev/null and b/tests-clar/resources/binaryunicode/.gitted/objects/pack/pack-c5bfca875b4995d7aba6e5abf36241f3c397327d.pack differ diff --git a/tests-clar/resources/binaryunicode/.gitted/refs/heads/branch1 b/tests-clar/resources/binaryunicode/.gitted/refs/heads/branch1 new file mode 100644 index 000000000..0595fbd31 --- /dev/null +++ b/tests-clar/resources/binaryunicode/.gitted/refs/heads/branch1 @@ -0,0 +1 @@ +39e046d1416a208265b754124d0d197b4c9c0c47 diff --git a/tests-clar/resources/binaryunicode/.gitted/refs/heads/branch2 b/tests-clar/resources/binaryunicode/.gitted/refs/heads/branch2 new file mode 100644 index 000000000..d86856687 --- /dev/null +++ b/tests-clar/resources/binaryunicode/.gitted/refs/heads/branch2 @@ -0,0 +1 @@ +9e7d8bcd4d24dd57e3f1179aaf7afe648ff50e80 diff --git a/tests-clar/resources/binaryunicode/.gitted/refs/heads/master b/tests-clar/resources/binaryunicode/.gitted/refs/heads/master new file mode 100644 index 000000000..552d166da --- /dev/null +++ b/tests-clar/resources/binaryunicode/.gitted/refs/heads/master @@ -0,0 +1 @@ +d2a291469f4c11f387600d189313b927ddfe891c diff --git a/tests-clar/resources/binaryunicode/file.txt b/tests-clar/resources/binaryunicode/file.txt new file mode 100644 index 000000000..2255035d4 --- /dev/null +++ b/tests-clar/resources/binaryunicode/file.txt @@ -0,0 +1 @@ +Master branch.