@matheusbsbs / matheus-bagliano / commits / ded0abc73c
Fork 0

diff: handle diffstat of rewritten binary files

The logic in builtin_diffstat assumes that a complete_rewrite pair should have its lines counted. This is nonsensical for binary files and leads to confusing things like: $ git diff --stat --summary HEAD^ HEAD foo.rand | Bin 4096 -> 4096 bytes 1 files changed, 0 insertions(+), 0 deletions(-) $ git diff --stat --summary -B HEAD^ HEAD foo.rand | 34 +++++++++++++++------------------- 1 files changed, 15 insertions(+), 19 deletions(-) rewrite foo.rand (100%) So let's reorder the function to handle binary files first (which from diffstat's perspective look like complete rewrites anyway), then rewrites, then actual diffstats. There are two bonus prizes to this reorder: 1. It gets rid of a now-superfluous goto. 2. The binary case is at the top, which means we can further optimize it in the next patch. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 19, 2011 at 03:04 UTC ded0abc73c67c6a9b9dbc2a22755fab01ae17e41
2 files changed +21 -10
diff.c
+14 -10
@@ -1811,26 +1811,31 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
1811 data->is_unmerged = 1;
1812 return;
1813 }
1814 - if (complete_rewrite) {
1814 +
1815 + if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
1816 + if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1817 + die("unable to read files to diff");
1818 + data->is_binary = 1;
1819 + data->added = mf2.size;
1820 + data->deleted = mf1.size;
1821 + }
1822 +
1823 + else if (complete_rewrite) {
1824 diff_populate_filespec(one, 0);
1825 diff_populate_filespec(two, 0);
1826 data->deleted = count_lines(one->data, one->size);
1827 data->added = count_lines(two->data, two->size);
1819 - goto free_and_return;
1828 }
1821 - if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1822 - die("unable to read files to diff");
1829
1824 - if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
1825 - data->is_binary = 1;
1826 - data->added = mf2.size;
1827 - data->deleted = mf1.size;
1828 - } else {
1830 + else {
1831 /* Crazy xdl interfaces.. */
1832 xpparam_t xpp;
1833 xdemitconf_t xecfg;
1834 xdemitcb_t ecb;
1835
1836 + if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1837 + die("unable to read files to diff");
1838 +
1839 memset(&xpp, 0, sizeof(xpp));
1840 memset(&xecfg, 0, sizeof(xecfg));
1841 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
@@ -1838,7 +1843,6 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
1843 &xpp, &xecfg, &ecb);
1844 }
1845
1841 - free_and_return:
1846 diff_free_filespec_data(one);
1847 diff_free_filespec_data(two);
1848 }
t/t4031-diff-rewrite-binary.sh
+7
@@ -44,6 +44,13 @@ test_expect_success 'rewrite diff can show binary patch' '
44 grep "GIT binary patch" diff
45 '
46
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 +
54 {
55 echo "#!$SHELL_PATH"
56 cat <<'EOF'