Skip to content

Commit 0940e5f

Browse files
torvaldsgitster
authored andcommitted
diffcore-rename: properly honor the difference between -M and -C
We would allow rename detection to do copy detection even when asked purely for renames. That confuses users, but more importantly it can terminally confuse the recursive merge rename logic. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 11f944d commit 0940e5f

File tree

6 files changed

+32
-33
lines changed

6 files changed

+32
-33
lines changed

diffcore-rename.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ static int find_identical_files(struct file_similarity *src,
278278
}
279279
/* Give higher scores to sources that haven't been used already */
280280
score = !source->rename_used;
281+
if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
282+
continue;
281283
score += basename_same(source, target);
282284
if (score > best_score) {
283285
best = p;
@@ -416,6 +418,27 @@ static void record_if_better(struct diff_score m[], struct diff_score *o)
416418
m[worst] = *o;
417419
}
418420

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+
419442
void diffcore_rename(struct diff_options *options)
420443
{
421444
int detect_rename = options->detect_rename;
@@ -538,33 +561,9 @@ void diffcore_rename(struct diff_options *options)
538561
/* cost matrix sorted by most to least similar pair */
539562
qsort(mx, dst_cnt * NUM_CANDIDATE_PER_DST, sizeof(*mx), score_compare);
540563

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

570569
cleanup:

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)