@matheusbsbs / matheus-bagliano / commits / a5a5a04863
Fork 0

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 <casey@nrlssc.navy.mil> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Brandon Casey committed Oct 1, 2008 at 14:28 UTC a5a5a04863036678919705979b9c0f3579fa3710
1 file changed +11 -1
xdiff-interface.c
+11 -1
@@ -191,12 +191,22 @@ struct ff_regs {
191 static long ff_regexp(const char *line, long len,
192 char *buffer, long buffer_size, void *priv)
193 {
194 - char *line_buffer = xstrndup(line, len); /* make NUL terminated */
194 + char *line_buffer;
195 struct ff_regs *regs = priv;
196 regmatch_t pmatch[2];
197 int i;
198 int result = -1;
199
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 +
210 for (i = 0; i < regs->nr; i++) {
211 struct ff_reg *reg = regs->array + i;
212 if (!regexec(&reg->re, line_buffer, 2, pmatch, 0)) {