@matheusbsbs / matheus-bagliano / commits / f31027c99c
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Jan 6, 2011 at 13:50 UTC f31027c99cb2ec4eb7ad8d1ebc7f0e20fef4bd1d
7 files changed +112 -10
builtin/diff-tree.c
+11 -1
@@ -163,6 +163,9 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
163 }
164
165 if (read_stdin) {
166 + int saved_nrl = 0;
167 + int saved_dcctc = 0;
168 +
169 if (opt->diffopt.detect_rename)
170 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
171 DIFF_SETUP_USE_CACHE);
@@ -173,9 +176,16 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
176 fputs(line, stdout);
177 fflush(stdout);
178 }
176 - else
179 + else {
180 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 + }
186 }
187 + opt->diffopt.degraded_cc_to_c = saved_dcctc;
188 + opt->diffopt.needed_rename_limit = saved_nrl;
189 }
190
191 return diff_result_code(&opt->diffopt, 0);
builtin/log.c
+9
@@ -247,6 +247,8 @@ static void finish_early_output(struct rev_info *rev)
247 static int cmd_log_walk(struct rev_info *rev)
248 {
249 struct commit *commit;
250 + int saved_nrl = 0;
251 + int saved_dcctc = 0;
252
253 if (rev->early_output)
254 setup_early_output(rev);
@@ -277,7 +279,14 @@ static int cmd_log_walk(struct rev_info *rev)
279 }
280 free_commit_list(commit->parents);
281 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;
286 }
287 + rev->diffopt.degraded_cc_to_c = saved_dcctc;
288 + rev->diffopt.needed_rename_limit = saved_nrl;
289 +
290 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
291 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
292 return 02;
diff.c
+26
@@ -3956,6 +3956,28 @@ static int is_summary_empty(const struct diff_queue_struct *q)
3956 return 1;
3957 }
3958
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 +
3981 void diff_flush(struct diff_options *options)
3982 {
3983 struct diff_queue_struct *q = &diff_queued_diff;
@@ -4237,6 +4259,10 @@ void diffcore_std(struct diff_options *options)
4259 int diff_result_code(struct diff_options *opt, int status)
4260 {
4261 int result = 0;
4262 +
4263 + diff_warn_rename_limit("diff.renamelimit",
4264 + opt->needed_rename_limit,
4265 + opt->degraded_cc_to_c);
4266 if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
4267 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
4268 return status;
diff.h
+2
@@ -111,6 +111,7 @@ struct diff_options {
111 int rename_score;
112 int rename_limit;
113 int needed_rename_limit;
114 + int degraded_cc_to_c;
115 int show_rename_progress;
116 int dirstat_percent;
117 int setup;
@@ -273,6 +274,7 @@ extern void diffcore_fix_diff_index(struct diff_options *);
274
275 extern int diff_queue_is_empty(void);
276 extern void diff_flush(struct diff_options*);
277 +extern void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc);
278
279 /* diff-raw status letters */
280 #define DIFF_STATUS_ADDED 'A'
diffcore-rename.c
+36 -2
@@ -420,11 +420,18 @@ static void record_if_better(struct diff_score m[], struct diff_score *o)
420 m[worst] = *o;
421 }
422
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
@@ -445,6 +452,20 @@ static int too_many_rename_candidates(int num_create,
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
@@ -476,7 +497,7 @@ void diffcore_rename(struct diff_options *options)
497 struct diff_queue_struct *q = &diff_queued_diff;
498 struct diff_queue_struct outq;
499 struct diff_score *mx;
479 - int i, j, rename_count;
500 + int i, j, rename_count, skip_unmodified = 0;
501 int num_create, num_src, dst_cnt;
502 struct progress *progress = NULL;
503
@@ -539,8 +560,16 @@ void diffcore_rename(struct diff_options *options)
560 if (!num_create)
561 goto cleanup;
562
542 - if (too_many_rename_candidates(num_create, options))
563 + switch (too_many_rename_candidates(num_create, options)) {
564 + case 1:
565 goto cleanup;
566 + case 2:
567 + options->degraded_cc_to_c = 1;
568 + skip_unmodified = 1;
569 + break;
570 + default:
571 + break;
572 + }
573
574 if (options->show_rename_progress) {
575 progress = start_progress_delay(
@@ -563,6 +592,11 @@ void diffcore_rename(struct diff_options *options)
592 for (j = 0; j < rename_src_nr; j++) {
593 struct diff_filespec *one = rename_src[j].p->one;
594 struct diff_score this_src;
595 +
596 + if (skip_unmodified &&
597 + diff_unmodified_pair(rename_src[j].p))
598 + continue;
599 +
600 this_src.score = estimate_similarity(one, two,
601 minimum_score);
602 this_src.name_score = basename_same(one, two);
merge-recursive.c
+3 -7
@@ -22,11 +22,6 @@
22 #include "dir.h"
23 #include "submodule.h"
24
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 -
25 static struct tree *shift_tree_object(struct tree *one, struct tree *two,
26 const char *subtree_shift)
27 {
@@ -1656,8 +1651,9 @@ int merge_recursive(struct merge_options *o,
1651 commit_list_insert(h2, &(*result)->parents->next);
1652 }
1653 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);
1657 return clean;
1658 }
1659
t/t4001-diff-rename.sh
+25
@@ -77,4 +77,29 @@ test_expect_success 'favour same basenames even with minor differences' '
77 git show HEAD:path1 | sed "s/15/16/" > subdir/path1 &&
78 git status | grep "renamed: .*path1 -> subdir/path1"'
79
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 +
105 test_done