Skip to content

Commit be95387

Browse files
committed
Merge branch 'rr/submodule-diff-config'
Allow "git diff --submodule=log" to set to be the default via configuration. * rr/submodule-diff-config: submodule: display summary header in bold diff: rename "set" variable diff: introduce diff.submodule configuration variable Documentation: move diff.wordRegex from config.txt to diff-config.txt
2 parents 5ab539b + 4e21513 commit be95387

File tree

7 files changed

+98
-24
lines changed

7 files changed

+98
-24
lines changed

Documentation/config.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -962,12 +962,6 @@ difftool.<tool>.cmd::
962962
difftool.prompt::
963963
Prompt before each invocation of the diff tool.
964964

965-
diff.wordRegex::
966-
A POSIX Extended Regular Expression used to determine what is a "word"
967-
when performing word-by-word difference calculations. Character
968-
sequences that match the regular expression are "words", all other
969-
characters are *ignorable* whitespace.
970-
971965
fetch.recurseSubmodules::
972966
This option can be either set to a boolean value or to 'on-demand'.
973967
Setting it to a boolean changes the behavior of fetch and pull to

Documentation/diff-config.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ 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+
117+
diff.wordRegex::
118+
A POSIX Extended Regular Expression used to determine what is a "word"
119+
when performing word-by-word difference calculations. Character
120+
sequences that match the regular expression are "words", all other
121+
characters are *ignorable* whitespace.
122+
110123
diff.<driver>.command::
111124
The custom diff driver command. See linkgit:gitattributes[5]
112125
for details.

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: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ static int parse_dirstat_params(struct diff_options *options, const char *params
123123
return ret;
124124
}
125125

126+
static int parse_submodule_params(struct diff_options *options, const char *value)
127+
{
128+
if (!strcmp(value, "log"))
129+
DIFF_OPT_SET(options, SUBMODULE_LOG);
130+
else if (!strcmp(value, "short"))
131+
DIFF_OPT_CLR(options, SUBMODULE_LOG);
132+
else
133+
return -1;
134+
return 0;
135+
}
136+
126137
static int git_config_rename(const char *var, const char *value)
127138
{
128139
if (!value)
@@ -178,6 +189,13 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
178189
if (!strcmp(var, "diff.ignoresubmodules"))
179190
handle_ignore_submodules_arg(&default_diff_options, value);
180191

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

@@ -2223,7 +2241,7 @@ static void builtin_diff(const char *name_a,
22232241
mmfile_t mf1, mf2;
22242242
const char *lbl[2];
22252243
char *a_one, *b_two;
2226-
const char *set = diff_get_color_opt(o, DIFF_METAINFO);
2244+
const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
22272245
const char *reset = diff_get_color_opt(o, DIFF_RESET);
22282246
const char *a_prefix, *b_prefix;
22292247
struct userdiff_driver *textconv_one = NULL;
@@ -2244,7 +2262,7 @@ static void builtin_diff(const char *name_a,
22442262
const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
22452263
show_submodule_summary(o->file, one ? one->path : two->path,
22462264
one->sha1, two->sha1, two->dirty_submodule,
2247-
del, add, reset);
2265+
meta, del, add, reset);
22482266
return;
22492267
}
22502268

@@ -2270,24 +2288,24 @@ static void builtin_diff(const char *name_a,
22702288
b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
22712289
lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
22722290
lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
2273-
strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, set, a_one, b_two, reset);
2291+
strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset);
22742292
if (lbl[0][0] == '/') {
22752293
/* /dev/null */
2276-
strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, set, two->mode, reset);
2294+
strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
22772295
if (xfrm_msg)
22782296
strbuf_addstr(&header, xfrm_msg);
22792297
must_show_header = 1;
22802298
}
22812299
else if (lbl[1][0] == '/') {
2282-
strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, set, one->mode, reset);
2300+
strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
22832301
if (xfrm_msg)
22842302
strbuf_addstr(&header, xfrm_msg);
22852303
must_show_header = 1;
22862304
}
22872305
else {
22882306
if (one->mode != two->mode) {
2289-
strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, set, one->mode, reset);
2290-
strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, set, two->mode, reset);
2307+
strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
2308+
strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
22912309
must_show_header = 1;
22922310
}
22932311
if (xfrm_msg)
@@ -3480,6 +3498,14 @@ static int parse_dirstat_opt(struct diff_options *options, const char *params)
34803498
return 1;
34813499
}
34823500

3501+
static int parse_submodule_opt(struct diff_options *options, const char *value)
3502+
{
3503+
if (parse_submodule_params(options, value))
3504+
die(_("Failed to parse --submodule option parameter: '%s'"),
3505+
value);
3506+
return 1;
3507+
}
3508+
34833509
int diff_opt_parse(struct diff_options *options, const char **av, int ac)
34843510
{
34853511
const char *arg = av[0];
@@ -3660,10 +3686,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
36603686
handle_ignore_submodules_arg(options, arg + 20);
36613687
} else if (!strcmp(arg, "--submodule"))
36623688
DIFF_OPT_SET(options, SUBMODULE_LOG);
3663-
else if (!prefixcmp(arg, "--submodule=")) {
3664-
if (!strcmp(arg + 12, "log"))
3665-
DIFF_OPT_SET(options, SUBMODULE_LOG);
3666-
}
3689+
else if (!prefixcmp(arg, "--submodule="))
3690+
return parse_submodule_opt(options, arg + 12);
36673691

36683692
/* misc options */
36693693
else if (!strcmp(arg, "-z"))

submodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
258258

259259
void show_submodule_summary(FILE *f, const char *path,
260260
unsigned char one[20], unsigned char two[20],
261-
unsigned dirty_submodule,
261+
unsigned dirty_submodule, const char *meta,
262262
const char *del, const char *add, const char *reset)
263263
{
264264
struct rev_info rev;
@@ -292,15 +292,15 @@ void show_submodule_summary(FILE *f, const char *path,
292292
return;
293293
}
294294

295-
strbuf_addf(&sb, "Submodule %s %s..", path,
295+
strbuf_addf(&sb, "%sSubmodule %s %s..", meta, path,
296296
find_unique_abbrev(one, DEFAULT_ABBREV));
297297
if (!fast_backward && !fast_forward)
298298
strbuf_addch(&sb, '.');
299299
strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
300300
if (message)
301-
strbuf_addf(&sb, " %s\n", message);
301+
strbuf_addf(&sb, " %s%s\n", message, reset);
302302
else
303-
strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
303+
strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
304304
fwrite(sb.buf, sb.len, 1, f);
305305

306306
if (!message) {

submodule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void handle_ignore_submodules_arg(struct diff_options *diffopt, const char *);
2020
int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
2121
void show_submodule_summary(FILE *f, const char *path,
2222
unsigned char one[20], unsigned char two[20],
23-
unsigned dirty_submodule,
23+
unsigned dirty_submodule, const char *meta,
2424
const char *del, const char *add, const char *reset);
2525
void set_config_fetch_recurse_submodules(int value);
2626
void check_for_new_submodule_commits(unsigned char new_sha1[20]);

t/t4041-diff-submodule-option.sh

Lines changed: 43 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,48 @@ 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+
74+
test_expect_success 'diff.submodule does not affect plumbing' '
75+
test_config diff.submodule log &&
76+
git diff-index -p HEAD >actual &&
77+
cat >expected <<-EOF &&
78+
diff --git a/sm1 b/sm1
79+
new file mode 160000
80+
index 0000000..a2c4dab
81+
--- /dev/null
82+
+++ b/sm1
83+
@@ -0,0 +1 @@
84+
+Subproject commit $fullhead1
85+
EOF
86+
test_cmp expected actual
87+
'
88+
4689
commit_file sm1 &&
4790
head2=$(add_file sm1 foo3)
4891

@@ -73,7 +116,6 @@ EOF
73116
test_cmp expected actual
74117
"
75118

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

0 commit comments

Comments
 (0)