Skip to content

Commit f31027c

Browse files
committed
diffcore-rename: fall back to -C when -C -C busts the rename limit
When there are too many paths in the project, the number of rename source candidates "git diff -C -C" finds will exceed the rename detection limit, and no inexact rename detection is performed. We however could fall back to "git diff -C" if the number of modified paths is sufficiently small. Signed-off-by: Junio C Hamano <[email protected]>
1 parent e88d6bc commit f31027c

File tree

7 files changed

+112
-10
lines changed

7 files changed

+112
-10
lines changed

builtin/diff-tree.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
163163
}
164164

165165
if (read_stdin) {
166+
int saved_nrl = 0;
167+
int saved_dcctc = 0;
168+
166169
if (opt->diffopt.detect_rename)
167170
opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
168171
DIFF_SETUP_USE_CACHE);
@@ -173,9 +176,16 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
173176
fputs(line, stdout);
174177
fflush(stdout);
175178
}
176-
else
179+
else {
177180
diff_tree_stdin(line);
181+
if (saved_nrl < opt->diffopt.needed_rename_limit)
182+
saved_nrl = opt->diffopt.needed_rename_limit;
183+
if (opt->diffopt.degraded_cc_to_c)
184+
saved_dcctc = 1;
185+
}
178186
}
187+
opt->diffopt.degraded_cc_to_c = saved_dcctc;
188+
opt->diffopt.needed_rename_limit = saved_nrl;
179189
}
180190

181191
return diff_result_code(&opt->diffopt, 0);

builtin/log.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ static void finish_early_output(struct rev_info *rev)
247247
static int cmd_log_walk(struct rev_info *rev)
248248
{
249249
struct commit *commit;
250+
int saved_nrl = 0;
251+
int saved_dcctc = 0;
250252

251253
if (rev->early_output)
252254
setup_early_output(rev);
@@ -277,7 +279,14 @@ static int cmd_log_walk(struct rev_info *rev)
277279
}
278280
free_commit_list(commit->parents);
279281
commit->parents = NULL;
282+
if (saved_nrl < rev->diffopt.needed_rename_limit)
283+
saved_nrl = rev->diffopt.needed_rename_limit;
284+
if (rev->diffopt.degraded_cc_to_c)
285+
saved_dcctc = 1;
280286
}
287+
rev->diffopt.degraded_cc_to_c = saved_dcctc;
288+
rev->diffopt.needed_rename_limit = saved_nrl;
289+
281290
if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
282291
DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
283292
return 02;

diff.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3956,6 +3956,28 @@ static int is_summary_empty(const struct diff_queue_struct *q)
39563956
return 1;
39573957
}
39583958

3959+
static const char rename_limit_warning[] =
3960+
"inexact rename detection was skipped due to too many files.";
3961+
3962+
static const char degrade_cc_to_c_warning[] =
3963+
"only found copies from modified paths due to too many files.";
3964+
3965+
static const char rename_limit_advice[] =
3966+
"you may want to set your %s variable to at least "
3967+
"%d and retry the command.";
3968+
3969+
void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
3970+
{
3971+
if (degraded_cc)
3972+
warning(degrade_cc_to_c_warning);
3973+
else if (needed)
3974+
warning(rename_limit_warning);
3975+
else
3976+
return;
3977+
if (0 < needed && needed < 32767)
3978+
warning(rename_limit_advice, varname, needed);
3979+
}
3980+
39593981
void diff_flush(struct diff_options *options)
39603982
{
39613983
struct diff_queue_struct *q = &diff_queued_diff;
@@ -4237,6 +4259,10 @@ void diffcore_std(struct diff_options *options)
42374259
int diff_result_code(struct diff_options *opt, int status)
42384260
{
42394261
int result = 0;
4262+
4263+
diff_warn_rename_limit("diff.renamelimit",
4264+
opt->needed_rename_limit,
4265+
opt->degraded_cc_to_c);
42404266
if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
42414267
!(opt->output_format & DIFF_FORMAT_CHECKDIFF))
42424268
return status;

diff.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct diff_options {
111111
int rename_score;
112112
int rename_limit;
113113
int needed_rename_limit;
114+
int degraded_cc_to_c;
114115
int show_rename_progress;
115116
int dirstat_percent;
116117
int setup;
@@ -273,6 +274,7 @@ extern void diffcore_fix_diff_index(struct diff_options *);
273274

274275
extern int diff_queue_is_empty(void);
275276
extern void diff_flush(struct diff_options*);
277+
extern void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc);
276278

277279
/* diff-raw status letters */
278280
#define DIFF_STATUS_ADDED 'A'

diffcore-rename.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,18 @@ static void record_if_better(struct diff_score m[], struct diff_score *o)
420420
m[worst] = *o;
421421
}
422422

423+
/*
424+
* Returns:
425+
* 0 if we are under the limit;
426+
* 1 if we need to disable inexact rename detection;
427+
* 2 if we would be under the limit if we were given -C instead of -C -C.
428+
*/
423429
static int too_many_rename_candidates(int num_create,
424430
struct diff_options *options)
425431
{
426432
int rename_limit = options->rename_limit;
427433
int num_src = rename_src_nr;
434+
int i;
428435

429436
options->needed_rename_limit = 0;
430437

@@ -445,6 +452,20 @@ static int too_many_rename_candidates(int num_create,
445452

446453
options->needed_rename_limit =
447454
num_src > num_create ? num_src : num_create;
455+
456+
/* Are we running under -C -C? */
457+
if (!DIFF_OPT_TST(options, FIND_COPIES_HARDER))
458+
return 1;
459+
460+
/* Would we bust the limit if we were running under -C? */
461+
for (num_src = i = 0; i < rename_src_nr; i++) {
462+
if (diff_unmodified_pair(rename_src[i].p))
463+
continue;
464+
num_src++;
465+
}
466+
if ((num_create <= rename_limit || num_src <= rename_limit) &&
467+
(num_create * num_src <= rename_limit * rename_limit))
468+
return 2;
448469
return 1;
449470
}
450471

@@ -476,7 +497,7 @@ void diffcore_rename(struct diff_options *options)
476497
struct diff_queue_struct *q = &diff_queued_diff;
477498
struct diff_queue_struct outq;
478499
struct diff_score *mx;
479-
int i, j, rename_count;
500+
int i, j, rename_count, skip_unmodified = 0;
480501
int num_create, num_src, dst_cnt;
481502
struct progress *progress = NULL;
482503

@@ -539,8 +560,16 @@ void diffcore_rename(struct diff_options *options)
539560
if (!num_create)
540561
goto cleanup;
541562

542-
if (too_many_rename_candidates(num_create, options))
563+
switch (too_many_rename_candidates(num_create, options)) {
564+
case 1:
543565
goto cleanup;
566+
case 2:
567+
options->degraded_cc_to_c = 1;
568+
skip_unmodified = 1;
569+
break;
570+
default:
571+
break;
572+
}
544573

545574
if (options->show_rename_progress) {
546575
progress = start_progress_delay(
@@ -563,6 +592,11 @@ void diffcore_rename(struct diff_options *options)
563592
for (j = 0; j < rename_src_nr; j++) {
564593
struct diff_filespec *one = rename_src[j].p->one;
565594
struct diff_score this_src;
595+
596+
if (skip_unmodified &&
597+
diff_unmodified_pair(rename_src[j].p))
598+
continue;
599+
566600
this_src.score = estimate_similarity(one, two,
567601
minimum_score);
568602
this_src.name_score = basename_same(one, two);

merge-recursive.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
#include "dir.h"
2323
#include "submodule.h"
2424

25-
static const char rename_limit_advice[] =
26-
"inexact rename detection was skipped because there were too many\n"
27-
" files. You may want to set your merge.renamelimit variable to at least\n"
28-
" %d and retry this merge.";
29-
3025
static struct tree *shift_tree_object(struct tree *one, struct tree *two,
3126
const char *subtree_shift)
3227
{
@@ -1656,8 +1651,9 @@ int merge_recursive(struct merge_options *o,
16561651
commit_list_insert(h2, &(*result)->parents->next);
16571652
}
16581653
flush_output(o);
1659-
if (o->needed_rename_limit)
1660-
warning(rename_limit_advice, o->needed_rename_limit);
1654+
if (show(o, 2))
1655+
diff_warn_rename_limit("merge.renamelimit",
1656+
o->needed_rename_limit, 0);
16611657
return clean;
16621658
}
16631659

t/t4001-diff-rename.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,29 @@ test_expect_success 'favour same basenames even with minor differences' '
7777
git show HEAD:path1 | sed "s/15/16/" > subdir/path1 &&
7878
git status | grep "renamed: .*path1 -> subdir/path1"'
7979

80+
test_expect_success 'setup for many rename source candidates' '
81+
git reset --hard &&
82+
for i in 0 1 2 3 4 5 6 7 8 9;
83+
do
84+
for j in 0 1 2 3 4 5 6 7 8 9;
85+
do
86+
echo "$i$j" >"path$i$j"
87+
done
88+
done &&
89+
git add "path??" &&
90+
test_tick &&
91+
git commit -m "hundred" &&
92+
(cat path1; echo new) >new-path &&
93+
echo old >>path1 &&
94+
git add new-path path1 &&
95+
git diff -l 4 -C -C --cached --name-status >actual 2>actual.err &&
96+
sed -e "s/^\([CM]\)[0-9]* /\1 /" actual >actual.munged &&
97+
cat >expect <<-EOF &&
98+
C path1 new-path
99+
M path1
100+
EOF
101+
test_cmp expect actual.munged &&
102+
grep warning actual.err
103+
'
104+
80105
test_done

0 commit comments

Comments
 (0)