diff mbox series

diff: suppress --name-only paths where all hunks are ignored

Message ID 20201216231840.3163806-2-aclopte@gmail.com (mailing list archive)
State New, archived
Headers show
Series diff: suppress --name-only paths where all hunks are ignored | expand

Commit Message

Johannes Altmanninger Dec. 16, 2020, 11:18 p.m. UTC
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 mbox series

Patch

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" &&