Skip to content

Commit 563d5a2

Browse files
drafnelgitster
authored andcommitted
xdiff-interface.c: strip newline (and cr) from line before pattern matching
POSIX doth sayeth: "In the regular expression processing described in IEEE Std 1003.1-2001, the <newline> is regarded as an ordinary character and both a period and a non-matching list can match one. ... Those utilities (like grep) that do not allow <newline>s to match are responsible for eliminating any <newline> from strings before matching against the RE." Thus far git has not been removing the trailing newline from strings matched against regular expression patterns. This has the effect that (quoting Jonathan del Strother) "... a line containing just 'FUNCNAME' (terminated by a newline) will be matched by the pattern '^(FUNCNAME.$)' but not '^(FUNCNAME$)'", and more simply not '^FUNCNAME$'. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent b19d288 commit 563d5a2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

t/t4018-diff-funcname.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test_expect_success 'last regexp must not be negated' '
6969
grep "fatal: Last expression must not be negated:"
7070
'
7171

72-
test_expect_failure 'pattern which matches to end of line' '
72+
test_expect_success 'pattern which matches to end of line' '
7373
git config diff.java.funcname "Beer$" &&
7474
git diff --no-index Beer.java Beer-correct.java |
7575
grep "^@@.*@@ Beer"

xdiff-interface.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,21 @@ struct ff_regs {
179179
static long ff_regexp(const char *line, long len,
180180
char *buffer, long buffer_size, void *priv)
181181
{
182-
char *line_buffer = xstrndup(line, len); /* make NUL terminated */
182+
char *line_buffer;
183183
struct ff_regs *regs = priv;
184184
regmatch_t pmatch[2];
185185
int result = 0, i;
186186

187+
/* Exclude terminating newline (and cr) from matching */
188+
if (len > 0 && line[len-1] == '\n') {
189+
if (len > 1 && line[len-2] == '\r')
190+
len -= 2;
191+
else
192+
len--;
193+
}
194+
195+
line_buffer = xstrndup(line, len); /* make NUL terminated */
196+
187197
for (i = 0; i < regs->nr; i++) {
188198
struct ff_reg *reg = regs->array + i;
189199
if (reg->negate ^ !!regexec(&reg->re,

0 commit comments

Comments
 (0)