Skip to content

Commit 2d17495

Browse files
jherlandgitster
authored andcommitted
Add config variable for specifying default --dirstat behavior
The new diff.dirstat config variable takes the same arguments as '--dirstat=<args>', and specifies the default arguments for --dirstat. The config is obviously overridden by --dirstat arguments passed on the command line. When not specified, the --dirstat defaults are 'changes,noncumulative,3'. The patch also adds several tests verifying the interaction between the diff.dirstat config variable, and the --dirstat command line option. Improved-by: Junio C Hamano <[email protected]> Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 333f3fb commit 2d17495

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

Documentation/diff-config.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,42 @@ diff.autorefreshindex::
88
affects only 'git diff' Porcelain, and not lower level
99
'diff' commands such as 'git diff-files'.
1010

11+
diff.dirstat::
12+
A comma separated list of `--dirstat` parameters specifying the
13+
default behavior of the `--dirstat` option to linkgit:git-diff[1]`
14+
and friends. The defaults can be overridden on the command line
15+
(using `--dirstat=<param1,param2,...>`). The fallback defaults
16+
(when not changed by `diff.dirstat`) are `changes,noncumulative,3`.
17+
The following parameters are available:
18+
+
19+
--
20+
`changes`;;
21+
Compute the dirstat numbers by counting the lines that have been
22+
removed from the source, or added to the destination. This ignores
23+
the amount of pure code movements within a file. In other words,
24+
rearranging lines in a file is not counted as much as other changes.
25+
This is the default behavior when no parameter is given.
26+
`files`;;
27+
Compute the dirstat numbers by counting the number of files changed.
28+
Each changed file counts equally in the dirstat analysis. This is
29+
the computationally cheapest `--dirstat` behavior, since it does
30+
not have to look at the file contents at all.
31+
`cumulative`;;
32+
Count changes in a child directory for the parent directory as well.
33+
Note that when using `cumulative`, the sum of the percentages
34+
reported may exceed 100%. The default (non-cumulative) behavior can
35+
be specified with the `noncumulative` parameter.
36+
<limit>;;
37+
An integer parameter specifies a cut-off percent (3% by default).
38+
Directories contributing less than this percentage of the changes
39+
are not shown in the output.
40+
--
41+
+
42+
Example: The following will count changed files, while ignoring
43+
directories with less than 10% of the total amount of changed files,
44+
and accumulating child directory counts in the parent directories:
45+
`files,10,cumulative`.
46+
1147
diff.external::
1248
If this config variable is set, diff generation is not
1349
performed using the internal diff machinery, but using the

Documentation/diff-options.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ endif::git-format-patch[]
7070
Output the distribution of relative amount of changes for each
7171
sub-directory. The behavior of `--dirstat` can be customized by
7272
passing it a comma separated list of parameters.
73+
The defaults are controlled by the `diff.dirstat` configuration
74+
variable (see linkgit:git-config[1]).
7375
The following parameters are available:
7476
+
7577
--

diff.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +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;
3435
static struct diff_options default_diff_options;
3536

3637
static char diff_colors[][COLOR_MAXLEN] = {
@@ -180,6 +181,13 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
180181
return 0;
181182
}
182183

184+
if (!strcmp(var, "diff.dirstat")) {
185+
default_diff_options.dirstat_percent = diff_dirstat_percent_default;
186+
(void) parse_dirstat_params(&default_diff_options, value);
187+
diff_dirstat_percent_default = default_diff_options.dirstat_percent;
188+
return 0;
189+
}
190+
183191
if (!prefixcmp(var, "submodule."))
184192
return parse_submodule_config_option(var, value);
185193

@@ -2926,7 +2934,7 @@ void diff_setup(struct diff_options *options)
29262934
options->line_termination = '\n';
29272935
options->break_opt = -1;
29282936
options->rename_limit = -1;
2929-
options->dirstat_percent = 3;
2937+
options->dirstat_percent = diff_dirstat_percent_default;
29302938
options->context = 3;
29312939

29322940
options->change = diff_change;

t/t4047-diff-dirstat.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,15 @@ test_expect_success 'later options override earlier options:' '
386386
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
387387
'
388388

389+
test_expect_success 'non-defaults in config overridden by explicit defaults on command line' '
390+
git -c diff.dirstat=files,cumulative,50 diff --dirstat=changes,noncumulative,3 HEAD^..HEAD >actual_diff_dirstat &&
391+
test_cmp expect_diff_dirstat actual_diff_dirstat &&
392+
git -c diff.dirstat=files,cumulative,50 diff --dirstat=changes,noncumulative,3 -M HEAD^..HEAD >actual_diff_dirstat_M &&
393+
test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
394+
git -c diff.dirstat=files,cumulative,50 diff --dirstat=changes,noncumulative,3 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
395+
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
396+
'
397+
389398
cat <<EOF >expect_diff_dirstat
390399
2.1% changed/
391400
10.8% dst/copy/changed/
@@ -437,6 +446,15 @@ test_expect_success '-X0' '
437446
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
438447
'
439448

449+
test_expect_success 'diff.dirstat=0' '
450+
git -c diff.dirstat=0 diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
451+
test_cmp expect_diff_dirstat actual_diff_dirstat &&
452+
git -c diff.dirstat=0 diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
453+
test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
454+
git -c diff.dirstat=0 diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
455+
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
456+
'
457+
440458
cat <<EOF >expect_diff_dirstat
441459
2.1% changed/
442460
10.8% dst/copy/changed/
@@ -507,6 +525,24 @@ test_expect_success '-X0,cumulative' '
507525
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
508526
'
509527

528+
test_expect_success 'diff.dirstat=0,cumulative' '
529+
git -c diff.dirstat=0,cumulative diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
530+
test_cmp expect_diff_dirstat actual_diff_dirstat &&
531+
git -c diff.dirstat=0,cumulative diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
532+
test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
533+
git -c diff.dirstat=0,cumulative diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
534+
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
535+
'
536+
537+
test_expect_success 'diff.dirstat=0 & --dirstat=cumulative' '
538+
git -c diff.dirstat=0 diff --dirstat=cumulative HEAD^..HEAD >actual_diff_dirstat &&
539+
test_cmp expect_diff_dirstat actual_diff_dirstat &&
540+
git -c diff.dirstat=0 diff --dirstat=cumulative -M HEAD^..HEAD >actual_diff_dirstat_M &&
541+
test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
542+
git -c diff.dirstat=0 diff --dirstat=cumulative -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
543+
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
544+
'
545+
510546
cat <<EOF >expect_diff_dirstat
511547
9.0% changed/
512548
9.0% dst/copy/changed/
@@ -558,6 +594,15 @@ test_expect_success '--dirstat=files' '
558594
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
559595
'
560596

597+
test_expect_success 'diff.dirstat=files' '
598+
git -c diff.dirstat=files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
599+
test_cmp expect_diff_dirstat actual_diff_dirstat &&
600+
git -c diff.dirstat=files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
601+
test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
602+
git -c diff.dirstat=files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
603+
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
604+
'
605+
561606
cat <<EOF >expect_diff_dirstat
562607
27.2% dst/copy/
563608
27.2% dst/move/
@@ -601,6 +646,15 @@ test_expect_success '--dirstat=files,10' '
601646
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
602647
'
603648

649+
test_expect_success 'diff.dirstat=10,files' '
650+
git -c diff.dirstat=10,files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
651+
test_cmp expect_diff_dirstat actual_diff_dirstat &&
652+
git -c diff.dirstat=10,files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
653+
test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
654+
git -c diff.dirstat=10,files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
655+
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
656+
'
657+
604658
cat <<EOF >expect_diff_dirstat
605659
9.0% changed/
606660
9.0% dst/copy/changed/
@@ -662,6 +716,15 @@ test_expect_success '--dirstat=files,cumulative' '
662716
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
663717
'
664718

719+
test_expect_success 'diff.dirstat=cumulative,files' '
720+
git -c diff.dirstat=cumulative,files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
721+
test_cmp expect_diff_dirstat actual_diff_dirstat &&
722+
git -c diff.dirstat=cumulative,files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
723+
test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
724+
git -c diff.dirstat=cumulative,files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
725+
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
726+
'
727+
665728
cat <<EOF >expect_diff_dirstat
666729
27.2% dst/copy/
667730
27.2% dst/move/
@@ -703,4 +766,13 @@ test_expect_success '--dirstat=files,cumulative,10' '
703766
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
704767
'
705768

769+
test_expect_success 'diff.dirstat=10,cumulative,files' '
770+
git -c diff.dirstat=10,cumulative,files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
771+
test_cmp expect_diff_dirstat actual_diff_dirstat &&
772+
git -c diff.dirstat=10,cumulative,files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
773+
test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
774+
git -c diff.dirstat=10,cumulative,files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
775+
test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
776+
'
777+
706778
test_done

0 commit comments

Comments
 (0)