Skip to content

Commit 63adcc4

Browse files
committed
attr: optionally treat leading whitespace as significant
When `allow_space` is unset, ensure that leading whitespace is not skipped.
1 parent 7d33054 commit 63adcc4

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/attr_file.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,11 @@ int git_attr_fnmatch__parse(
583583

584584
pattern = *base;
585585

586-
while (git__isspace(*pattern)) pattern++;
587-
if (!*pattern || *pattern == '#') {
586+
while (!allow_space && git__isspace(*pattern))
587+
pattern++;
588+
589+
if (!*pattern || *pattern == '#' || *pattern == '\n' ||
590+
(*pattern == '\r' && *(pattern + 1) == '\n')) {
588591
*base = git__next_line(pattern);
589592
return GIT_ENOTFOUND;
590593
}
@@ -606,8 +609,12 @@ int git_attr_fnmatch__parse(
606609

607610
slash_count = 0;
608611
for (scan = pattern; *scan != '\0'; ++scan) {
609-
/* scan until (non-escaped) white space */
610-
if (git__isspace(*scan) && *(scan - 1) != '\\') {
612+
/*
613+
* Scan until a non-escaped whitespace: find a whitespace, then look
614+
* one char backward to ensure that it's not prefixed by a `\`.
615+
* Only look backward if we're not at the first position (`pattern`).
616+
*/
617+
if (git__isspace(*scan) && scan > pattern && *(scan - 1) != '\\') {
611618
if (!allow_space || (*scan != ' ' && *scan != '\t' && *scan != '\r'))
612619
break;
613620
}

0 commit comments

Comments
 (0)