git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Junio C Hamano <gitster@pobox.com>,
	Git Mailing List <git@vger.kernel.org>
Subject: Fix "git diff --stat" for interesting - but empty - file changes
Date: Wed, 17 Oct 2012 10:00:37 -0700	[thread overview]
Message-ID: <CA+55aFz88GPJcfMSqiyY+u0Cdm48bEyrsTGxHVJbGsYsDg=Q5w@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2306 bytes --]

The behavior of "git diff --stat" is rather odd for files that have
zero lines of changes: it will discount them entirely unless they were
renames.

Which means that the stat output will simply not show files that only
had "other" changes: they were created or deleted, or their mode was
changed.

Now, those changes do show up in the summary, but so do renames, so
the diffstat logic is inconsistent. Why does it show renames with zero
lines changed, but not mode changes or added files with zero lines
changed?

So change the logic to not check for "is_renamed", but for
"is_interesting" instead, where "interesting" is judged to be any
action but a pure data change (because a pure data change with zero
data changed really isn't worth showing, if we ever get one in our
diffpairs).

So if you did

   chmod +x Makefile
   git diff --stat

before, it would show empty (" 0 files changed"), with this it shows

 Makefile | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

which I think is a more correct diffstat (and then with "--summary" it
shows *what* the metadata change to Makefile was - this is completely
consistent with our handling of renamed files).

Side note: the old behavior was *really* odd. With no changes at all,
"git diff --stat" output was empty. With just a chmod, it said "0
files changed". No way is our legacy behavior sane.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---

This was triggered by kernel developers not noticing that they had
added zero-sized files, because those additions never showed up in the
diffstat.

NOTE! This does break two of our tests, so we clearly did this on
purpose, or at least tested for it. I just uncommented the subtests
that this makes irrelevant, and changed the output of another one.

Another test was simply buggy. It used "git diff --root cmit", and
thought that would be the diff against root. It isn't, and never has
been. It just happened to give the same (no file) output before.
Fixing --stat to show new files showed how buggy the test was. The
"--root" thing matters for "git show" or "git log" (when showing a
root commit) and for "git diff-tree" with a single tree.

Maybe we would *want* to make "git diff --root <cmit>" be the "diff
between root and cmit", but that's not what it actually is.

Comments?

[-- Attachment #2: patch.diff --]
[-- Type: application/octet-stream, Size: 6596 bytes --]

 diff.c                        | 25 +++++++++++++----------
 t/t4006-diff-mode.sh          | 46 +++++++++++++++++++++----------------------
 t/t4049-diff-stat-count.sh    |  3 ++-
 t/t4205-log-pretty-formats.sh |  4 ++--
 4 files changed, 42 insertions(+), 36 deletions(-)

diff --git a/diff.c b/diff.c
index 35d3f073859a..95bbad66c686 100644
--- a/diff.c
+++ b/diff.c
@@ -1300,6 +1300,7 @@ struct diffstat_t {
 		unsigned is_unmerged:1;
 		unsigned is_binary:1;
 		unsigned is_renamed:1;
+		unsigned is_interesting:1;
 		uintmax_t added, deleted;
 	} **files;
 };
@@ -1469,7 +1470,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
 	for (i = 0; (i < count) && (i < data->nr); i++) {
 		struct diffstat_file *file = data->files[i];
 		uintmax_t change = file->added + file->deleted;
-		if (!data->files[i]->is_renamed &&
+		if (!data->files[i]->is_interesting &&
 			 (change == 0)) {
 			count++; /* not shown == room for one more */
 			continue;
@@ -1590,7 +1591,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
 		uintmax_t deleted = data->files[i]->deleted;
 		int name_len;
 
-		if (!data->files[i]->is_renamed &&
+		if (!data->files[i]->is_interesting &&
 			 (added + deleted == 0)) {
 			total_files--;
 			continue;
@@ -1669,7 +1670,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
 	for (i = count; i < data->nr; i++) {
 		uintmax_t added = data->files[i]->added;
 		uintmax_t deleted = data->files[i]->deleted;
-		if (!data->files[i]->is_renamed &&
+		if (!data->files[i]->is_interesting &&
 			 (added + deleted == 0)) {
 			total_files--;
 			continue;
@@ -1697,7 +1698,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
 
 		if (data->files[i]->is_unmerged)
 			continue;
-		if (!data->files[i]->is_renamed && (added + deleted == 0)) {
+		if (!data->files[i]->is_interesting && (added + deleted == 0)) {
 			total_files--;
 		} else if (!data->files[i]->is_binary) { /* don't count bytes */
 			adds += added;
@@ -2397,13 +2398,20 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
 			     struct diff_filespec *two,
 			     struct diffstat_t *diffstat,
 			     struct diff_options *o,
-			     int complete_rewrite)
+			     struct diff_filepair *p)
 {
 	mmfile_t mf1, mf2;
 	struct diffstat_file *data;
 	int same_contents;
+	int complete_rewrite = 0;
+
+	if (!DIFF_PAIR_UNMERGED(p)) {
+		if (p->status == DIFF_STATUS_MODIFIED && p->score)
+			complete_rewrite = 1;
+	}
 
 	data = diffstat_add(diffstat, name_a, name_b);
+	data->is_interesting = p->status != 0;
 
 	if (!one || !two) {
 		data->is_unmerged = 1;
@@ -3114,11 +3122,10 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
 {
 	const char *name;
 	const char *other;
-	int complete_rewrite = 0;
 
 	if (DIFF_PAIR_UNMERGED(p)) {
 		/* unmerged */
-		builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
+		builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, p);
 		return;
 	}
 
@@ -3131,9 +3138,7 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
 	diff_fill_sha1_info(p->one);
 	diff_fill_sha1_info(p->two);
 
-	if (p->status == DIFF_STATUS_MODIFIED && p->score)
-		complete_rewrite = 1;
-	builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
+	builtin_diffstat(name, other, p->one, p->two, diffstat, o, p);
 }
 
 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
diff --git a/t/t4006-diff-mode.sh b/t/t4006-diff-mode.sh
index 3d4b1ba23f9e..05911492ca6d 100755
--- a/t/t4006-diff-mode.sh
+++ b/t/t4006-diff-mode.sh
@@ -32,28 +32,28 @@ test_expect_success 'prepare binary file' '
 	git commit -m binbin
 '
 
-test_expect_success '--stat output after text chmod' '
-	test_chmod -x rezrov &&
-	echo " 0 files changed" >expect &&
-	git diff HEAD --stat >actual &&
-	test_i18ncmp expect actual
-'
-
-test_expect_success '--shortstat output after text chmod' '
-	git diff HEAD --shortstat >actual &&
-	test_i18ncmp expect actual
-'
-
-test_expect_success '--stat output after binary chmod' '
-	test_chmod +x binbin &&
-	echo " 0 files changed" >expect &&
-	git diff HEAD --stat >actual &&
-	test_i18ncmp expect actual
-'
-
-test_expect_success '--shortstat output after binary chmod' '
-	git diff HEAD --shortstat >actual &&
-	test_i18ncmp expect actual
-'
+# test_expect_success '--stat output after text chmod' '
+# 	test_chmod -x rezrov &&
+# 	echo " 0 files changed" >expect &&
+# 	git diff HEAD --stat >actual &&
+#	test_i18ncmp expect actual
+# '
+#
+# test_expect_success '--shortstat output after text chmod' '
+# 	git diff HEAD --shortstat >actual &&
+# 	test_i18ncmp expect actual
+# '
+#
+# test_expect_success '--stat output after binary chmod' '
+# 	test_chmod +x binbin &&
+# 	echo " 0 files changed" >expect &&
+# 	git diff HEAD --stat >actual &&
+# 	test_i18ncmp expect actual
+# '
+#
+# test_expect_success '--shortstat output after binary chmod' '
+# 	git diff HEAD --shortstat >actual &&
+# 	test_i18ncmp expect actual
+# '
 
 test_done
diff --git a/t/t4049-diff-stat-count.sh b/t/t4049-diff-stat-count.sh
index b41eb61ca8b1..7b3ef00533f7 100755
--- a/t/t4049-diff-stat-count.sh
+++ b/t/t4049-diff-stat-count.sh
@@ -16,7 +16,8 @@ test_expect_success setup '
 	cat >expect <<-\EOF
 	 a | 1 +
 	 b | 1 +
-	 2 files changed, 2 insertions(+)
+	 ...
+	 4 files changed, 2 insertions(+)
 	EOF
 	git diff --stat --stat-count=2 >actual &&
 	test_i18ncmp expect actual
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 2c45de7aeaf2..98a43d457a3d 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -85,7 +85,7 @@ test_expect_success 'NUL termination' '
 
 test_expect_success 'NUL separation with --stat' '
 	stat0_part=$(git diff --stat HEAD^ HEAD) &&
-	stat1_part=$(git diff --stat --root HEAD^) &&
+	stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
 	printf "add bar\n$stat0_part\n\0initial\n$stat1_part\n" >expected &&
 	git log -z --stat --pretty="format:%s" >actual &&
 	test_i18ncmp expected actual
@@ -93,7 +93,7 @@ test_expect_success 'NUL separation with --stat' '
 
 test_expect_failure 'NUL termination with --stat' '
 	stat0_part=$(git diff --stat HEAD^ HEAD) &&
-	stat1_part=$(git diff --stat --root HEAD^) &&
+	stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
 	printf "add bar\n$stat0_part\n\0initial\n$stat1_part\n\0" >expected &&
 	git log -z --stat --pretty="tformat:%s" >actual &&
 	test_i18ncmp expected actual

             reply	other threads:[~2012-10-17 17:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-17 17:00 Linus Torvalds [this message]
2012-10-17 18:28 ` Fix "git diff --stat" for interesting - but empty - file changes Junio C Hamano
2012-10-17 19:35   ` Linus Torvalds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+55aFz88GPJcfMSqiyY+u0Cdm48bEyrsTGxHVJbGsYsDg=Q5w@mail.gmail.com' \
    --to=torvalds@linux-foundation.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).