@matheusbsbs / matheus-bagliano / commits / b9905fed7a
Fork 0

diffcore-delta.c: Ignore CR in CRLF for text files

This ignores CR byte in CRLF sequence in text file when computing similarity of two blobs. Usually this should not matter as nobody sane would be checking in a file with CRLF line endings to the repository (they would use autocrlf so that the repository copy would have LF line endings). Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 28, 2007 at 23:14 UTC b9905fed7a028cc9749cf8ad479cbb07940c8638
2 files changed +44 -3
diffcore-delta.c
+11 -3
@@ -122,11 +122,14 @@ static struct spanhash_top *add_spanhash(struct spanhash_top *top,
122 }
123 }
124
125 -static struct spanhash_top *hash_chars(unsigned char *buf, unsigned int sz)
125 +static struct spanhash_top *hash_chars(struct diff_filespec *one)
126 {
127 int i, n;
128 unsigned int accum1, accum2, hashval;
129 struct spanhash_top *hash;
130 + unsigned char *buf = one->data;
131 + unsigned int sz = one->size;
132 + int is_text = !one->is_binary;
133
134 i = INITIAL_HASH_SIZE;
135 hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));
@@ -140,6 +143,11 @@ static struct spanhash_top *hash_chars(unsigned char *buf, unsigned int sz)
143 unsigned int c = *buf++;
144 unsigned int old_1 = accum1;
145 sz--;
146 +
147 + /* Ignore CR in CRLF sequence if text */
148 + if (is_text && c == '\r' && sz && *buf == '\n')
149 + continue;
150 +
151 accum1 = (accum1 << 7) ^ (accum2 >> 25);
152 accum2 = (accum2 << 7) ^ (old_1 >> 25);
153 accum1 += c;
@@ -169,14 +177,14 @@ int diffcore_count_changes(struct diff_filespec *src,
177 if (src_count_p)
178 src_count = *src_count_p;
179 if (!src_count) {
172 - src_count = hash_chars(src->data, src->size);
180 + src_count = hash_chars(src);
181 if (src_count_p)
182 *src_count_p = src_count;
183 }
184 if (dst_count_p)
185 dst_count = *dst_count_p;
186 if (!dst_count) {
179 - dst_count = hash_chars(dst->data, dst->size);
187 + dst_count = hash_chars(dst);
188 if (dst_count_p)
189 *dst_count_p = dst_count;
190 }
t/t0022-crlf-rename.sh new
+33
@@ -0,0 +1,33 @@
1 +#!/bin/sh
2 +
3 +test_description='ignore CR in CRLF sequence while computing similiarity'
4 +
5 +. ./test-lib.sh
6 +
7 +test_expect_success setup '
8 +
9 + cat ../t0022-crlf-rename.sh >sample &&
10 + git add sample &&
11 +
12 + test_tick &&
13 + git commit -m Initial &&
14 +
15 + sed -e "s/\$/ /" ../t0022-crlf-rename.sh >elpmas &&
16 + git add elpmas &&
17 + rm -f sample &&
18 +
19 + test_tick &&
20 + git commit -a -m Second
21 +
22 +'
23 +
24 +test_expect_success 'diff -M' '
25 +
26 + git diff-tree -M -r --name-status HEAD^ HEAD |
27 + sed -e "s/R[0-9]*/RNUM/" >actual &&
28 + echo "RNUM sample elpmas" >expect &&
29 + diff -u expect actual
30 +
31 +'
32 +
33 +test_done