From: Johannes Altmanninger <aclopte@gmail.com>
To: git@vger.kernel.org
Cc: "Michał Kępień" <michal@isc.org>
Subject: git diff -I<regex> does not work with empty +/- lines
Date: Sun, 28 Nov 2021 10:15:21 +0100 [thread overview]
Message-ID: <20211128091521.7ysocurtt4qlgcf6@gmail.com> (raw)
diff -I<regex> suppresses hunks where all +/- lines match <regex>.
it is useful to filter away boilerplate changes.
Unfortunately, it doesn't help if a hunk has a blank line, because the one
obvious way to filter out blank lines (^$) match *every* line, surprisingly.
This is because for a line "01\n"
we have a zero-width match here ^$ (offset 3).
IOW, while we succesfully ignore deleted blank lines
printf '\n' | git diff --no-index - /dev/null -I'^$'
diff --git a/- b/-
deleted file mode 100644
we also ignore non-blank lines (very surprising)
printf 'non-blank-line\n' | git diff --no-index - /dev/null -I'^$'
diff --git a/- b/-
deleted file mode 100644
unless they don't end in a newline (special case)
printf 'line without ending newline' | git diff --no-index - /dev/null -I'^$'
diff --git a/- b/-
deleted file mode 100644
--- a/-
+++ /dev/null
@@ -1 +0,0 @@
-line without ending newline
\ No newline at end of file
This patch fixes the second example. Is this the right direction?
Do we want to honor core.eol, so we preserve the \r when we have Unix endings?
In any case -I<regex> won't be able to discern between "line\n" and "line"
but that's not important to me.
diff --git a/xdiff/xdiffi.c b/xdiff/xdiffi.c
index a4542c05b6..23325022b9 100644
--- a/xdiff/xdiffi.c
+++ b/xdiff/xdiffi.c
@@ -1016,10 +1016,17 @@ static void xdl_mark_ignorable_lines(xdchange_t *xscr, xdfenv_t *xe, long flags)
static int record_matches_regex(xrecord_t *rec, xpparam_t const *xpp) {
regmatch_t regmatch;
int i;
+ const char *end = rec->ptr + rec->size;
+
+ if (rec->size >= 2 && end[-2] == '\r' && end[-1] == '\n') {
+ end -= 2;
+ } else if (rec->size && end[-1] == '\n') {
+ end -= 1;
+ }
for (i = 0; i < xpp->ignore_regex_nr; i++)
- if (!regexec_buf(xpp->ignore_regex[i], rec->ptr, rec->size, 1,
- ®match, 0))
+ if (!regexec_buf(xpp->ignore_regex[i], rec->ptr,
+ end - rec->ptr, 1, ®match, 0))
return 1;
return 0;
next reply other threads:[~2021-11-28 9:19 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-28 9:15 Johannes Altmanninger [this message]
2021-11-28 23:26 ` git diff -I<regex> does not work with empty +/- lines Junio C Hamano
2021-11-29 4:54 ` Johannes Altmanninger
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=20211128091521.7ysocurtt4qlgcf6@gmail.com \
--to=aclopte@gmail.com \
--cc=git@vger.kernel.org \
--cc=michal@isc.org \
/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).