Skip to content

Commit ff0c7fa

Browse files
dermothgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e1cfff6 commit ff0c7fa

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

diff.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,7 +3663,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
36633663
{
36643664
mmfile_t mf1, mf2;
36653665
struct diffstat_file *data;
3666-
int same_contents;
3666+
int may_differ;
36673667
int complete_rewrite = 0;
36683668

36693669
if (!DIFF_PAIR_UNMERGED(p)) {
@@ -3681,12 +3681,14 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
36813681
return;
36823682
}
36833683

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));
36853687

36863688
if (diff_filespec_is_binary(o->repo, one) ||
36873689
diff_filespec_is_binary(o->repo, two)) {
36883690
data->is_binary = 1;
3689-
if (same_contents) {
3691+
if (!may_differ) {
36903692
data->added = 0;
36913693
data->deleted = 0;
36923694
} else {
@@ -3702,7 +3704,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
37023704
data->added = count_lines(two->data, two->size);
37033705
}
37043706

3705-
else if (!same_contents) {
3707+
else if (may_differ) {
37063708
/* Crazy xdl interfaces.. */
37073709
xpparam_t xpp;
37083710
xdemitconf_t xecfg;
@@ -3727,7 +3729,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
37273729
diffstat->files[diffstat->nr - 1];
37283730
/*
37293731
* 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
37313733
* ignoring whitespace changes, etc.
37323734
*
37333735
* But note that we special-case additions, deletions,

t/t3206-range-diff.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,13 @@ test_expect_success 'changed commit with --stat diff option' '
252252
git range-diff --no-color --stat topic...changed >actual &&
253253
cat >expect <<-EOF &&
254254
1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
255-
a => b | 0
256-
1 file changed, 0 insertions(+), 0 deletions(-)
257255
2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
258-
a => b | 0
259-
1 file changed, 0 insertions(+), 0 deletions(-)
260256
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(-)
263259
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(-)
266262
EOF
267263
test_cmp expect actual
268264
'

0 commit comments

Comments
 (0)