Skip to content

Commit 355e76a

Browse files
Junio C HamanoLinus Torvalds
authored andcommitted
[PATCH] Tweak count-delta interface
Make it return copied source and insertion separately, so that later implementation of heuristics can use them more flexibly. This does not change the heuristics implemented in diffcore-rename nor diffcore-break in any way. Signed-off-by: Junio C Hamano <junkio@cox.net> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 5b86040 commit 355e76a

File tree

5 files changed

+40
-25
lines changed

5 files changed

+40
-25
lines changed

count-delta.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@ static unsigned long get_hdr_size(const unsigned char **datap)
2929
/*
3030
* NOTE. We do not _interpret_ delta fully. As an approximation, we
3131
* just count the number of bytes that are copied from the source, and
32-
* the number of literal data bytes that are inserted. Number of
33-
* bytes that are _not_ copied from the source is deletion, and number
34-
* of inserted literal bytes are addition, so sum of them is what we
35-
* return. xdelta can express an edit that copies data inside of the
36-
* destination which originally came from the source. We do not count
37-
* that in the following routine, so we are undercounting the source
38-
* material that remains in the final output that way.
32+
* the number of literal data bytes that are inserted.
33+
*
34+
* Number of bytes that are _not_ copied from the source is deletion,
35+
* and number of inserted literal bytes are addition, so sum of them
36+
* is the extent of damage. xdelta can express an edit that copies
37+
* data inside of the destination which originally came from the
38+
* source. We do not count that in the following routine, so we are
39+
* undercounting the source material that remains in the final output
40+
* that way.
3941
*/
40-
unsigned long count_delta(void *delta_buf, unsigned long delta_size)
42+
int count_delta(void *delta_buf, unsigned long delta_size,
43+
unsigned long *src_copied, unsigned long *literal_added)
4144
{
4245
unsigned long copied_from_source, added_literal;
4346
const unsigned char *data, *top;
@@ -46,7 +49,7 @@ unsigned long count_delta(void *delta_buf, unsigned long delta_size)
4649

4750
/* the smallest delta size possible is 6 bytes */
4851
if (delta_size < 6)
49-
return UINT_MAX;
52+
return -1;
5053

5154
data = delta_buf;
5255
top = delta_buf + delta_size;
@@ -83,13 +86,12 @@ unsigned long count_delta(void *delta_buf, unsigned long delta_size)
8386

8487
/* sanity check */
8588
if (data != top || out != dst_size)
86-
return UINT_MAX;
89+
return -1;
8790

8891
/* delete size is what was _not_ copied from source.
8992
* edit size is that and literal additions.
9093
*/
91-
if (src_size + added_literal < copied_from_source)
92-
/* we ended up overcounting and underflowed */
93-
return 0;
94-
return (src_size - copied_from_source) + added_literal;
94+
*src_copied = copied_from_source;
95+
*literal_added = added_literal;
96+
return 0;
9597
}

count-delta.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#ifndef COUNT_DELTA_H
55
#define COUNT_DELTA_H
66

7-
unsigned long count_delta(void *, unsigned long);
7+
int count_delta(void *, unsigned long,
8+
unsigned long *src_copied, unsigned long *literal_added);
89

910
#endif

diffcore-break.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static int very_different(struct diff_filespec *src,
2323
* want to get the filepair broken.
2424
*/
2525
void *delta;
26-
unsigned long delta_size, base_size;
26+
unsigned long delta_size, base_size, src_copied, literal_added;
2727

2828
if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
2929
return 0; /* leave symlink rename alone */
@@ -61,10 +61,17 @@ static int very_different(struct diff_filespec *src,
6161
return MAX_SCORE;
6262

6363
/* Estimate the edit size by interpreting delta. */
64-
delta_size = count_delta(delta, delta_size);
64+
if (count_delta(delta, delta_size, &src_copied, &literal_added)) {
65+
free(delta);
66+
return 0;
67+
}
6568
free(delta);
66-
if (delta_size == UINT_MAX)
67-
return 0; /* error in delta computation */
69+
70+
/* Extent of damage */
71+
if (src->size + literal_added < src_copied)
72+
delta_size = 0;
73+
else
74+
delta_size = (src->size - src_copied) + literal_added;
6875

6976
if (base_size < delta_size)
7077
return MAX_SCORE;

diffcore-rename.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static int estimate_similarity(struct diff_filespec *src,
135135
* call into this function in that case.
136136
*/
137137
void *delta;
138-
unsigned long delta_size, base_size;
138+
unsigned long delta_size, base_size, src_copied, literal_added;
139139
int score;
140140

141141
/* We deal only with regular files. Symlink renames are handled
@@ -174,10 +174,17 @@ static int estimate_similarity(struct diff_filespec *src,
174174
return 0;
175175

176176
/* Estimate the edit size by interpreting delta. */
177-
delta_size = count_delta(delta, delta_size);
178-
free(delta);
179-
if (delta_size == UINT_MAX)
177+
if (count_delta(delta, delta_size, &src_copied, &literal_added)) {
178+
free(delta);
180179
return 0;
180+
}
181+
free(delta);
182+
183+
/* Extent of damage */
184+
if (src->size + literal_added < src_copied)
185+
delta_size = 0;
186+
else
187+
delta_size = (src->size - src_copied) + literal_added;
181188

182189
/*
183190
* Now we will give some score to it. 100% edit gets 0 points

diffcore.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#define DEFAULT_RENAME_SCORE 30000 /* rename/copy similarity minimum (50%) */
1313
#define DEFAULT_BREAK_SCORE 59400 /* minimum for break to happen (99%)*/
1414

15-
#define RENAME_DST_MATCHED 01
16-
1715
struct diff_filespec {
1816
unsigned char sha1[20];
1917
char *path;

0 commit comments

Comments
 (0)