@matheusbsbs / matheus-bagliano / commits / 163b959194
Fork 0

Update 'crlf' attribute semantics.

This updates the semantics of 'crlf' so that .gitattributes file can say "this is text, even though it may look funny". Setting the `crlf` attribute on a path is meant to mark the path as a "text" file. 'core.autocrlf' conversion takes place without guessing the content type by inspection. Unsetting the `crlf` attribute on a path is meant to mark the path as a "binary" file. The path never goes through line endings conversion upon checkin/checkout. Unspecified `crlf` attribute tells git to apply the `core.autocrlf` conversion when the file content looks like text. Setting the `crlf` attribut to string value "input" is similar to setting the attribute to `true`, but also forces git to act as if `core.autocrlf` is set to `input` for the path. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 19, 2007 at 22:37 UTC 163b95919428cd7d782af91296e0b886683f2daa
2 files changed +89 -60
convert.c
+26 -49
@@ -10,6 +10,11 @@
10 * translation when the "auto_crlf" option is set.
11 */
12
13 +#define CRLF_GUESS (-1)
14 +#define CRLF_BINARY 0
15 +#define CRLF_TEXT 1
16 +#define CRLF_INPUT 2
17 +
18 struct text_stat {
19 /* CR, LF and CRLF counts */
20 unsigned cr, lf, crlf;
@@ -74,13 +79,13 @@ static int is_binary(unsigned long size, struct text_stat *stats)
79 return 0;
80 }
81
77 -static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int guess)
82 +static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int action)
83 {
84 char *buffer, *nbuf;
85 unsigned long size, nsize;
86 struct text_stat stats;
87
83 - if (guess && !auto_crlf)
88 + if ((action == CRLF_BINARY) || (action == CRLF_GUESS && !auto_crlf))
89 return 0;
90
91 size = *sizep;
@@ -94,7 +99,7 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int
99 if (!stats.cr)
100 return 0;
101
97 - if (guess) {
102 + if (action == CRLF_GUESS) {
103 /*
104 * We're currently not going to even try to convert stuff
105 * that has bare CR characters. Does anybody do that crazy
@@ -119,7 +124,12 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int
124 *bufp = nbuf;
125 *sizep = nsize;
126
122 - if (guess) {
127 + if (action == CRLF_GUESS) {
128 + /*
129 + * If we guessed, we already know we rejected a file with
130 + * lone CR, and we can strip a CR without looking at what
131 + * follow it.
132 + */
133 do {
134 unsigned char c = *buffer++;
135 if (c != '\r')
@@ -136,24 +146,15 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int
146 return 1;
147 }
148
139 -static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep)
140 -{
141 - return crlf_to_git(path, bufp, sizep, 1);
142 -}
143 -
144 -static int forcecrlf_to_git(const char *path, char **bufp, unsigned long *sizep)
145 -{
146 - return crlf_to_git(path, bufp, sizep, 0);
147 -}
148 -
149 -static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep, int guess)
149 +static int crlf_to_worktree(const char *path, char **bufp, unsigned long *sizep, int action)
150 {
151 char *buffer, *nbuf;
152 unsigned long size, nsize;
153 struct text_stat stats;
154 unsigned char last;
155
156 - if (guess && auto_crlf <= 0)
156 + if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
157 + (action == CRLF_GUESS && auto_crlf <= 0))
158 return 0;
159
160 size = *sizep;
@@ -171,7 +172,7 @@ static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *si
172 if (stats.lf == stats.crlf)
173 return 0;
174
174 - if (guess) {
175 + if (action == CRLF_GUESS) {
176 /* If we have any bare CR characters, we're not going to touch it */
177 if (stats.cr != stats.crlf)
178 return 0;
@@ -200,16 +201,6 @@ static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *si
201 return 1;
202 }
203
203 -static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
204 -{
205 - return crlf_to_working_tree(path, bufp, sizep, 1);
206 -}
207 -
208 -static int forcecrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
209 -{
210 - return crlf_to_working_tree(path, bufp, sizep, 0);
211 -}
212 -
204 static void setup_crlf_check(struct git_attr_check *check)
205 {
206 static struct git_attr *attr_crlf;
@@ -228,38 +219,24 @@ static int git_path_check_crlf(const char *path)
219 if (!git_checkattr(path, 1, &attr_crlf_check)) {
220 const char *value = attr_crlf_check.value;
221 if (ATTR_TRUE(value))
231 - return 1;
222 + return CRLF_TEXT;
223 else if (ATTR_FALSE(value))
233 - return 0;
224 + return CRLF_BINARY;
225 else if (ATTR_UNSET(value))
226 ;
236 - else
237 - die("unknown value %s given to 'crlf' attribute",
238 - (char *)value);
227 + else if (!strcmp(value, "input"))
228 + return CRLF_INPUT;
229 + /* fallthru */
230 }
240 - return -1;
231 + return CRLF_GUESS;
232 }
233
234 int convert_to_git(const char *path, char **bufp, unsigned long *sizep)
235 {
245 - switch (git_path_check_crlf(path)) {
246 - case 0:
247 - return 0;
248 - case 1:
249 - return forcecrlf_to_git(path, bufp, sizep);
250 - default:
251 - return autocrlf_to_git(path, bufp, sizep);
252 - }
236 + return crlf_to_git(path, bufp, sizep, git_path_check_crlf(path));
237 }
238
239 int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
240 {
257 - switch (git_path_check_crlf(path)) {
258 - case 0:
259 - return 0;
260 - case 1:
261 - return forcecrlf_to_working_tree(path, bufp, sizep);
262 - default:
263 - return autocrlf_to_working_tree(path, bufp, sizep);
264 - }
241 + return crlf_to_worktree(path, bufp, sizep, git_path_check_crlf(path));
242 }
t/t0020-crlf.sh
+63 -11
@@ -4,6 +4,10 @@ test_description='CRLF conversion'
4
5 . ./test-lib.sh
6
7 +q_to_nul () {
8 + tr Q '\0'
9 +}
10 +
11 append_cr () {
12 sed -e 's/$/Q/' | tr Q '\015'
13 }
@@ -20,6 +24,7 @@ test_expect_success setup '
24 for w in Hello world how are you; do echo $w; done >one &&
25 mkdir dir &&
26 for w in I am very very fine thank you; do echo $w; done >dir/two &&
27 + for w in Oh here is NULQin text here; do echo $w; done | q_to_nul >three &&
28 git add . &&
29
30 git commit -m initial &&
@@ -27,6 +32,7 @@ test_expect_success setup '
32 one=`git rev-parse HEAD:one` &&
33 dir=`git rev-parse HEAD:dir` &&
34 two=`git rev-parse HEAD:dir/two` &&
35 + three=`git rev-parse HEAD:three` &&
36
37 for w in Some extra lines here; do echo $w; done >>one &&
38 git diff >patch.file &&
@@ -38,7 +44,7 @@ test_expect_success setup '
44
45 test_expect_success 'update with autocrlf=input' '
46
41 - rm -f tmp one dir/two &&
47 + rm -f tmp one dir/two three &&
48 git read-tree --reset -u HEAD &&
49 git repo-config core.autocrlf input &&
50
@@ -62,7 +68,7 @@ test_expect_success 'update with autocrlf=input' '
68
69 test_expect_success 'update with autocrlf=true' '
70
65 - rm -f tmp one dir/two &&
71 + rm -f tmp one dir/two three &&
72 git read-tree --reset -u HEAD &&
73 git repo-config core.autocrlf true &&
74
@@ -86,7 +92,7 @@ test_expect_success 'update with autocrlf=true' '
92
93 test_expect_success 'checkout with autocrlf=true' '
94
89 - rm -f tmp one dir/two &&
95 + rm -f tmp one dir/two three &&
96 git repo-config core.autocrlf true &&
97 git read-tree --reset -u HEAD &&
98
@@ -110,7 +116,7 @@ test_expect_success 'checkout with autocrlf=true' '
116
117 test_expect_success 'checkout with autocrlf=input' '
118
113 - rm -f tmp one dir/two &&
119 + rm -f tmp one dir/two three &&
120 git repo-config core.autocrlf input &&
121 git read-tree --reset -u HEAD &&
122
@@ -136,7 +142,7 @@ test_expect_success 'checkout with autocrlf=input' '
142
143 test_expect_success 'apply patch (autocrlf=input)' '
144
139 - rm -f tmp one dir/two &&
145 + rm -f tmp one dir/two three &&
146 git repo-config core.autocrlf input &&
147 git read-tree --reset -u HEAD &&
148
@@ -149,7 +155,7 @@ test_expect_success 'apply patch (autocrlf=input)' '
155
156 test_expect_success 'apply patch --cached (autocrlf=input)' '
157
152 - rm -f tmp one dir/two &&
158 + rm -f tmp one dir/two three &&
159 git repo-config core.autocrlf input &&
160 git read-tree --reset -u HEAD &&
161
@@ -162,7 +168,7 @@ test_expect_success 'apply patch --cached (autocrlf=input)' '
168
169 test_expect_success 'apply patch --index (autocrlf=input)' '
170
165 - rm -f tmp one dir/two &&
171 + rm -f tmp one dir/two three &&
172 git repo-config core.autocrlf input &&
173 git read-tree --reset -u HEAD &&
174
@@ -176,7 +182,7 @@ test_expect_success 'apply patch --index (autocrlf=input)' '
182
183 test_expect_success 'apply patch (autocrlf=true)' '
184
179 - rm -f tmp one dir/two &&
185 + rm -f tmp one dir/two three &&
186 git repo-config core.autocrlf true &&
187 git read-tree --reset -u HEAD &&
188
@@ -189,7 +195,7 @@ test_expect_success 'apply patch (autocrlf=true)' '
195
196 test_expect_success 'apply patch --cached (autocrlf=true)' '
197
192 - rm -f tmp one dir/two &&
198 + rm -f tmp one dir/two three &&
199 git repo-config core.autocrlf true &&
200 git read-tree --reset -u HEAD &&
201
@@ -202,7 +208,7 @@ test_expect_success 'apply patch --cached (autocrlf=true)' '
208
209 test_expect_success 'apply patch --index (autocrlf=true)' '
210
205 - rm -f tmp one dir/two &&
211 + rm -f tmp one dir/two three &&
212 git repo-config core.autocrlf true &&
213 git read-tree --reset -u HEAD &&
214
@@ -216,8 +222,8 @@ test_expect_success 'apply patch --index (autocrlf=true)' '
222
223 test_expect_success '.gitattributes says two is binary' '
224
225 + rm -f tmp one dir/two three &&
226 echo "two -crlf" >.gitattributes &&
220 - rm -f tmp one dir/two &&
227 git repo-config core.autocrlf true &&
228 git read-tree --reset -u HEAD &&
229
@@ -230,6 +236,52 @@ test_expect_success '.gitattributes says two is binary' '
236 fi &&
237
238 if remove_cr one >/dev/null
239 + then
240 + : happy
241 + else
242 + echo "Huh?"
243 + false
244 + fi &&
245 +
246 + if remove_cr three >/dev/null
247 + then
248 + echo "Huh?"
249 + false
250 + else
251 + : happy
252 + fi
253 +'
254 +
255 +test_expect_success '.gitattributes says two is input' '
256 +
257 + rm -f tmp one dir/two three &&
258 + echo "two crlf=input" >.gitattributes &&
259 + git read-tree --reset -u HEAD &&
260 +
261 + if remove_cr dir/two >/dev/null
262 + then
263 + echo "Huh?"
264 + false
265 + else
266 + : happy
267 + fi
268 +'
269 +
270 +test_expect_success '.gitattributes says two and three are text' '
271 +
272 + rm -f tmp one dir/two three &&
273 + echo "t* crlf" >.gitattributes &&
274 + git read-tree --reset -u HEAD &&
275 +
276 + if remove_cr dir/two >/dev/null
277 + then
278 + : happy
279 + else
280 + echo "Huh?"
281 + false
282 + fi &&
283 +
284 + if remove_cr three >/dev/null
285 then
286 : happy
287 else