Skip to content

Commit 712d2c7

Browse files
jherlandgitster
authored andcommitted
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 <[email protected]> Improved-by: Linus Torvalds <[email protected]> Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2d17495 commit 712d2c7

File tree

3 files changed

+81
-11
lines changed

3 files changed

+81
-11
lines changed

diff.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static const char *external_diff_cmd_cfg;
3131
int diff_auto_refresh_index = 1;
3232
static int diff_mnemonic_prefix;
3333
static int diff_no_prefix;
34-
static int diff_dirstat_percent_default = 3;
34+
static int diff_dirstat_permille_default = 30;
3535
static struct diff_options default_diff_options;
3636

3737
static char diff_colors[][COLOR_MAXLEN] = {
@@ -85,8 +85,15 @@ static int parse_dirstat_params(struct diff_options *options, const char *params
8585
DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
8686
} else if (isdigit(*p)) {
8787
char *end;
88-
options->dirstat_percent = strtoul(p, &end, 10);
88+
options->dirstat_permille = strtoul(p, &end, 10) * 10;
8989
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+
}
9097
} else
9198
return error("Unknown --dirstat parameter '%s'", p);
9299

@@ -182,9 +189,9 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
182189
}
183190

184191
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;
186193
(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;
188195
return 0;
189196
}
190197

@@ -1498,7 +1505,7 @@ struct dirstat_file {
14981505

14991506
struct dirstat_dir {
15001507
struct dirstat_file *files;
1501-
int alloc, nr, percent, cumulative;
1508+
int alloc, nr, permille, cumulative;
15021509
};
15031510

15041511
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,
15471554
if (baselen && sources != 1) {
15481555
if (this_dir) {
15491556
int permille = this_dir * 1000 / changed;
1550-
int percent = permille / 10;
1551-
if (percent >= dir->percent) {
1557+
if (permille >= dir->permille) {
15521558
fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
1553-
percent, permille % 10, baselen, base);
1559+
permille / 10, permille % 10, baselen, base);
15541560
if (!dir->cumulative)
15551561
return 0;
15561562
}
@@ -1576,7 +1582,7 @@ static void show_dirstat(struct diff_options *options)
15761582
dir.files = NULL;
15771583
dir.alloc = 0;
15781584
dir.nr = 0;
1579-
dir.percent = options->dirstat_percent;
1585+
dir.permille = options->dirstat_permille;
15801586
dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
15811587

15821588
changed = 0;
@@ -2934,7 +2940,7 @@ void diff_setup(struct diff_options *options)
29342940
options->line_termination = '\n';
29352941
options->break_opt = -1;
29362942
options->rename_limit = -1;
2937-
options->dirstat_percent = diff_dirstat_percent_default;
2943+
options->dirstat_permille = diff_dirstat_permille_default;
29382944
options->context = 3;
29392945

29402946
options->change = diff_change;

diff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ struct diff_options {
114114
int needed_rename_limit;
115115
int degraded_cc_to_c;
116116
int show_rename_progress;
117-
int dirstat_percent;
117+
int dirstat_permille;
118118
int setup;
119119
int abbrev;
120120
const char *prefix;

t/t4047-diff-dirstat.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,4 +775,68 @@ test_expect_success 'diff.dirstat=10,cumulative,files' '
775775
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
776776
'
777777

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+
778842
test_done

0 commit comments

Comments
 (0)