Skip to content

Commit

Permalink
diffcore-rename: properly honor the difference between -M and -C
Browse files Browse the repository at this point in the history
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 <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Linus Torvalds authored and Junio C Hamano committed Feb 19, 2011
1 parent 11f944d commit 0940e5f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 33 deletions.
53 changes: 26 additions & 27 deletions diffcore-rename.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,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;
Expand Down Expand Up @@ -416,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;
Expand Down Expand Up @@ -538,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:
Expand Down
2 changes: 1 addition & 1 deletion t/t4003-diff-rename-1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion t/t4004-diff-rename-symlink.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion t/t4005-diff-rename-2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions t/t4008-diff-break-rewrite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion t/t4009-diff-rename-4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0940e5f

Please sign in to comment.