Merge branch 'jc/diffcore'

* jc/diffcore:
  diffcore-delta.c: Ignore CR in CRLF for text files
  diffcore-delta.c: update the comment on the algorithm.
  diffcore_filespec: add is_binary
  diffcore_count_changes: pass diffcore_filespec
This commit is contained in:
Junio C Hamano 2007-07-02 01:45:12 -07:00
commit e1bc8dc66d
6 changed files with 76 additions and 23 deletions

16
diff.c
View File

@ -3005,6 +3005,22 @@ void diffcore_std(struct diff_options *options)
{ {
if (options->quiet) if (options->quiet)
return; return;
/*
* break/rename count similarity differently depending on
* the binary-ness.
*/
if ((options->break_opt != -1) || (options->detect_rename)) {
struct diff_queue_struct *q = &diff_queued_diff;
int i;
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
p->one->is_binary = file_is_binary(p->one);
p->two->is_binary = file_is_binary(p->two);
}
}
if (options->break_opt != -1) if (options->break_opt != -1)
diffcore_break(options->break_opt); diffcore_break(options->break_opt);
if (options->detect_rename) if (options->detect_rename)

View File

@ -66,8 +66,7 @@ static int should_break(struct diff_filespec *src,
if (base_size < MINIMUM_BREAK_SIZE) if (base_size < MINIMUM_BREAK_SIZE)
return 0; /* we do not break too small filepair */ return 0; /* we do not break too small filepair */
if (diffcore_count_changes(src->data, src->size, if (diffcore_count_changes(src, dst,
dst->data, dst->size,
NULL, NULL, NULL, NULL,
0, 0,
&src_copied, &literal_added)) &src_copied, &literal_added))

View File

@ -5,23 +5,20 @@
/* /*
* Idea here is very simple. * Idea here is very simple.
* *
* We have total of (sz-N+1) N-byte overlapping sequences in buf whose * Almost all data we are interested in are text, but sometimes we have
* size is sz. If the same N-byte sequence appears in both source and * to deal with binary data. So we cut them into chunks delimited by
* destination, we say the byte that starts that sequence is shared * LF byte, or 64-byte sequence, whichever comes first, and hash them.
* between them (i.e. copied from source to destination).
* *
* For each possible N-byte sequence, if the source buffer has more * For those chunks, if the source buffer has more instances of it
* instances of it than the destination buffer, that means the * than the destination buffer, that means the difference are the
* difference are the number of bytes not copied from source to * number of bytes not copied from source to destination. If the
* destination. If the counts are the same, everything was copied * counts are the same, everything was copied from source to
* from source to destination. If the destination has more, * destination. If the destination has more, everything was copied,
* everything was copied, and destination added more. * and destination added more.
* *
* We are doing an approximation so we do not really have to waste * We are doing an approximation so we do not really have to waste
* memory by actually storing the sequence. We just hash them into * memory by actually storing the sequence. We just hash them into
* somewhere around 2^16 hashbuckets and count the occurrences. * somewhere around 2^16 hashbuckets and count the occurrences.
*
* The length of the sequence is arbitrarily set to 8 for now.
*/ */
/* Wild guess at the initial hash size */ /* Wild guess at the initial hash size */
@ -125,11 +122,14 @@ static struct spanhash_top *add_spanhash(struct spanhash_top *top,
} }
} }
static struct spanhash_top *hash_chars(unsigned char *buf, unsigned int sz) static struct spanhash_top *hash_chars(struct diff_filespec *one)
{ {
int i, n; int i, n;
unsigned int accum1, accum2, hashval; unsigned int accum1, accum2, hashval;
struct spanhash_top *hash; struct spanhash_top *hash;
unsigned char *buf = one->data;
unsigned int sz = one->size;
int is_text = !one->is_binary;
i = INITIAL_HASH_SIZE; i = INITIAL_HASH_SIZE;
hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i)); hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));
@ -143,6 +143,11 @@ static struct spanhash_top *hash_chars(unsigned char *buf, unsigned int sz)
unsigned int c = *buf++; unsigned int c = *buf++;
unsigned int old_1 = accum1; unsigned int old_1 = accum1;
sz--; sz--;
/* Ignore CR in CRLF sequence if text */
if (is_text && c == '\r' && sz && *buf == '\n')
continue;
accum1 = (accum1 << 7) ^ (accum2 >> 25); accum1 = (accum1 << 7) ^ (accum2 >> 25);
accum2 = (accum2 << 7) ^ (old_1 >> 25); accum2 = (accum2 << 7) ^ (old_1 >> 25);
accum1 += c; accum1 += c;
@ -156,8 +161,8 @@ static struct spanhash_top *hash_chars(unsigned char *buf, unsigned int sz)
return hash; return hash;
} }
int diffcore_count_changes(void *src, unsigned long src_size, int diffcore_count_changes(struct diff_filespec *src,
void *dst, unsigned long dst_size, struct diff_filespec *dst,
void **src_count_p, void **src_count_p,
void **dst_count_p, void **dst_count_p,
unsigned long delta_limit, unsigned long delta_limit,
@ -172,14 +177,14 @@ int diffcore_count_changes(void *src, unsigned long src_size,
if (src_count_p) if (src_count_p)
src_count = *src_count_p; src_count = *src_count_p;
if (!src_count) { if (!src_count) {
src_count = hash_chars(src, src_size); src_count = hash_chars(src);
if (src_count_p) if (src_count_p)
*src_count_p = src_count; *src_count_p = src_count;
} }
if (dst_count_p) if (dst_count_p)
dst_count = *dst_count_p; dst_count = *dst_count_p;
if (!dst_count) { if (!dst_count) {
dst_count = hash_chars(dst, dst_size); dst_count = hash_chars(dst);
if (dst_count_p) if (dst_count_p)
*dst_count_p = dst_count; *dst_count_p = dst_count;
} }

View File

@ -190,8 +190,7 @@ static int estimate_similarity(struct diff_filespec *src,
delta_limit = (unsigned long) delta_limit = (unsigned long)
(base_size * (MAX_SCORE-minimum_score) / MAX_SCORE); (base_size * (MAX_SCORE-minimum_score) / MAX_SCORE);
if (diffcore_count_changes(src->data, src->size, if (diffcore_count_changes(src, dst,
dst->data, dst->size,
&src->cnt_data, &dst->cnt_data, &src->cnt_data, &dst->cnt_data,
delta_limit, delta_limit,
&src_copied, &literal_added)) &src_copied, &literal_added))

View File

@ -37,6 +37,7 @@ struct diff_filespec {
#define DIFF_FILE_VALID(spec) (((spec)->mode) != 0) #define DIFF_FILE_VALID(spec) (((spec)->mode) != 0)
unsigned should_free : 1; /* data should be free()'ed */ unsigned should_free : 1; /* data should be free()'ed */
unsigned should_munmap : 1; /* data should be munmap()'ed */ unsigned should_munmap : 1; /* data should be munmap()'ed */
unsigned is_binary : 1; /* data should be considered "binary" */
}; };
extern struct diff_filespec *alloc_filespec(const char *); extern struct diff_filespec *alloc_filespec(const char *);
@ -103,8 +104,8 @@ void diff_debug_queue(const char *, struct diff_queue_struct *);
#define diff_debug_queue(a,b) do {} while(0) #define diff_debug_queue(a,b) do {} while(0)
#endif #endif
extern int diffcore_count_changes(void *src, unsigned long src_size, extern int diffcore_count_changes(struct diff_filespec *src,
void *dst, unsigned long dst_size, struct diff_filespec *dst,
void **src_count_p, void **src_count_p,
void **dst_count_p, void **dst_count_p,
unsigned long delta_limit, unsigned long delta_limit,

33
t/t0022-crlf-rename.sh Executable file
View File

@ -0,0 +1,33 @@
#!/bin/sh
test_description='ignore CR in CRLF sequence while computing similiarity'
. ./test-lib.sh
test_expect_success setup '
cat ../t0022-crlf-rename.sh >sample &&
git add sample &&
test_tick &&
git commit -m Initial &&
sed -e "s/\$/ /" ../t0022-crlf-rename.sh >elpmas &&
git add elpmas &&
rm -f sample &&
test_tick &&
git commit -a -m Second
'
test_expect_success 'diff -M' '
git diff-tree -M -r --name-status HEAD^ HEAD |
sed -e "s/R[0-9]*/RNUM/" >actual &&
echo "RNUM sample elpmas" >expect &&
diff -u expect actual
'
test_done