Skip to content

Commit 76a89d6

Browse files
committed
Merge branch 'jc/rename-degrade-cc-to-c'
* jc/rename-degrade-cc-to-c: diffcore-rename: fall back to -C when -C -C busts the rename limit diffcore-rename: record filepair for rename src diffcore-rename: refactor "too many candidates" logic builtin/diff.c: remove duplicated call to diff_result_code()
2 parents 78c6e0f + f31027c commit 76a89d6

File tree

8 files changed

+152
-40
lines changed

8 files changed

+152
-40
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/diff.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ static void refresh_index_quietly(void)
202202

203203
static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
204204
{
205-
int result;
206205
unsigned int options = 0;
207206

208207
while (1 < argc && argv[1][0] == '-') {
@@ -236,8 +235,7 @@ static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv
236235
perror("read_cache_preload");
237236
return -1;
238237
}
239-
result = run_diff_files(revs, options);
240-
return diff_result_code(&revs->diffopt, result);
238+
return run_diff_files(revs, options);
241239
}
242240

243241
int cmd_diff(int argc, const char **argv, const char *prefix)

builtin/log.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ static void finish_early_output(struct rev_info *rev)
256256
static int cmd_log_walk(struct rev_info *rev)
257257
{
258258
struct commit *commit;
259+
int saved_nrl = 0;
260+
int saved_dcctc = 0;
259261

260262
if (rev->early_output)
261263
setup_early_output(rev);
@@ -286,7 +288,14 @@ static int cmd_log_walk(struct rev_info *rev)
286288
}
287289
free_commit_list(commit->parents);
288290
commit->parents = NULL;
291+
if (saved_nrl < rev->diffopt.needed_rename_limit)
292+
saved_nrl = rev->diffopt.needed_rename_limit;
293+
if (rev->diffopt.degraded_cc_to_c)
294+
saved_dcctc = 1;
289295
}
296+
rev->diffopt.degraded_cc_to_c = saved_dcctc;
297+
rev->diffopt.needed_rename_limit = saved_nrl;
298+
290299
if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
291300
DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
292301
return 02;

diff.c

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

3990+
static const char rename_limit_warning[] =
3991+
"inexact rename detection was skipped due to too many files.";
3992+
3993+
static const char degrade_cc_to_c_warning[] =
3994+
"only found copies from modified paths due to too many files.";
3995+
3996+
static const char rename_limit_advice[] =
3997+
"you may want to set your %s variable to at least "
3998+
"%d and retry the command.";
3999+
4000+
void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
4001+
{
4002+
if (degraded_cc)
4003+
warning(degrade_cc_to_c_warning);
4004+
else if (needed)
4005+
warning(rename_limit_warning);
4006+
else
4007+
return;
4008+
if (0 < needed && needed < 32767)
4009+
warning(rename_limit_advice, varname, needed);
4010+
}
4011+
39904012
void diff_flush(struct diff_options *options)
39914013
{
39924014
struct diff_queue_struct *q = &diff_queued_diff;
@@ -4268,6 +4290,10 @@ void diffcore_std(struct diff_options *options)
42684290
int diff_result_code(struct diff_options *opt, int status)
42694291
{
42704292
int result = 0;
4293+
4294+
diff_warn_rename_limit("diff.renamelimit",
4295+
opt->needed_rename_limit,
4296+
opt->degraded_cc_to_c);
42714297
if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
42724298
!(opt->output_format & DIFF_FORMAT_CHECKDIFF))
42734299
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: 75 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,23 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
5555

5656
/* Table of rename/copy src files */
5757
static struct diff_rename_src {
58-
struct diff_filespec *one;
58+
struct diff_filepair *p;
5959
unsigned short score; /* to remember the break score */
6060
} *rename_src;
6161
static int rename_src_nr, rename_src_alloc;
6262

63-
static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
64-
unsigned short score)
63+
static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
6564
{
6665
int first, last;
66+
struct diff_filespec *one = p->one;
67+
unsigned short score = p->score;
6768

6869
first = 0;
6970
last = rename_src_nr;
7071
while (last > first) {
7172
int next = (last + first) >> 1;
7273
struct diff_rename_src *src = &(rename_src[next]);
73-
int cmp = strcmp(one->path, src->one->path);
74+
int cmp = strcmp(one->path, src->p->one->path);
7475
if (!cmp)
7576
return src;
7677
if (cmp < 0) {
@@ -90,7 +91,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
9091
if (first < rename_src_nr)
9192
memmove(rename_src + first + 1, rename_src + first,
9293
(rename_src_nr - first - 1) * sizeof(*rename_src));
93-
rename_src[first].one = one;
94+
rename_src[first].p = p;
9495
rename_src[first].score = score;
9596
return &(rename_src[first]);
9697
}
@@ -205,7 +206,7 @@ static void record_rename_pair(int dst_index, int src_index, int score)
205206
if (rename_dst[dst_index].pair)
206207
die("internal error: dst already matched.");
207208

208-
src = rename_src[src_index].one;
209+
src = rename_src[src_index].p->one;
209210
src->rename_used++;
210211
src->count++;
211212

@@ -389,7 +390,7 @@ static int find_exact_renames(struct diff_options *options)
389390

390391
init_hash(&file_table);
391392
for (i = 0; i < rename_src_nr; i++)
392-
insert_file_table(&file_table, -1, i, rename_src[i].one);
393+
insert_file_table(&file_table, -1, i, rename_src[i].p->one);
393394

394395
for (i = 0; i < rename_dst_nr; i++)
395396
insert_file_table(&file_table, 1, i, rename_dst[i].two);
@@ -419,6 +420,55 @@ static void record_if_better(struct diff_score m[], struct diff_score *o)
419420
m[worst] = *o;
420421
}
421422

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+
*/
429+
static int too_many_rename_candidates(int num_create,
430+
struct diff_options *options)
431+
{
432+
int rename_limit = options->rename_limit;
433+
int num_src = rename_src_nr;
434+
int i;
435+
436+
options->needed_rename_limit = 0;
437+
438+
/*
439+
* This basically does a test for the rename matrix not
440+
* growing larger than a "rename_limit" square matrix, ie:
441+
*
442+
* num_create * num_src > rename_limit * rename_limit
443+
*
444+
* but handles the potential overflow case specially (and we
445+
* assume at least 32-bit integers)
446+
*/
447+
if (rename_limit <= 0 || rename_limit > 32767)
448+
rename_limit = 32767;
449+
if ((num_create <= rename_limit || num_src <= rename_limit) &&
450+
(num_create * num_src <= rename_limit * rename_limit))
451+
return 0;
452+
453+
options->needed_rename_limit =
454+
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;
469+
return 1;
470+
}
471+
422472
static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, int copies)
423473
{
424474
int count = 0, i;
@@ -432,7 +482,7 @@ static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, i
432482
dst = &rename_dst[mx[i].dst];
433483
if (dst->pair)
434484
continue; /* already done, either exact or fuzzy. */
435-
if (!copies && rename_src[mx[i].src].one->rename_used)
485+
if (!copies && rename_src[mx[i].src].p->one->rename_used)
436486
continue;
437487
record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
438488
count++;
@@ -444,11 +494,10 @@ void diffcore_rename(struct diff_options *options)
444494
{
445495
int detect_rename = options->detect_rename;
446496
int minimum_score = options->rename_score;
447-
int rename_limit = options->rename_limit;
448497
struct diff_queue_struct *q = &diff_queued_diff;
449498
struct diff_queue_struct outq;
450499
struct diff_score *mx;
451-
int i, j, rename_count;
500+
int i, j, rename_count, skip_unmodified = 0;
452501
int num_create, num_src, dst_cnt;
453502
struct progress *progress = NULL;
454503

@@ -476,15 +525,15 @@ void diffcore_rename(struct diff_options *options)
476525
*/
477526
if (p->broken_pair && !p->score)
478527
p->one->rename_used++;
479-
register_rename_src(p->one, p->score);
528+
register_rename_src(p);
480529
}
481530
else if (detect_rename == DIFF_DETECT_COPY) {
482531
/*
483532
* Increment the "rename_used" score by
484533
* one, to indicate ourselves as a user.
485534
*/
486535
p->one->rename_used++;
487-
register_rename_src(p->one, p->score);
536+
register_rename_src(p);
488537
}
489538
}
490539
if (rename_dst_nr == 0 || rename_src_nr == 0)
@@ -511,23 +560,15 @@ void diffcore_rename(struct diff_options *options)
511560
if (!num_create)
512561
goto cleanup;
513562

514-
/*
515-
* This basically does a test for the rename matrix not
516-
* growing larger than a "rename_limit" square matrix, ie:
517-
*
518-
* num_create * num_src > rename_limit * rename_limit
519-
*
520-
* but handles the potential overflow case specially (and we
521-
* assume at least 32-bit integers)
522-
*/
523-
options->needed_rename_limit = 0;
524-
if (rename_limit <= 0 || rename_limit > 32767)
525-
rename_limit = 32767;
526-
if ((num_create > rename_limit && num_src > rename_limit) ||
527-
(num_create * num_src > rename_limit * rename_limit)) {
528-
options->needed_rename_limit =
529-
num_src > num_create ? num_src : num_create;
563+
switch (too_many_rename_candidates(num_create, options)) {
564+
case 1:
530565
goto cleanup;
566+
case 2:
567+
options->degraded_cc_to_c = 1;
568+
skip_unmodified = 1;
569+
break;
570+
default:
571+
break;
531572
}
532573

533574
if (options->show_rename_progress) {
@@ -549,8 +590,13 @@ void diffcore_rename(struct diff_options *options)
549590
m[j].dst = -1;
550591

551592
for (j = 0; j < rename_src_nr; j++) {
552-
struct diff_filespec *one = rename_src[j].one;
593+
struct diff_filespec *one = rename_src[j].p->one;
553594
struct diff_score this_src;
595+
596+
if (skip_unmodified &&
597+
diff_unmodified_pair(rename_src[j].p))
598+
continue;
599+
554600
this_src.score = estimate_similarity(one, two,
555601
minimum_score);
556602
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
{
@@ -1664,8 +1659,9 @@ int merge_recursive(struct merge_options *o,
16641659
commit_list_insert(h2, &(*result)->parents->next);
16651660
}
16661661
flush_output(o);
1667-
if (o->needed_rename_limit)
1668-
warning(rename_limit_advice, o->needed_rename_limit);
1662+
if (show(o, 2))
1663+
diff_warn_rename_limit("merge.renamelimit",
1664+
o->needed_rename_limit, 0);
16691665
return clean;
16701666
}
16711667

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 C_LOCALE_OUTPUT 'favour same basenames even with minor diff
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)