1 From 1855ca47e678e00ba9151f9734d7e682b7c67f6d Mon Sep 17 00:00:00 2001
2 From: Sven Strickroth <email@cs-ware.de>
3 Date: Sun, 1 Feb 2015 13:39:31 +0100
4 Subject: [PATCH] Make CRLF filter behave like vanilla git for adding files
5 in combination with attributes
7 Based on lots of manual testing (even with "malformed" files and binary) with vanilla git executable...
9 Signed-off-by: Sven Strickroth <email@cs-ware.de>
11 src/crlf.c | 57 +++++++++++++++++++++++++++++++++------------------------
12 1 files changed, 29 insertions(+), 28 deletions(-)
14 diff --git a/src/crlf.c b/src/crlf.c
15 index 858a94484..468f33b4c 100644
18 @@ -124,18 +124,21 @@ static int crlf_apply_to_odb(
20 const git_filter_source *src)
22 + git_buf_text_stats stats;
25 /* Empty file? Nothing to do */
26 if (!git_buf_len(from))
29 + is_binary = git_buf_text_gather_stats(&stats, from, false);
31 /* Heuristics to see if we can skip the conversion.
32 * Straight from Core Git.
34 if (ca->crlf_action == GIT_CRLF_AUTO || ca->crlf_action == GIT_CRLF_GUESS) {
35 - git_buf_text_stats stats;
37 - /* Check heuristics for binary vs text - returns true if binary */
38 - if (git_buf_text_gather_stats(&stats, from, false))
39 + /* Check heuristics for binary vs text */
41 return GIT_PASSTHROUGH;
44 @@ -146,16 +149,26 @@ static int crlf_apply_to_odb(
45 if (stats.cr != stats.crlf)
46 return GIT_PASSTHROUGH;
48 - /* If there are no CR characters to filter out and CrLf is not set to "true", then just pass */
49 - if (!stats.cr && ca->auto_crlf != GIT_AUTO_CRLF_TRUE)
50 - return GIT_PASSTHROUGH;
51 + if (ca->crlf_action == GIT_CRLF_GUESS) {
53 + * If the file in the index has any CR in it, do not convert.
54 + * This is the new safer autocrlf handling.
56 + if (has_cr_in_index(src))
57 + return GIT_PASSTHROUGH;
61 - /* If safecrlf is enabled, sanity-check the result. */
62 - if (stats.lf != stats.crlf) {
63 + /* If safecrlf is enabled, sanity-check the result. */
64 + if (ca->crlf_action == GIT_CRLF_INPUT ||
65 + (ca->auto_crlf == GIT_AUTO_CRLF_INPUT &&
66 + (ca->crlf_action == GIT_CRLF_GUESS || ca->crlf_action == GIT_CRLF_AUTO ||
67 + (ca->crlf_action == GIT_CRLF_TEXT && ca->eol==GIT_EOL_UNSET)))) {
69 switch (ca->safe_crlf) {
70 case GIT_SAFE_CRLF_FAIL:
72 - GITERR_FILTER, "LF would be replaced by CRLF in '%s'",
73 + GITERR_FILTER, "CRLF would be replaced by LF in '%s'",
74 git_filter_source_path(src));
76 case GIT_SAFE_CRLF_WARN:
77 @@ -164,11 +177,15 @@ static int crlf_apply_to_odb(
81 - } else if (stats.crlf && ca->auto_crlf == GIT_AUTO_CRLF_INPUT) {
83 + } else if (ca->crlf_action == GIT_CRLF_CRLF ||
84 + (ca->auto_crlf == GIT_AUTO_CRLF_TRUE && ca->crlf_action == GIT_CRLF_GUESS ||
85 + ((ca->crlf_action == GIT_CRLF_TEXT || ca->crlf_action == GIT_CRLF_AUTO) && ca->eol == GIT_EOL_UNSET))) {
86 + if (stats.lf != stats.crlf) {
87 switch (ca->safe_crlf) {
88 case GIT_SAFE_CRLF_FAIL:
90 - GITERR_FILTER, "CRLF would be replaced by LF in '%s'",
91 + GITERR_FILTER, "LF would be replaced by CRLF in '%s'",
92 git_filter_source_path(src));
94 case GIT_SAFE_CRLF_WARN:
95 @@ -178,20 +195,12 @@ static int crlf_apply_to_odb(
100 - if (ca->crlf_action == GIT_CRLF_GUESS) {
102 - * If the file in the index has any CR in it, do not convert.
103 - * This is the new safer autocrlf handling.
105 - if (has_cr_in_index(src))
106 - return GIT_PASSTHROUGH;
110 - return GIT_PASSTHROUGH;
113 + /* If there are no CR characters to filter out, then just pass */
115 + return GIT_PASSTHROUGH;
117 /* Actually drop the carriage returns */
118 return git_buf_text_crlf_to_lf(to, from);