Use CStrBuf
[TortoiseGit.git] / ext / libgit2-0005-Make-CRLF-filter-behave-like-vanilla-git-for-adding-.patch
blobaa3e11c0ad1586f5182095f42412ed45d79ba927
1 From 47fd8aac030c9a685b930574a34fddc41860ed62 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 5/6] 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>
10 ---
11 CHANGELOG.md | 5 +++++
12 src/crlf.c | 57 +++++++++++++++++++++++++++++++++------------------------
13 2 files changed, 38 insertions(+), 24 deletions(-)
15 diff --git a/CHANGELOG.md b/CHANGELOG.md
16 index d210632..b22b497 100644
17 --- a/CHANGELOG.md
18 +++ b/CHANGELOG.md
19 @@ -15,6 +15,11 @@ v0.22 + 1
20 * files containig CRLF in combination with core.autocrlf=input were accepted
21 * adding files containing CR and CRLF but not the same number failed
23 +* LF -> CRLF filter now handles files on adding to index the way vanilla git does
24 + * skip files marked as "binary" (-crlf)
25 + * especially files containing single CR chars are added as is now
26 + * honor "text" attribute for forcing a file being interpreted as text
28 * Rename and copy detection is enabled for small files.
30 * Checkout can now handle an initial checkout of a repository, making
31 diff --git a/src/crlf.c b/src/crlf.c
32 index 2de107c..444e7a6 100644
33 --- a/src/crlf.c
34 +++ b/src/crlf.c
35 @@ -124,18 +124,21 @@ static int crlf_apply_to_odb(
36 const git_buf *from,
37 const git_filter_source *src)
39 + git_buf_text_stats stats;
40 + bool is_binary;
42 /* Empty file? Nothing to do */
43 if (!git_buf_len(from))
44 return 0;
46 + is_binary = git_buf_text_gather_stats(&stats, from, false);
48 /* Heuristics to see if we can skip the conversion.
49 * Straight from Core Git.
51 if (ca->crlf_action == GIT_CRLF_AUTO || ca->crlf_action == GIT_CRLF_GUESS) {
52 - git_buf_text_stats stats;
54 - /* Check heuristics for binary vs text - returns true if binary */
55 - if (git_buf_text_gather_stats(&stats, from, false))
56 + /* Check heuristics for binary vs text */
57 + if (is_binary)
58 return GIT_PASSTHROUGH;
61 @@ -146,16 +149,26 @@ static int crlf_apply_to_odb(
62 if (stats.cr != stats.crlf)
63 return GIT_PASSTHROUGH;
65 - /* If there are no CR characters to filter out and CrLf is not set to "true", then just pass */
66 - if (!stats.cr && ca->auto_crlf != GIT_AUTO_CRLF_TRUE)
67 - return GIT_PASSTHROUGH;
68 + if (ca->crlf_action == GIT_CRLF_GUESS) {
69 + /*
70 + * If the file in the index has any CR in it, do not convert.
71 + * This is the new safer autocrlf handling.
72 + */
73 + if (has_cr_in_index(src))
74 + return GIT_PASSTHROUGH;
75 + }
76 + }
78 - /* If safecrlf is enabled, sanity-check the result. */
79 - if (stats.lf != stats.crlf) {
80 + /* If safecrlf is enabled, sanity-check the result. */
81 + if (ca->crlf_action == GIT_CRLF_INPUT ||
82 + (ca->auto_crlf == GIT_AUTO_CRLF_INPUT &&
83 + (ca->crlf_action == GIT_CRLF_GUESS || ca->crlf_action == GIT_CRLF_AUTO ||
84 + (ca->crlf_action == GIT_CRLF_TEXT && ca->eol==GIT_EOL_UNSET)))) {
85 + if (stats.crlf) {
86 switch (ca->safe_crlf) {
87 case GIT_SAFE_CRLF_FAIL:
88 giterr_set(
89 - GITERR_FILTER, "LF would be replaced by CRLF in '%s'",
90 + GITERR_FILTER, "CRLF would be replaced by LF in '%s'",
91 git_filter_source_path(src));
92 return -1;
93 case GIT_SAFE_CRLF_WARN:
94 @@ -164,11 +177,15 @@ static int crlf_apply_to_odb(
95 default:
96 break;
98 - } else if (stats.crlf && ca->auto_crlf == GIT_AUTO_CRLF_INPUT) {
99 + }
100 + } else if (ca->crlf_action == GIT_CRLF_CRLF ||
101 + (ca->auto_crlf == GIT_AUTO_CRLF_TRUE && ca->crlf_action == GIT_CRLF_GUESS ||
102 + ((ca->crlf_action == GIT_CRLF_TEXT || ca->crlf_action == GIT_CRLF_AUTO) && ca->eol == GIT_EOL_UNSET))) {
103 + if (stats.lf != stats.crlf) {
104 switch (ca->safe_crlf) {
105 case GIT_SAFE_CRLF_FAIL:
106 giterr_set(
107 - GITERR_FILTER, "CRLF would be replaced by LF in '%s'",
108 + GITERR_FILTER, "LF would be replaced by CRLF in '%s'",
109 git_filter_source_path(src));
110 return -1;
111 case GIT_SAFE_CRLF_WARN:
112 @@ -178,20 +195,12 @@ static int crlf_apply_to_odb(
113 break;
117 - if (ca->crlf_action == GIT_CRLF_GUESS) {
118 - /*
119 - * If the file in the index has any CR in it, do not convert.
120 - * This is the new safer autocrlf handling.
121 - */
122 - if (has_cr_in_index(src))
123 - return GIT_PASSTHROUGH;
126 - if (!stats.cr)
127 - return GIT_PASSTHROUGH;
130 + /* If there are no CR characters to filter out, then just pass */
131 + if (!stats.cr)
132 + return GIT_PASSTHROUGH;
134 /* Actually drop the carriage returns */
135 return git_buf_text_crlf_to_lf(to, from);
138 2.4.4.windows.2