@matheusbsbs / matheus-bagliano / commits / 29a3eefde1
Fork 0

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>

Junio C Hamano committed Jul 6, 2007 at 00:18 UTC 29a3eefde111f6a24292163c4308f00ab3572627
3 files changed +39 -36
diff.c
+36 -35
@@ -1102,30 +1102,45 @@ static void setup_diff_attr_check(struct git_attr_check *check)
1102 {
1103 static struct git_attr *attr_diff;
1104
1105 - if (!attr_diff)
1105 + if (!attr_diff) {
1106 attr_diff = git_attr("diff", 4);
1107 - check->attr = attr_diff;
1107 + }
1108 + check[0].attr = attr_diff;
1109 }
1110
1110 -static int file_is_binary(struct diff_filespec *one)
1111 +static void diff_filespec_check_attr(struct diff_filespec *one)
1112 {
1112 - struct git_attr_check attr_diff_check;
1113 + struct git_attr_check attr_diff_check[1];
1114
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;
1126 if (ATTR_TRUE(value))
1118 - return 0;
1127 + ;
1128 else if (ATTR_FALSE(value))
1120 - return 1;
1129 + one->is_binary = 1;
1130 }
1131
1123 - if (!one->data) {
1124 - if (!DIFF_FILE_VALID(one))
1125 - return 0;
1132 + if (!one->data && DIFF_FILE_VALID(one))
1133 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;
1144 }
1145
1146 static void builtin_diff(const char *name_a,
@@ -1182,7 +1197,8 @@ static void builtin_diff(const char *name_a,
1197 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1198 die("unable to read files to diff");
1199
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))) {
1202 /* Quite common confusing case */
1203 if (mf1.size == mf2.size &&
1204 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
@@ -1260,7 +1276,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
1276 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1277 die("unable to read files to diff");
1278
1263 - if (file_is_binary(one) || file_is_binary(two)) {
1279 + if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
1280 data->is_binary = 1;
1281 data->added = mf2.size;
1282 data->deleted = mf1.size;
@@ -1302,7 +1318,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
1318 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1319 die("unable to read files to diff");
1320
1305 - if (file_is_binary(two))
1321 + if (diff_filespec_is_binary(two))
1322 goto free_and_return;
1323 else {
1324 /* Crazy xdl interfaces.. */
@@ -1880,8 +1896,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
1896
1897 if (o->binary) {
1898 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)))
1901 abbrev = 40;
1902 }
1903 len += snprintf(msg + len, sizeof(msg) - len,
@@ -2783,7 +2799,7 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2799 return error("unable to read files to diff");
2800
2801 /* Maybe hash p->two? into the patch id? */
2786 - if (file_is_binary(p->two))
2802 + if (diff_filespec_is_binary(p->two))
2803 continue;
2804
2805 len1 = remove_space(p->one->path, strlen(p->one->path));
@@ -3011,21 +3027,6 @@ void diffcore_std(struct diff_options *options)
3027 if (options->quiet)
3028 return;
3029
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 -
3030 if (options->break_opt != -1)
3031 diffcore_break(options->break_opt);
3032 if (options->detect_rename)
diffcore-delta.c
+1 -1
@@ -129,7 +129,7 @@ static struct spanhash_top *hash_chars(struct diff_filespec *one)
129 struct spanhash_top *hash;
130 unsigned char *buf = one->data;
131 unsigned int sz = one->size;
132 - int is_text = !one->is_binary;
132 + int is_text = !diff_filespec_is_binary(one);
133
134 i = INITIAL_HASH_SIZE;
135 hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));
diffcore.h
+2
@@ -37,6 +37,7 @@ struct diff_filespec {
37 #define DIFF_FILE_VALID(spec) (((spec)->mode) != 0)
38 unsigned should_free : 1; /* data should be free()'ed */
39 unsigned should_munmap : 1; /* data should be munmap()'ed */
40 + unsigned checked_attr : 1;
41 unsigned is_binary : 1; /* data should be considered "binary" */
42 };
43
@@ -46,6 +47,7 @@ extern void fill_filespec(struct diff_filespec *, const unsigned char *,
47
48 extern int diff_populate_filespec(struct diff_filespec *, int);
49 extern void diff_free_filespec_data(struct diff_filespec *);
50 +extern int diff_filespec_is_binary(struct diff_filespec *);
51
52 struct diff_filepair {
53 struct diff_filespec *one;