Merge pull request #2375 from libgit2/rb/safecrlf-on-lf-platform Make core.safecrlf not generate an error on LF-ending platforms
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
diff --git a/src/crlf.c b/src/crlf.c
index dad3ece..821e04e 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -138,6 +138,10 @@ static int crlf_apply_to_odb(
if (git_buf_text_gather_stats(&stats, from, false))
return GIT_PASSTHROUGH;
+ /* 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) {
diff --git a/tests/filter/crlf.c b/tests/filter/crlf.c
index 66c267e..334b1e3 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 cf69c62..7babd59 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"));
+}