Teach 'diff' about 'diff' attribute.
This makes paths that explicitly unset 'diff' attribute not to produce "textual" diffs from 'git-diff' family. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Apr 12, 2007 at 23:05 UTC
8c701249d2257699c19822b528c101668abc55b9
2 files changed
+31
-27
attr.c
-18
@@ -378,21 +378,3 @@ 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
-}
diff.c
+31
-9
@@ -8,6 +8,7 @@
8
#include "delta.h"
9
#include "xdiff-interface.h"
10
#include "color.h"
11
+#include "attr.h"
12
13
#ifdef NO_FAST_WORKING_DIRECTORY
14
#define FAST_WORKING_DIRECTORY 0
@@ -1051,13 +1052,34 @@ static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
1052
emit_binary_diff_body(two, one);
1053
}
1054
1055
+static void setup_diff_attr_check(struct git_attr_check *check)
1056
+{
1057
+ static struct git_attr *attr_diff;
1058
+
1059
+ if (!attr_diff)
1060
+ attr_diff = git_attr("diff", 4);
1061
+ check->attr = attr_diff;
1062
+}
1063
+
1064
#define FIRST_FEW_BYTES 8000
1055
-static int mmfile_is_binary(mmfile_t *mf)
1065
+static int file_is_binary(struct diff_filespec *one)
1066
{
1057
- long sz = mf->size;
1067
+ unsigned long sz;
1068
+ struct git_attr_check attr_diff_check;
1069
+
1070
+ setup_diff_attr_check(&attr_diff_check);
1071
+ if (!git_checkattr(one->path, 1, &attr_diff_check) &&
1072
+ (0 == attr_diff_check.isset))
1073
+ return 1;
1074
+ if (!one->data) {
1075
+ if (!DIFF_FILE_VALID(one))
1076
+ return 0;
1077
+ diff_populate_filespec(one, 0);
1078
+ }
1079
+ sz = one->size;
1080
if (FIRST_FEW_BYTES < sz)
1081
sz = FIRST_FEW_BYTES;
1060
- return !!memchr(mf->ptr, 0, sz);
1082
+ return !!memchr(one->data, 0, sz);
1083
}
1084
1085
static void builtin_diff(const char *name_a,
@@ -1114,7 +1136,7 @@ static void builtin_diff(const char *name_a,
1136
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1137
die("unable to read files to diff");
1138
1117
- if (!o->text && (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))) {
1139
+ if (!o->text && (file_is_binary(one) || file_is_binary(two))) {
1140
/* Quite common confusing case */
1141
if (mf1.size == mf2.size &&
1142
!memcmp(mf1.ptr, mf2.ptr, mf1.size))
@@ -1190,7 +1212,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
1212
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1213
die("unable to read files to diff");
1214
1193
- if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) {
1215
+ if (file_is_binary(one) || file_is_binary(two)) {
1216
data->is_binary = 1;
1217
data->added = mf2.size;
1218
data->deleted = mf1.size;
@@ -1228,7 +1250,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
1250
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1251
die("unable to read files to diff");
1252
1231
- if (mmfile_is_binary(&mf2))
1253
+ if (file_is_binary(two))
1254
return;
1255
else {
1256
/* Crazy xdl interfaces.. */
@@ -1805,8 +1827,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
1827
1828
if (o->binary) {
1829
mmfile_t mf;
1808
- if ((!fill_mmfile(&mf, one) && mmfile_is_binary(&mf)) ||
1809
- (!fill_mmfile(&mf, two) && mmfile_is_binary(&mf)))
1830
+ if ((!fill_mmfile(&mf, one) && file_is_binary(one)) ||
1831
+ (!fill_mmfile(&mf, two) && file_is_binary(two)))
1832
abbrev = 40;
1833
}
1834
len += snprintf(msg + len, sizeof(msg) - len,
@@ -2701,7 +2723,7 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2723
return error("unable to read files to diff");
2724
2725
/* Maybe hash p->two? into the patch id? */
2704
- if (mmfile_is_binary(&mf2))
2726
+ if (file_is_binary(p->two))
2727
continue;
2728
2729
len1 = remove_space(p->one->path, strlen(p->one->path));