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

diff: fix modified lines stats with --stat and --numstat

Only skip diffstats when both oids are valid and identical. This check was causing both false-positives (files included in diffstats with no actual changes (0 lines modified) and false-negatives (showing 0 lines modified in stats when files had actually changed). Also replaced same_contents with may_differ to avoid confusion. Signed-off-by: Thomas Guyot-Sionnest <tguyot@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Guyot-Sionnest committed Sep 24, 2020 at 03:41 UTC ff0c7fa8cbd7f7d5e81c2501222b60f0aac30bb9
2 files changed +11 -13
diff.c
+7 -5
@@ -3663,7 +3663,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
3663 {
3664 mmfile_t mf1, mf2;
3665 struct diffstat_file *data;
3666 - int same_contents;
3666 + int may_differ;
3667 int complete_rewrite = 0;
3668
3669 if (!DIFF_PAIR_UNMERGED(p)) {
@@ -3681,12 +3681,14 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
3681 return;
3682 }
3683
3684 - same_contents = oideq(&one->oid, &two->oid);
3684 + /* saves some reads if true, not a guarantee of diff outcome */
3685 + may_differ = !(one->oid_valid && two->oid_valid &&
3686 + oideq(&one->oid, &two->oid));
3687
3688 if (diff_filespec_is_binary(o->repo, one) ||
3689 diff_filespec_is_binary(o->repo, two)) {
3690 data->is_binary = 1;
3689 - if (same_contents) {
3691 + if (!may_differ) {
3692 data->added = 0;
3693 data->deleted = 0;
3694 } else {
@@ -3702,7 +3704,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
3704 data->added = count_lines(two->data, two->size);
3705 }
3706
3705 - else if (!same_contents) {
3707 + else if (may_differ) {
3708 /* Crazy xdl interfaces.. */
3709 xpparam_t xpp;
3710 xdemitconf_t xecfg;
@@ -3727,7 +3729,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
3729 diffstat->files[diffstat->nr - 1];
3730 /*
3731 * Omit diffstats of modified files where nothing changed.
3730 - * Even if !same_contents, this might be the case due to
3732 + * Even if may_differ, this might be the case due to
3733 * ignoring whitespace changes, etc.
3734 *
3735 * But note that we special-case additions, deletions,
t/t3206-range-diff.sh
+4 -8
@@ -252,17 +252,13 @@ test_expect_success 'changed commit with --stat diff option' '
252 git range-diff --no-color --stat topic...changed >actual &&
253 cat >expect <<-EOF &&
254 1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
255 - a => b | 0
256 - 1 file changed, 0 insertions(+), 0 deletions(-)
255 2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
258 - a => b | 0
259 - 1 file changed, 0 insertions(+), 0 deletions(-)
256 3: $(test_oid t3) ! 3: $(test_oid c3) s/11/B/
261 - a => b | 0
262 - 1 file changed, 0 insertions(+), 0 deletions(-)
257 + a => b | 2 +-
258 + 1 file changed, 1 insertion(+), 1 deletion(-)
259 4: $(test_oid t4) ! 4: $(test_oid c4) s/12/B/
264 - a => b | 0
265 - 1 file changed, 0 insertions(+), 0 deletions(-)
260 + a => b | 2 +-
261 + 1 file changed, 1 insertion(+), 1 deletion(-)
262 EOF
263 test_cmp expect actual
264 '