Skip to content

Commit 43c1b6e

Browse files
committed
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
2 parents 4e530c5 + 3a4d676 commit 43c1b6e

9 files changed

+46
-45
lines changed

builtin/describe.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static inline struct commit_name *find_commit_name(const unsigned char *peeled)
6363
return n;
6464
}
6565

66-
static int set_util(void *chain)
66+
static int set_util(void *chain, void *data)
6767
{
6868
struct commit_name *n;
6969
for (n = chain; n; n = n->next) {
@@ -289,7 +289,7 @@ static void describe(const char *arg, int last_one)
289289
fprintf(stderr, "searching to describe %s\n", arg);
290290

291291
if (!have_util) {
292-
for_each_hash(&names, set_util);
292+
for_each_hash(&names, set_util, NULL);
293293
have_util = 1;
294294
}
295295

diffcore-rename.c

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static int estimate_similarity(struct diff_filespec *src,
170170
* and the final score computation below would not have a
171171
* divide-by-zero issue.
172172
*/
173-
if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
173+
if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
174174
return 0;
175175

176176
if (!src->cnt_data && diff_populate_filespec(src, 0))
@@ -247,7 +247,8 @@ struct file_similarity {
247247
};
248248

249249
static int find_identical_files(struct file_similarity *src,
250-
struct file_similarity *dst)
250+
struct file_similarity *dst,
251+
struct diff_options *options)
251252
{
252253
int renames = 0;
253254

@@ -277,6 +278,8 @@ static int find_identical_files(struct file_similarity *src,
277278
}
278279
/* Give higher scores to sources that haven't been used already */
279280
score = !source->rename_used;
281+
if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
282+
continue;
280283
score += basename_same(source, target);
281284
if (score > best_score) {
282285
best = p;
@@ -306,11 +309,12 @@ static void free_similarity_list(struct file_similarity *p)
306309
}
307310
}
308311

309-
static int find_same_files(void *ptr)
312+
static int find_same_files(void *ptr, void *data)
310313
{
311314
int ret;
312315
struct file_similarity *p = ptr;
313316
struct file_similarity *src = NULL, *dst = NULL;
317+
struct diff_options *options = data;
314318

315319
/* Split the hash list up into sources and destinations */
316320
do {
@@ -329,7 +333,7 @@ static int find_same_files(void *ptr)
329333
* If we have both sources *and* destinations, see if
330334
* we can match them up
331335
*/
332-
ret = (src && dst) ? find_identical_files(src, dst) : 0;
336+
ret = (src && dst) ? find_identical_files(src, dst, options) : 0;
333337

334338
/* Free the hashes and return the number of renames found */
335339
free_similarity_list(src);
@@ -377,7 +381,7 @@ static void insert_file_table(struct hash_table *table, int src_dst, int index,
377381
* and then during the second round we try to match
378382
* cache-dirty entries as well.
379383
*/
380-
static int find_exact_renames(void)
384+
static int find_exact_renames(struct diff_options *options)
381385
{
382386
int i;
383387
struct hash_table file_table;
@@ -390,7 +394,7 @@ static int find_exact_renames(void)
390394
insert_file_table(&file_table, 1, i, rename_dst[i].two);
391395

392396
/* Find the renames */
393-
i = for_each_hash(&file_table, find_same_files);
397+
i = for_each_hash(&file_table, find_same_files, options);
394398

395399
/* .. and free the hash data structure */
396400
free_hash(&file_table);
@@ -414,6 +418,27 @@ static void record_if_better(struct diff_score m[], struct diff_score *o)
414418
m[worst] = *o;
415419
}
416420

421+
static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, int copies)
422+
{
423+
int count = 0, i;
424+
425+
for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
426+
struct diff_rename_dst *dst;
427+
428+
if ((mx[i].dst < 0) ||
429+
(mx[i].score < minimum_score))
430+
break; /* there is no more usable pair. */
431+
dst = &rename_dst[mx[i].dst];
432+
if (dst->pair)
433+
continue; /* already done, either exact or fuzzy. */
434+
if (!copies && rename_src[mx[i].src].one->rename_used)
435+
continue;
436+
record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
437+
count++;
438+
}
439+
return count;
440+
}
441+
417442
void diffcore_rename(struct diff_options *options)
418443
{
419444
int detect_rename = options->detect_rename;
@@ -467,7 +492,7 @@ void diffcore_rename(struct diff_options *options)
467492
* We really want to cull the candidates list early
468493
* with cheap tests in order to avoid doing deltas.
469494
*/
470-
rename_count = find_exact_renames();
495+
rename_count = find_exact_renames(options);
471496

472497
/* Did we only want exact renames? */
473498
if (minimum_score == MAX_SCORE)
@@ -536,33 +561,9 @@ void diffcore_rename(struct diff_options *options)
536561
/* cost matrix sorted by most to least similar pair */
537562
qsort(mx, dst_cnt * NUM_CANDIDATE_PER_DST, sizeof(*mx), score_compare);
538563

539-
for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
540-
struct diff_rename_dst *dst;
541-
542-
if ((mx[i].dst < 0) ||
543-
(mx[i].score < minimum_score))
544-
break; /* there is no more usable pair. */
545-
dst = &rename_dst[mx[i].dst];
546-
if (dst->pair)
547-
continue; /* already done, either exact or fuzzy. */
548-
if (rename_src[mx[i].src].one->rename_used)
549-
continue;
550-
record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
551-
rename_count++;
552-
}
553-
554-
for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
555-
struct diff_rename_dst *dst;
556-
557-
if ((mx[i].dst < 0) ||
558-
(mx[i].score < minimum_score))
559-
break; /* there is no more usable pair. */
560-
dst = &rename_dst[mx[i].dst];
561-
if (dst->pair)
562-
continue; /* already done, either exact or fuzzy. */
563-
record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
564-
rename_count++;
565-
}
564+
rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
565+
if (detect_rename == DIFF_DETECT_COPY)
566+
rename_count += find_renames(mx, dst_cnt, minimum_score, 1);
566567
free(mx);
567568

568569
cleanup:

hash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table)
8181
return insert_hash_entry(hash, ptr, table);
8282
}
8383

84-
int for_each_hash(const struct hash_table *table, int (*fn)(void *))
84+
int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data)
8585
{
8686
int sum = 0;
8787
unsigned int i;
@@ -92,7 +92,7 @@ int for_each_hash(const struct hash_table *table, int (*fn)(void *))
9292
void *ptr = array->ptr;
9393
array++;
9494
if (ptr) {
95-
int val = fn(ptr);
95+
int val = fn(ptr, data);
9696
if (val < 0)
9797
return val;
9898
sum += val;

hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct hash_table {
3030

3131
extern void *lookup_hash(unsigned int hash, const struct hash_table *table);
3232
extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table);
33-
extern int for_each_hash(const struct hash_table *table, int (*fn)(void *));
33+
extern int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data);
3434
extern void free_hash(struct hash_table *table);
3535

3636
static inline void init_hash(struct hash_table *table)

t/t4003-diff-rename-1.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test_expect_success \
2929
# copy-and-edit one, and rename-and-edit the other. We do not say
3030
# anything about rezrov.
3131

32-
GIT_DIFF_OPTS=--unified=0 git diff-index -M -p $tree >current
32+
GIT_DIFF_OPTS=--unified=0 git diff-index -C -p $tree >current
3333
cat >expected <<\EOF
3434
diff --git a/COPYING b/COPYING.1
3535
copy from COPYING

t/t4004-diff-rename-symlink.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test_expect_success SYMLINKS \
3535
# a new creation.
3636

3737
test_expect_success SYMLINKS 'setup diff output' "
38-
GIT_DIFF_OPTS=--unified=0 git diff-index -M -p $tree >current &&
38+
GIT_DIFF_OPTS=--unified=0 git diff-index -C -p $tree >current &&
3939
cat >expected <<\EOF
4040
diff --git a/bozbar b/bozbar
4141
new file mode 120000

t/t4005-diff-rename-2.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test_expect_success \
2929
# and COPYING.2 are based on COPYING, and do not say anything about
3030
# rezrov.
3131

32-
git diff-index -M $tree >current
32+
git diff-index -C $tree >current
3333

3434
cat >expected <<\EOF
3535
:100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0603b3238a076dc6c8022aedc6648fa523a17178 C1234 COPYING COPYING.1

t/t4008-diff-break-rewrite.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ test_expect_success \
173173
'compare_diff_raw expected current'
174174

175175
test_expect_success \
176-
'run diff with -B -M' \
177-
'git diff-index -B -M "$tree" >current'
176+
'run diff with -B -C' \
177+
'git diff-index -B -C "$tree" >current'
178178

179179
cat >expected <<\EOF
180180
:100644 100644 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 08bb2fb671deff4c03a4d4a0a1315dff98d5732c C095 file0 file1

t/t4009-diff-rename-4.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test_expect_success \
2929
# and COPYING.2 are based on COPYING, and do not say anything about
3030
# rezrov.
3131

32-
git diff-index -z -M $tree >current
32+
git diff-index -z -C $tree >current
3333

3434
cat >expected <<\EOF
3535
:100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0603b3238a076dc6c8022aedc6648fa523a17178 C1234

0 commit comments

Comments
 (0)