From 16798d08cf3a1757deb9f0363b35fbf775cfc3fb Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Mon, 19 May 2014 14:57:09 -0700 Subject: [PATCH 1/2] Make core.safecrlf work on LF-ending platforms If you enabled core.safecrlf on an LF-ending platform, we would error even for files with all LFs. We should only be warning on irreversible mappings, I think. --- src/crlf.c | 4 +++- tests/filter/crlf.c | 6 +++--- tests/index/crlf.c | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/crlf.c b/src/crlf.c index dad3ecebc..22cba84ab 100644 --- a/src/crlf.c +++ b/src/crlf.c @@ -139,7 +139,9 @@ static int crlf_apply_to_odb( return GIT_PASSTHROUGH; /* If safecrlf is enabled, sanity-check the result. */ - if (stats.cr != stats.crlf || stats.lf != stats.crlf) { + if (stats.cr != stats.crlf || + (stats.crlf > 0 && stats.lf != stats.crlf)) { + switch (ca->safe_crlf) { case GIT_SAFE_CRLF_FAIL: giterr_set( diff --git a/tests/filter/crlf.c b/tests/filter/crlf.c index 66c267e31..334b1e349 100644 --- a/tests/filter/crlf.c +++ b/tests/filter/crlf.c @@ -103,12 +103,12 @@ void test_filter_crlf__with_safecrlf(void) cl_git_fail(git_filter_list_apply_to_data(&out, fl, &in)); cl_assert_equal_i(giterr_last()->klass, GITERR_FILTER); - /* Normalized \n fails with safecrlf */ + /* Normalized \n is reversible, so does not fail with safecrlf */ in.ptr = "Normal\nLF\nonly\nline-endings.\n"; in.size = strlen(in.ptr); - cl_git_fail(git_filter_list_apply_to_data(&out, fl, &in)); - cl_assert_equal_i(giterr_last()->klass, GITERR_FILTER); + cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_assert_equal_s(in.ptr, out.ptr); git_filter_list_free(fl); git_buf_free(&out); diff --git a/tests/index/crlf.c b/tests/index/crlf.c index cf69c6226..7babd5939 100644 --- a/tests/index/crlf.c +++ b/tests/index/crlf.c @@ -134,3 +134,21 @@ void test_index_crlf__autocrlf_input_text_auto_attr(void) cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF)); cl_assert(git_oid_cmp(&oid, &entry->id) == 0); } + +void test_index_crlf__safecrlf_true_no_attrs(void) +{ + cl_repo_set_bool(g_repo, "core.autocrlf", true); + cl_repo_set_bool(g_repo, "core.safecrlf", true); + + cl_git_mkfile("crlf/newfile.txt", ALL_LF_TEXT_RAW); + cl_git_pass(git_index_add_bypath(g_index, "newfile.txt")); + + cl_git_mkfile("crlf/newfile.txt", ALL_CRLF_TEXT_RAW); + cl_git_pass(git_index_add_bypath(g_index, "newfile.txt")); + + cl_git_mkfile("crlf/newfile.txt", MORE_CRLF_TEXT_RAW); + cl_git_fail(git_index_add_bypath(g_index, "newfile.txt")); + + cl_git_mkfile("crlf/newfile.txt", MORE_LF_TEXT_RAW); + cl_git_fail(git_index_add_bypath(g_index, "newfile.txt")); +} From c094197bf92736bb1c40cf1ca87bda970ab7f999 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Mon, 19 May 2014 15:05:39 -0700 Subject: [PATCH 2/2] Just don't CRLF filter if there are no CRs --- src/crlf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/crlf.c b/src/crlf.c index 22cba84ab..821e04eb2 100644 --- a/src/crlf.c +++ b/src/crlf.c @@ -138,10 +138,12 @@ static int crlf_apply_to_odb( if (git_buf_text_gather_stats(&stats, from, false)) return GIT_PASSTHROUGH; - /* If safecrlf is enabled, sanity-check the result. */ - if (stats.cr != stats.crlf || - (stats.crlf > 0 && stats.lf != stats.crlf)) { + /* If there are no CR characters to filter out, then just pass */ + if (!stats.cr) + return GIT_PASSTHROUGH; + /* If safecrlf is enabled, sanity-check the result. */ + if (stats.cr != stats.crlf || stats.lf != stats.crlf) { switch (ca->safe_crlf) { case GIT_SAFE_CRLF_FAIL: giterr_set(