From: Johannes Altmanninger <aclopte@gmail.com>
To: gitster@pobox.com
Cc: aclopte@gmail.com, git@vger.kernel.org
Subject: [PATCH] diff: suppress --name-only paths where all hunks are ignored
Date: Thu, 17 Dec 2020 00:18:40 +0100 [thread overview]
Message-ID: <20201216231840.3163806-2-aclopte@gmail.com> (raw)
In-Reply-To: <20201216231840.3163806-1-aclopte@gmail.com>
Diff options -w and the new -I<regex> can be used to suppress some hunks. Honor
these ignores in combination with --name-only, --name-stat, and --raw,
to not output files where all hunks are ignored.
Commit f245194f9a made "git diff -w --exit-code" exit with zero if all
changed hunks are whitespace. This uses the diff_from_contents bit.
Set that also when given -I<regex>, for consistent exit codes.
The diff_from_contents bit means that we have to look at content
changes to know if a path has changed - modulo ignored hunks. Teach
diff.c::flush_one_pair() to do so. In the caller, reset the found_changes bit
after each file pair, so we can test each file separately for content changes.
Signed-off-by: Johannes Altmanninger <aclopte@gmail.com>
---
diff.c | 36 +++++++++++++++++++++++++++++++-----
t/t4013-diff-various.sh | 13 +++++++++++++
t/t4015-diff-whitespace.sh | 9 +++++++++
3 files changed, 53 insertions(+), 5 deletions(-)
diff --git a/diff.c b/diff.c
index 643f4f3f6d..560f2d5fad 100644
--- a/diff.c
+++ b/diff.c
@@ -4630,11 +4630,10 @@ void diff_setup_done(struct diff_options *options)
/*
* Most of the time we can say "there are changes"
* only by checking if there are changed paths, but
- * --ignore-whitespace* options force us to look
- * inside contents.
+ * --ignore-* options force us to look inside contents.
*/
- if ((options->xdl_opts & XDF_WHITESPACE_FLAGS))
+ if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) || options->ignore_regex)
options->flags.diff_from_contents = 1;
else
options->flags.diff_from_contents = 0;
@@ -5967,6 +5966,26 @@ static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
{
int fmt = opt->output_format;
+ if (opt->flags.diff_from_contents &&
+ (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME | DIFF_FORMAT_NAME_STATUS))) {
+ static FILE *devnull;
+ FILE *diff_file;
+
+ if (!devnull)
+ devnull = xfopen("/dev/null", "w");
+
+ diff_file = opt->file;
+ opt->file = devnull;
+ opt->color_moved = 0;
+
+ if (check_pair_status(p))
+ diff_flush_patch(p, opt);
+
+ opt->file = diff_file;
+ if (!opt->found_changes)
+ return;
+ }
+
if (fmt & DIFF_FORMAT_CHECKDIFF)
diff_flush_checkdiff(p, opt);
else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
@@ -6350,11 +6369,18 @@ void diff_flush(struct diff_options *options)
DIFF_FORMAT_NAME |
DIFF_FORMAT_NAME_STATUS |
DIFF_FORMAT_CHECKDIFF)) {
+ int found_changes = 0;
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
- if (check_pair_status(p))
- flush_one_pair(p, options);
+ if (!check_pair_status(p))
+ continue;
+ flush_one_pair(p, options);
+ if (options->found_changes) {
+ found_changes = 1;
+ options->found_changes = 0;
+ }
}
+ options->found_changes = found_changes;
separator++;
}
diff --git a/t/t4013-diff-various.sh b/t/t4013-diff-various.sh
index f72d456d3b..7cfd3a22d1 100755
--- a/t/t4013-diff-various.sh
+++ b/t/t4013-diff-various.sh
@@ -509,6 +509,19 @@ test_expect_success 'diff -I<regex> --stat' '
test_cmp expect actual
'
+test_expect_success 'diff -I<regex> --name-only' '
+ git diff -I "" >actual --exit-code &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'diff -I<regex> --name-status' '
+ ! git diff -I"[0-9]" --name-status --exit-code >actual &&
+ cat >expect <<-\EOF &&
+ M file0
+ EOF
+ test_cmp expect actual
+'
+
test_expect_success 'diff -I<regex>: detect malformed regex' '
test_expect_code 129 git diff --ignore-matching-lines="^[124-9" 2>error &&
test_i18ngrep "invalid regex given to -I: " error
diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
index 47f0e2889d..3c4941cf96 100755
--- a/t/t4015-diff-whitespace.sh
+++ b/t/t4015-diff-whitespace.sh
@@ -805,6 +805,15 @@ test_expect_success 'whitespace-only changes not reported (diffstat)' '
test_must_be_empty actual
'
+test_expect_success 'whitespace-only changes not reported (name-only)' '
+ # reuse state from previous test
+ ! git diff --name-only >actual --exit-code &&
+ git diff --name-only -b >actual --exit-code &&
+ git diff --name-status -b >>actual &&
+ git diff --raw -b >>actual &&
+ test_must_be_empty actual
+'
+
test_expect_success 'whitespace changes with modification reported (diffstat)' '
git reset --hard &&
echo >x "hello world" &&
--
2.29.2
next prev parent reply other threads:[~2020-12-16 23:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-14 19:00 Unexpected behavior on diff -I<regex> --name-only Johannes Altmanninger
2020-12-14 19:49 ` Junio C Hamano
2020-12-16 23:18 ` Johannes Altmanninger
2020-12-16 23:18 ` Johannes Altmanninger [this message]
2020-12-17 1:27 ` Re* [PATCH] diff: suppress --name-only paths where all hunks are ignored Junio C Hamano
2020-12-20 22:34 ` Johannes Altmanninger
2020-12-21 19:29 ` Junio C Hamano
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201216231840.3163806-2-aclopte@gmail.com \
--to=aclopte@gmail.com \
--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.
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 NNTP newsgroup(s).