Skip to content

Commit 4e530c5

Browse files
committed
Merge branch 'jk/diffstat-binary' into maint
* jk/diffstat-binary: diff: don't retrieve binary blobs for diffstat diff: handle diffstat of rewritten binary files
2 parents 13a39e2 + abb371a commit 4e530c5

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

diff.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
245245
return 0;
246246
}
247247

248+
/* like fill_mmfile, but only for size, so we can avoid retrieving blob */
249+
static unsigned long diff_filespec_size(struct diff_filespec *one)
250+
{
251+
if (!DIFF_FILE_VALID(one))
252+
return 0;
253+
diff_populate_filespec(one, 1);
254+
return one->size;
255+
}
256+
248257
static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
249258
{
250259
char *ptr = mf->ptr;
@@ -2077,33 +2086,35 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
20772086
data->is_unmerged = 1;
20782087
return;
20792088
}
2080-
if (complete_rewrite) {
2089+
2090+
if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
2091+
data->is_binary = 1;
2092+
data->added = diff_filespec_size(two);
2093+
data->deleted = diff_filespec_size(one);
2094+
}
2095+
2096+
else if (complete_rewrite) {
20812097
diff_populate_filespec(one, 0);
20822098
diff_populate_filespec(two, 0);
20832099
data->deleted = count_lines(one->data, one->size);
20842100
data->added = count_lines(two->data, two->size);
2085-
goto free_and_return;
20862101
}
2087-
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
2088-
die("unable to read files to diff");
20892102

2090-
if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
2091-
data->is_binary = 1;
2092-
data->added = mf2.size;
2093-
data->deleted = mf1.size;
2094-
} else {
2103+
else {
20952104
/* Crazy xdl interfaces.. */
20962105
xpparam_t xpp;
20972106
xdemitconf_t xecfg;
20982107

2108+
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
2109+
die("unable to read files to diff");
2110+
20992111
memset(&xpp, 0, sizeof(xpp));
21002112
memset(&xecfg, 0, sizeof(xecfg));
21012113
xpp.flags = o->xdl_opts;
21022114
xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
21032115
&xpp, &xecfg);
21042116
}
21052117

2106-
free_and_return:
21072118
diff_free_filespec_data(one);
21082119
diff_free_filespec_data(two);
21092120
}

t/t4031-diff-rewrite-binary.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ test_expect_success 'rewrite diff can show binary patch' '
4444
grep "GIT binary patch" diff
4545
'
4646

47+
test_expect_success 'rewrite diff --stat shows binary changes' '
48+
git diff -B --stat --summary >diff &&
49+
grep "Bin" diff &&
50+
grep "0 insertions.*0 deletions" diff &&
51+
grep " rewrite file" diff
52+
'
53+
4754
{
4855
echo "#!$SHELL_PATH"
4956
cat <<'EOF'

0 commit comments

Comments
 (0)