@matheusbsbs / matheus-bagliano / commits / 563d5a2c84
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 563d5a2c84fb5daf77dd8bfe569e92566a8fd52b
2 files changed +12 -2
t/t4018-diff-funcname.sh
+1 -1
@@ -69,7 +69,7 @@ test_expect_success 'last regexp must not be negated' '
69 grep "fatal: Last expression must not be negated:"
70 '
71
72 -test_expect_failure 'pattern which matches to end of line' '
72 +test_expect_success 'pattern which matches to end of line' '
73 git config diff.java.funcname "Beer$" &&
74 git diff --no-index Beer.java Beer-correct.java |
75 grep "^@@.*@@ Beer"
xdiff-interface.c
+11 -1
@@ -179,11 +179,21 @@ struct ff_regs {
179 static long ff_regexp(const char *line, long len,
180 char *buffer, long buffer_size, void *priv)
181 {
182 - char *line_buffer = xstrndup(line, len); /* make NUL terminated */
182 + char *line_buffer;
183 struct ff_regs *regs = priv;
184 regmatch_t pmatch[2];
185 int result = 0, i;
186
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 +
197 for (i = 0; i < regs->nr; i++) {
198 struct ff_reg *reg = regs->array + i;
199 if (reg->negate ^ !!regexec(&reg->re,