@matheusbsbs / matheus-bagliano / commits / 712d2c7dd8
Fork 0

Allow specifying --dirstat cut-off percentage as a floating point number

Only the first digit after the decimal point is kept, as the dirstat calculations all happen in permille. Selftests verifying floating-point percentage input has been added. Improved-by: Junio C Hamano <gitster@pobox.com> Improved-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Apr 29, 2011 at 11:36 UTC 712d2c7dd893212756c21787fc12d6f71327e167
3 files changed +81 -11
diff.c
+16 -10
@@ -31,7 +31,7 @@ static const char *external_diff_cmd_cfg;
31 int diff_auto_refresh_index = 1;
32 static int diff_mnemonic_prefix;
33 static int diff_no_prefix;
34 -static int diff_dirstat_percent_default = 3;
34 +static int diff_dirstat_permille_default = 30;
35 static struct diff_options default_diff_options;
36
37 static char diff_colors[][COLOR_MAXLEN] = {
@@ -85,8 +85,15 @@ static int parse_dirstat_params(struct diff_options *options, const char *params
85 DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
86 } else if (isdigit(*p)) {
87 char *end;
88 - options->dirstat_percent = strtoul(p, &end, 10);
88 + options->dirstat_permille = strtoul(p, &end, 10) * 10;
89 p = end;
90 + if (*p == '.' && isdigit(*++p)) {
91 + /* only use first digit */
92 + options->dirstat_permille += *p - '0';
93 + /* .. and ignore any further digits */
94 + while (isdigit(*++p))
95 + ; /* nothing */
96 + }
97 } else
98 return error("Unknown --dirstat parameter '%s'", p);
99
@@ -182,9 +189,9 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
189 }
190
191 if (!strcmp(var, "diff.dirstat")) {
185 - default_diff_options.dirstat_percent = diff_dirstat_percent_default;
192 + default_diff_options.dirstat_permille = diff_dirstat_permille_default;
193 (void) parse_dirstat_params(&default_diff_options, value);
187 - diff_dirstat_percent_default = default_diff_options.dirstat_percent;
194 + diff_dirstat_permille_default = default_diff_options.dirstat_permille;
195 return 0;
196 }
197
@@ -1498,7 +1505,7 @@ struct dirstat_file {
1505
1506 struct dirstat_dir {
1507 struct dirstat_file *files;
1501 - int alloc, nr, percent, cumulative;
1508 + int alloc, nr, permille, cumulative;
1509 };
1510
1511 static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
@@ -1547,10 +1554,9 @@ static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
1554 if (baselen && sources != 1) {
1555 if (this_dir) {
1556 int permille = this_dir * 1000 / changed;
1550 - int percent = permille / 10;
1551 - if (percent >= dir->percent) {
1557 + if (permille >= dir->permille) {
1558 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
1553 - percent, permille % 10, baselen, base);
1559 + permille / 10, permille % 10, baselen, base);
1560 if (!dir->cumulative)
1561 return 0;
1562 }
@@ -1576,7 +1582,7 @@ static void show_dirstat(struct diff_options *options)
1582 dir.files = NULL;
1583 dir.alloc = 0;
1584 dir.nr = 0;
1579 - dir.percent = options->dirstat_percent;
1585 + dir.permille = options->dirstat_permille;
1586 dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
1587
1588 changed = 0;
@@ -2934,7 +2940,7 @@ void diff_setup(struct diff_options *options)
2940 options->line_termination = '\n';
2941 options->break_opt = -1;
2942 options->rename_limit = -1;
2937 - options->dirstat_percent = diff_dirstat_percent_default;
2943 + options->dirstat_permille = diff_dirstat_permille_default;
2944 options->context = 3;
2945
2946 options->change = diff_change;
diff.h
+1 -1
@@ -114,7 +114,7 @@ struct diff_options {
114 int needed_rename_limit;
115 int degraded_cc_to_c;
116 int show_rename_progress;
117 - int dirstat_percent;
117 + int dirstat_permille;
118 int setup;
119 int abbrev;
120 const char *prefix;
t/t4047-diff-dirstat.sh
+64
@@ -775,4 +775,68 @@ test_expect_success 'diff.dirstat=10,cumulative,files' '
775 test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
776 '
777
778 +cat <<EOF >expect_diff_dirstat
779 + 27.2% dst/copy/
780 + 27.2% dst/move/
781 + 54.5% dst/
782 + 27.2% src/move/
783 +EOF
784 +
785 +cat <<EOF >expect_diff_dirstat_M
786 + 42.8% dst/copy/
787 + 28.5% dst/move/
788 + 71.4% dst/
789 +EOF
790 +
791 +cat <<EOF >expect_diff_dirstat_CC
792 + 33.3% dst/copy/
793 + 33.3% dst/move/
794 + 66.6% dst/
795 +EOF
796 +
797 +test_expect_success '--dirstat=files,cumulative,16.7' '
798 + git diff --dirstat=files,cumulative,16.7 HEAD^..HEAD >actual_diff_dirstat &&
799 + test_cmp expect_diff_dirstat actual_diff_dirstat &&
800 + git diff --dirstat=files,cumulative,16.7 -M HEAD^..HEAD >actual_diff_dirstat_M &&
801 + test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
802 + git diff --dirstat=files,cumulative,16.7 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
803 + test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
804 +'
805 +
806 +test_expect_success 'diff.dirstat=16.7,cumulative,files' '
807 + git -c diff.dirstat=16.7,cumulative,files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
808 + test_cmp expect_diff_dirstat actual_diff_dirstat &&
809 + git -c diff.dirstat=16.7,cumulative,files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
810 + test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
811 + git -c diff.dirstat=16.7,cumulative,files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
812 + test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
813 +'
814 +
815 +test_expect_success 'diff.dirstat=16.70,cumulative,files' '
816 + git -c diff.dirstat=16.70,cumulative,files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
817 + test_cmp expect_diff_dirstat actual_diff_dirstat &&
818 + git -c diff.dirstat=16.70,cumulative,files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
819 + test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
820 + git -c diff.dirstat=16.70,cumulative,files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
821 + test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
822 +'
823 +
824 +test_expect_success '--dirstat=files,cumulative,27.2' '
825 + git diff --dirstat=files,cumulative,27.2 HEAD^..HEAD >actual_diff_dirstat &&
826 + test_cmp expect_diff_dirstat actual_diff_dirstat &&
827 + git diff --dirstat=files,cumulative,27.2 -M HEAD^..HEAD >actual_diff_dirstat_M &&
828 + test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
829 + git diff --dirstat=files,cumulative,27.2 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
830 + test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
831 +'
832 +
833 +test_expect_success '--dirstat=files,cumulative,27.09' '
834 + git diff --dirstat=files,cumulative,27.09 HEAD^..HEAD >actual_diff_dirstat &&
835 + test_cmp expect_diff_dirstat actual_diff_dirstat &&
836 + git diff --dirstat=files,cumulative,27.09 -M HEAD^..HEAD >actual_diff_dirstat_M &&
837 + test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
838 + git diff --dirstat=files,cumulative,27.09 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
839 + test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
840 +'
841 +
842 test_done