Merge branch 'lt/rename-no-extra-copy-detection' into maint

* lt/rename-no-extra-copy-detection:
  diffcore-rename: improve estimate_similarity() heuristics
  diffcore-rename: properly honor the difference between -M and -C
  for_each_hash: allow passing a 'void *data' pointer to callback
This commit is contained in:
Junio C Hamano 2011-03-16 16:47:26 -07:00
commit 43c1b6efb8
9 changed files with 46 additions and 45 deletions

View File

@ -63,7 +63,7 @@ static inline struct commit_name *find_commit_name(const unsigned char *peeled)
return n;
}
static int set_util(void *chain)
static int set_util(void *chain, void *data)
{
struct commit_name *n;
for (n = chain; n; n = n->next) {
@ -289,7 +289,7 @@ static void describe(const char *arg, int last_one)
fprintf(stderr, "searching to describe %s\n", arg);
if (!have_util) {
for_each_hash(&names, set_util);
for_each_hash(&names, set_util, NULL);
have_util = 1;
}

View File

@ -170,7 +170,7 @@ static int estimate_similarity(struct diff_filespec *src,
* and the final score computation below would not have a
* divide-by-zero issue.
*/
if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
return 0;
if (!src->cnt_data && diff_populate_filespec(src, 0))
@ -247,7 +247,8 @@ struct file_similarity {
};
static int find_identical_files(struct file_similarity *src,
struct file_similarity *dst)
struct file_similarity *dst,
struct diff_options *options)
{
int renames = 0;
@ -277,6 +278,8 @@ static int find_identical_files(struct file_similarity *src,
}
/* Give higher scores to sources that haven't been used already */
score = !source->rename_used;
if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
continue;
score += basename_same(source, target);
if (score > best_score) {
best = p;
@ -306,11 +309,12 @@ static void free_similarity_list(struct file_similarity *p)
}
}
static int find_same_files(void *ptr)
static int find_same_files(void *ptr, void *data)
{
int ret;
struct file_similarity *p = ptr;
struct file_similarity *src = NULL, *dst = NULL;
struct diff_options *options = data;
/* Split the hash list up into sources and destinations */
do {
@ -329,7 +333,7 @@ static int find_same_files(void *ptr)
* If we have both sources *and* destinations, see if
* we can match them up
*/
ret = (src && dst) ? find_identical_files(src, dst) : 0;
ret = (src && dst) ? find_identical_files(src, dst, options) : 0;
/* Free the hashes and return the number of renames found */
free_similarity_list(src);
@ -377,7 +381,7 @@ static void insert_file_table(struct hash_table *table, int src_dst, int index,
* and then during the second round we try to match
* cache-dirty entries as well.
*/
static int find_exact_renames(void)
static int find_exact_renames(struct diff_options *options)
{
int i;
struct hash_table file_table;
@ -390,7 +394,7 @@ static int find_exact_renames(void)
insert_file_table(&file_table, 1, i, rename_dst[i].two);
/* Find the renames */
i = for_each_hash(&file_table, find_same_files);
i = for_each_hash(&file_table, find_same_files, options);
/* .. and free the hash data structure */
free_hash(&file_table);
@ -414,6 +418,27 @@ static void record_if_better(struct diff_score m[], struct diff_score *o)
m[worst] = *o;
}
static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, int copies)
{
int count = 0, i;
for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
struct diff_rename_dst *dst;
if ((mx[i].dst < 0) ||
(mx[i].score < minimum_score))
break; /* there is no more usable pair. */
dst = &rename_dst[mx[i].dst];
if (dst->pair)
continue; /* already done, either exact or fuzzy. */
if (!copies && rename_src[mx[i].src].one->rename_used)
continue;
record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
count++;
}
return count;
}
void diffcore_rename(struct diff_options *options)
{
int detect_rename = options->detect_rename;
@ -467,7 +492,7 @@ void diffcore_rename(struct diff_options *options)
* We really want to cull the candidates list early
* with cheap tests in order to avoid doing deltas.
*/
rename_count = find_exact_renames();
rename_count = find_exact_renames(options);
/* Did we only want exact renames? */
if (minimum_score == MAX_SCORE)
@ -536,33 +561,9 @@ void diffcore_rename(struct diff_options *options)
/* cost matrix sorted by most to least similar pair */
qsort(mx, dst_cnt * NUM_CANDIDATE_PER_DST, sizeof(*mx), score_compare);
for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
struct diff_rename_dst *dst;
if ((mx[i].dst < 0) ||
(mx[i].score < minimum_score))
break; /* there is no more usable pair. */
dst = &rename_dst[mx[i].dst];
if (dst->pair)
continue; /* already done, either exact or fuzzy. */
if (rename_src[mx[i].src].one->rename_used)
continue;
record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
rename_count++;
}
for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
struct diff_rename_dst *dst;
if ((mx[i].dst < 0) ||
(mx[i].score < minimum_score))
break; /* there is no more usable pair. */
dst = &rename_dst[mx[i].dst];
if (dst->pair)
continue; /* already done, either exact or fuzzy. */
record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
rename_count++;
}
rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
if (detect_rename == DIFF_DETECT_COPY)
rename_count += find_renames(mx, dst_cnt, minimum_score, 1);
free(mx);
cleanup:

4
hash.c
View File

@ -81,7 +81,7 @@ void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table)
return insert_hash_entry(hash, ptr, table);
}
int for_each_hash(const struct hash_table *table, int (*fn)(void *))
int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data)
{
int sum = 0;
unsigned int i;
@ -92,7 +92,7 @@ int for_each_hash(const struct hash_table *table, int (*fn)(void *))
void *ptr = array->ptr;
array++;
if (ptr) {
int val = fn(ptr);
int val = fn(ptr, data);
if (val < 0)
return val;
sum += val;

2
hash.h
View File

@ -30,7 +30,7 @@ struct hash_table {
extern void *lookup_hash(unsigned int hash, const struct hash_table *table);
extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table);
extern int for_each_hash(const struct hash_table *table, int (*fn)(void *));
extern int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data);
extern void free_hash(struct hash_table *table);
static inline void init_hash(struct hash_table *table)

View File

@ -29,7 +29,7 @@ test_expect_success \
# copy-and-edit one, and rename-and-edit the other. We do not say
# anything about rezrov.
GIT_DIFF_OPTS=--unified=0 git diff-index -M -p $tree >current
GIT_DIFF_OPTS=--unified=0 git diff-index -C -p $tree >current
cat >expected <<\EOF
diff --git a/COPYING b/COPYING.1
copy from COPYING

View File

@ -35,7 +35,7 @@ test_expect_success SYMLINKS \
# a new creation.
test_expect_success SYMLINKS 'setup diff output' "
GIT_DIFF_OPTS=--unified=0 git diff-index -M -p $tree >current &&
GIT_DIFF_OPTS=--unified=0 git diff-index -C -p $tree >current &&
cat >expected <<\EOF
diff --git a/bozbar b/bozbar
new file mode 120000

View File

@ -29,7 +29,7 @@ test_expect_success \
# and COPYING.2 are based on COPYING, and do not say anything about
# rezrov.
git diff-index -M $tree >current
git diff-index -C $tree >current
cat >expected <<\EOF
:100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0603b3238a076dc6c8022aedc6648fa523a17178 C1234 COPYING COPYING.1

View File

@ -173,8 +173,8 @@ test_expect_success \
'compare_diff_raw expected current'
test_expect_success \
'run diff with -B -M' \
'git diff-index -B -M "$tree" >current'
'run diff with -B -C' \
'git diff-index -B -C "$tree" >current'
cat >expected <<\EOF
:100644 100644 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 08bb2fb671deff4c03a4d4a0a1315dff98d5732c C095 file0 file1

View File

@ -29,7 +29,7 @@ test_expect_success \
# and COPYING.2 are based on COPYING, and do not say anything about
# rezrov.
git diff-index -z -M $tree >current
git diff-index -z -C $tree >current
cat >expected <<\EOF
:100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0603b3238a076dc6c8022aedc6648fa523a17178 C1234