Skip to content

Commit c47ef57

Browse files
artagnongitster
authored andcommitted
diff: introduce diff.submodule configuration variable
Introduce a diff.submodule configuration variable corresponding to the '--submodule' command-line option of 'git diff'. Signed-off-by: Ramkumar Ramachandra <[email protected]> Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 22bc70f commit c47ef57

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

Documentation/diff-config.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ diff.suppressBlankEmpty::
107107
A boolean to inhibit the standard behavior of printing a space
108108
before each empty output line. Defaults to false.
109109

110+
diff.submodule::
111+
Specify the format in which differences in submodules are
112+
shown. The "log" format lists the commits in the range like
113+
linkgit:git-submodule[1] `summary` does. The "short" format
114+
format just shows the names of the commits at the beginning
115+
and end of the range. Defaults to short.
116+
110117
diff.wordRegex::
111118
A POSIX Extended Regular Expression used to determine what is a "word"
112119
when performing word-by-word difference calculations. Character

Documentation/diff-options.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ any of those replacements occurred.
170170
the commits in the range like linkgit:git-submodule[1] `summary` does.
171171
Omitting the `--submodule` option or specifying `--submodule=short`,
172172
uses the 'short' format. This format just shows the names of the commits
173-
at the beginning and end of the range.
173+
at the beginning and end of the range. Can be tweaked via the
174+
`diff.submodule` configuration variable.
174175

175176
--color[=<when>]::
176177
Show colored diff.

diff.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ static int parse_dirstat_params(struct diff_options *options, const char *params
121121
return ret;
122122
}
123123

124+
static int parse_submodule_params(struct diff_options *options, const char *value)
125+
{
126+
if (!strcmp(value, "log"))
127+
DIFF_OPT_SET(options, SUBMODULE_LOG);
128+
else if (!strcmp(value, "short"))
129+
DIFF_OPT_CLR(options, SUBMODULE_LOG);
130+
else
131+
return -1;
132+
return 0;
133+
}
134+
124135
static int git_config_rename(const char *var, const char *value)
125136
{
126137
if (!value)
@@ -176,6 +187,13 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
176187
if (!strcmp(var, "diff.ignoresubmodules"))
177188
handle_ignore_submodules_arg(&default_diff_options, value);
178189

190+
if (!strcmp(var, "diff.submodule")) {
191+
if (parse_submodule_params(&default_diff_options, value))
192+
warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
193+
value);
194+
return 0;
195+
}
196+
179197
if (git_color_config(var, value, cb) < 0)
180198
return -1;
181199

@@ -3473,6 +3491,14 @@ static int parse_dirstat_opt(struct diff_options *options, const char *params)
34733491
return 1;
34743492
}
34753493

3494+
static int parse_submodule_opt(struct diff_options *options, const char *value)
3495+
{
3496+
if (parse_submodule_params(options, value))
3497+
die(_("Failed to parse --submodule option parameter: '%s'"),
3498+
value);
3499+
return 1;
3500+
}
3501+
34763502
int diff_opt_parse(struct diff_options *options, const char **av, int ac)
34773503
{
34783504
const char *arg = av[0];
@@ -3653,10 +3679,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
36533679
handle_ignore_submodules_arg(options, arg + 20);
36543680
} else if (!strcmp(arg, "--submodule"))
36553681
DIFF_OPT_SET(options, SUBMODULE_LOG);
3656-
else if (!prefixcmp(arg, "--submodule=")) {
3657-
if (!strcmp(arg + 12, "log"))
3658-
DIFF_OPT_SET(options, SUBMODULE_LOG);
3659-
}
3682+
else if (!prefixcmp(arg, "--submodule="))
3683+
return parse_submodule_opt(options, arg + 12);
36603684

36613685
/* misc options */
36623686
else if (!strcmp(arg, "-z"))

t/t4041-diff-submodule-option.sh

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ test_create_repo sm1 &&
3333
add_file . foo >/dev/null
3434

3535
head1=$(add_file sm1 foo1 foo2)
36+
fullhead1=$(cd sm1; git rev-list --max-count=1 $head1)
3637

3738
test_expect_success 'added submodule' "
3839
git add sm1 &&
@@ -43,6 +44,33 @@ EOF
4344
test_cmp expected actual
4445
"
4546

47+
test_expect_success 'added submodule, set diff.submodule' "
48+
git config diff.submodule log &&
49+
git add sm1 &&
50+
git diff --cached >actual &&
51+
cat >expected <<-EOF &&
52+
Submodule sm1 0000000...$head1 (new submodule)
53+
EOF
54+
git config --unset diff.submodule &&
55+
test_cmp expected actual
56+
"
57+
58+
test_expect_success '--submodule=short overrides diff.submodule' "
59+
test_config diff.submodule log &&
60+
git add sm1 &&
61+
git diff --submodule=short --cached >actual &&
62+
cat >expected <<-EOF &&
63+
diff --git a/sm1 b/sm1
64+
new file mode 160000
65+
index 0000000..a2c4dab
66+
--- /dev/null
67+
+++ b/sm1
68+
@@ -0,0 +1 @@
69+
+Subproject commit $fullhead1
70+
EOF
71+
test_cmp expected actual
72+
"
73+
4674
commit_file sm1 &&
4775
head2=$(add_file sm1 foo3)
4876

@@ -73,7 +101,6 @@ EOF
73101
test_cmp expected actual
74102
"
75103

76-
fullhead1=$(cd sm1; git rev-list --max-count=1 $head1)
77104
fullhead2=$(cd sm1; git rev-list --max-count=1 $head2)
78105
test_expect_success 'modified submodule(forward) --submodule=short' "
79106
git diff --submodule=short >actual &&

0 commit comments

Comments
 (0)