Define 'crlf' attribute.
This defines the semantics of 'crlf' attribute as an example. When a path has this attribute unset (i.e. '!crlf'), autocrlf line-end conversion is not applied. Eventually we would want to let users to build a pipeline of processing to munge blob data to filesystem format (and in the other direction) based on combination of attributes, and at that point the mechanism in convert_to_{git,working_tree}() that looks at 'crlf' attribute needs to be enhanced. Perhaps the existing 'crlf' would become the first step in the input chain, and the last step in the output chain. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Apr 12, 2007 at 22:30 UTC
35ebfd6a0cd71795c4fa510b99e55ad89fb654f1
3 files changed
+84
-7
attr.c
+18
@@ -378,3 +378,21 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check)
378
rem = fill(path, pathlen, stk, check, num, rem);
379
return 0;
380
}
381
+
382
+static void setup_binary_check(struct git_attr_check *check)
383
+{
384
+ static struct git_attr *attr_binary;
385
+
386
+ if (!attr_binary)
387
+ attr_binary = git_attr("binary", 6);
388
+ check->attr = attr_binary;
389
+}
390
+
391
+int git_path_is_binary(const char *path)
392
+{
393
+ struct git_attr_check attr_binary_check;
394
+
395
+ setup_binary_check(&attr_binary_check);
396
+ return (!git_checkattr(path, 1, &attr_binary_check) &&
397
+ (0 < attr_binary_check.isset));
398
+}
convert.c
+42
-7
@@ -1,4 +1,6 @@
1
#include "cache.h"
2
+#include "attr.h"
3
+
4
/*
5
* convert.c - convert a file when checking it out and checking it in.
6
*
@@ -72,17 +74,12 @@ static int is_binary(unsigned long size, struct text_stat *stats)
74
return 0;
75
}
76
75
-int convert_to_git(const char *path, char **bufp, unsigned long *sizep)
77
+static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep)
78
{
79
char *buffer, *nbuf;
80
unsigned long size, nsize;
81
struct text_stat stats;
82
81
- /*
82
- * FIXME! Other pluggable conversions should go here,
83
- * based on filename patterns. Right now we just do the
84
- * stupid auto-CRLF one.
85
- */
83
if (!auto_crlf)
84
return 0;
85
@@ -128,7 +125,7 @@ int convert_to_git(const char *path, char **bufp, unsigned long *sizep)
125
return 1;
126
}
127
131
-int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
128
+static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
129
{
130
char *buffer, *nbuf;
131
unsigned long size, nsize;
@@ -184,3 +181,41 @@ int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
181
182
return 1;
183
}
184
+
185
+static void setup_crlf_check(struct git_attr_check *check)
186
+{
187
+ static struct git_attr *attr_crlf;
188
+
189
+ if (!attr_crlf)
190
+ attr_crlf = git_attr("crlf", 4);
191
+ check->attr = attr_crlf;
192
+}
193
+
194
+static int git_path_is_binary(const char *path)
195
+{
196
+ struct git_attr_check attr_crlf_check;
197
+
198
+ setup_crlf_check(&attr_crlf_check);
199
+
200
+ /*
201
+ * If crlf is not mentioned, default to autocrlf;
202
+ * disable autocrlf only when crlf attribute is explicitly
203
+ * unset.
204
+ */
205
+ return (!git_checkattr(path, 1, &attr_crlf_check) &&
206
+ (0 == attr_crlf_check.isset));
207
+}
208
+
209
+int convert_to_git(const char *path, char **bufp, unsigned long *sizep)
210
+{
211
+ if (git_path_is_binary(path))
212
+ return 0;
213
+ return autocrlf_to_git(path, bufp, sizep);
214
+}
215
+
216
+int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
217
+{
218
+ if (git_path_is_binary(path))
219
+ return 0;
220
+ return autocrlf_to_working_tree(path, bufp, sizep);
221
+}
t/t0020-crlf.sh
+24
@@ -214,4 +214,28 @@ test_expect_success 'apply patch --index (autocrlf=true)' '
214
}
215
'
216
217
+test_expect_success '.gitattributes says two is binary' '
218
+
219
+ echo "two !crlf" >.gitattributes &&
220
+ rm -f tmp one dir/two &&
221
+ git repo-config core.autocrlf true &&
222
+ git read-tree --reset -u HEAD &&
223
+
224
+ if remove_cr dir/two >/dev/null
225
+ then
226
+ echo "Huh?"
227
+ false
228
+ else
229
+ : happy
230
+ fi &&
231
+
232
+ if remove_cr one >/dev/null
233
+ then
234
+ : happy
235
+ else
236
+ echo "Huh?"
237
+ false
238
+ fi
239
+'
240
+
241
test_done