Skip to content

Commit a5a5a04

Browse files
drafnelspearce
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 52e8370 commit a5a5a04

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

xdiff-interface.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,22 @@ struct ff_regs {
191191
static long ff_regexp(const char *line, long len,
192192
char *buffer, long buffer_size, void *priv)
193193
{
194-
char *line_buffer = xstrndup(line, len); /* make NUL terminated */
194+
char *line_buffer;
195195
struct ff_regs *regs = priv;
196196
regmatch_t pmatch[2];
197197
int i;
198198
int result = -1;
199199

200+
/* Exclude terminating newline (and cr) from matching */
201+
if (len > 0 && line[len-1] == '\n') {
202+
if (len > 1 && line[len-2] == '\r')
203+
len -= 2;
204+
else
205+
len--;
206+
}
207+
208+
line_buffer = xstrndup(line, len); /* make NUL terminated */
209+
200210
for (i = 0; i < regs->nr; i++) {
201211
struct ff_reg *reg = regs->array + i;
202212
if (!regexec(&reg->re, line_buffer, 2, pmatch, 0)) {

0 commit comments

Comments
 (0)