Skip to content

Commit 29a3eef

Browse files
committed
Introduce diff_filespec_is_binary()
This replaces an explicit initialization of filespec->is_binary field used for rename/break followed by direct access to that field with a wrapper function that lazily iniaitlizes and accesses the field. We would add more attribute accesses for the use of diff routines, and it would be better to make this abstraction earlier. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 46f74f0 commit 29a3eef

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

diff.c

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,30 +1102,45 @@ static void setup_diff_attr_check(struct git_attr_check *check)
11021102
{
11031103
static struct git_attr *attr_diff;
11041104

1105-
if (!attr_diff)
1105+
if (!attr_diff) {
11061106
attr_diff = git_attr("diff", 4);
1107-
check->attr = attr_diff;
1107+
}
1108+
check[0].attr = attr_diff;
11081109
}
11091110

1110-
static int file_is_binary(struct diff_filespec *one)
1111+
static void diff_filespec_check_attr(struct diff_filespec *one)
11111112
{
1112-
struct git_attr_check attr_diff_check;
1113+
struct git_attr_check attr_diff_check[1];
11131114

1114-
setup_diff_attr_check(&attr_diff_check);
1115-
if (!git_checkattr(one->path, 1, &attr_diff_check)) {
1116-
const char *value = attr_diff_check.value;
1115+
if (one->checked_attr)
1116+
return;
1117+
1118+
setup_diff_attr_check(attr_diff_check);
1119+
one->is_binary = 0;
1120+
1121+
if (!git_checkattr(one->path, ARRAY_SIZE(attr_diff_check), attr_diff_check)) {
1122+
const char *value;
1123+
1124+
/* binaryness */
1125+
value = attr_diff_check[0].value;
11171126
if (ATTR_TRUE(value))
1118-
return 0;
1127+
;
11191128
else if (ATTR_FALSE(value))
1120-
return 1;
1129+
one->is_binary = 1;
11211130
}
11221131

1123-
if (!one->data) {
1124-
if (!DIFF_FILE_VALID(one))
1125-
return 0;
1132+
if (!one->data && DIFF_FILE_VALID(one))
11261133
diff_populate_filespec(one, 0);
1127-
}
1128-
return buffer_is_binary(one->data, one->size);
1134+
1135+
if (one->data)
1136+
one->is_binary = buffer_is_binary(one->data, one->size);
1137+
1138+
}
1139+
1140+
int diff_filespec_is_binary(struct diff_filespec *one)
1141+
{
1142+
diff_filespec_check_attr(one);
1143+
return one->is_binary;
11291144
}
11301145

11311146
static void builtin_diff(const char *name_a,
@@ -1182,7 +1197,8 @@ static void builtin_diff(const char *name_a,
11821197
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
11831198
die("unable to read files to diff");
11841199

1185-
if (!o->text && (file_is_binary(one) || file_is_binary(two))) {
1200+
if (!o->text &&
1201+
(diff_filespec_is_binary(one) || diff_filespec_is_binary(two))) {
11861202
/* Quite common confusing case */
11871203
if (mf1.size == mf2.size &&
11881204
!memcmp(mf1.ptr, mf2.ptr, mf1.size))
@@ -1260,7 +1276,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
12601276
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
12611277
die("unable to read files to diff");
12621278

1263-
if (file_is_binary(one) || file_is_binary(two)) {
1279+
if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
12641280
data->is_binary = 1;
12651281
data->added = mf2.size;
12661282
data->deleted = mf1.size;
@@ -1302,7 +1318,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
13021318
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
13031319
die("unable to read files to diff");
13041320

1305-
if (file_is_binary(two))
1321+
if (diff_filespec_is_binary(two))
13061322
goto free_and_return;
13071323
else {
13081324
/* Crazy xdl interfaces.. */
@@ -1880,8 +1896,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
18801896

18811897
if (o->binary) {
18821898
mmfile_t mf;
1883-
if ((!fill_mmfile(&mf, one) && file_is_binary(one)) ||
1884-
(!fill_mmfile(&mf, two) && file_is_binary(two)))
1899+
if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) ||
1900+
(!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
18851901
abbrev = 40;
18861902
}
18871903
len += snprintf(msg + len, sizeof(msg) - len,
@@ -2783,7 +2799,7 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
27832799
return error("unable to read files to diff");
27842800

27852801
/* Maybe hash p->two? into the patch id? */
2786-
if (file_is_binary(p->two))
2802+
if (diff_filespec_is_binary(p->two))
27872803
continue;
27882804

27892805
len1 = remove_space(p->one->path, strlen(p->one->path));
@@ -3011,21 +3027,6 @@ void diffcore_std(struct diff_options *options)
30113027
if (options->quiet)
30123028
return;
30133029

3014-
/*
3015-
* break/rename count similarity differently depending on
3016-
* the binary-ness.
3017-
*/
3018-
if ((options->break_opt != -1) || (options->detect_rename)) {
3019-
struct diff_queue_struct *q = &diff_queued_diff;
3020-
int i;
3021-
3022-
for (i = 0; i < q->nr; i++) {
3023-
struct diff_filepair *p = q->queue[i];
3024-
p->one->is_binary = file_is_binary(p->one);
3025-
p->two->is_binary = file_is_binary(p->two);
3026-
}
3027-
}
3028-
30293030
if (options->break_opt != -1)
30303031
diffcore_break(options->break_opt);
30313032
if (options->detect_rename)

diffcore-delta.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static struct spanhash_top *hash_chars(struct diff_filespec *one)
129129
struct spanhash_top *hash;
130130
unsigned char *buf = one->data;
131131
unsigned int sz = one->size;
132-
int is_text = !one->is_binary;
132+
int is_text = !diff_filespec_is_binary(one);
133133

134134
i = INITIAL_HASH_SIZE;
135135
hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));

diffcore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct diff_filespec {
3737
#define DIFF_FILE_VALID(spec) (((spec)->mode) != 0)
3838
unsigned should_free : 1; /* data should be free()'ed */
3939
unsigned should_munmap : 1; /* data should be munmap()'ed */
40+
unsigned checked_attr : 1;
4041
unsigned is_binary : 1; /* data should be considered "binary" */
4142
};
4243

@@ -46,6 +47,7 @@ extern void fill_filespec(struct diff_filespec *, const unsigned char *,
4647

4748
extern int diff_populate_filespec(struct diff_filespec *, int);
4849
extern void diff_free_filespec_data(struct diff_filespec *);
50+
extern int diff_filespec_is_binary(struct diff_filespec *);
4951

5052
struct diff_filepair {
5153
struct diff_filespec *one;

0 commit comments

Comments
 (0)