Commit 63adcc4e3436abb163426ee431a6f77c29415b10

Edward Thomson 2019-05-19T16:27:59

attr: optionally treat leading whitespace as significant When `allow_space` is unset, ensure that leading whitespace is not skipped.

diff --git a/src/attr_file.c b/src/attr_file.c
index ddd4470..aef3e64 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -583,8 +583,11 @@ int git_attr_fnmatch__parse(
 
 	pattern = *base;
 
-	while (git__isspace(*pattern)) pattern++;
-	if (!*pattern || *pattern == '#') {
+	while (!allow_space && git__isspace(*pattern))
+		pattern++;
+
+	if (!*pattern || *pattern == '#' || *pattern == '\n' ||
+	    (*pattern == '\r' && *(pattern + 1) == '\n')) {
 		*base = git__next_line(pattern);
 		return GIT_ENOTFOUND;
 	}
@@ -606,8 +609,12 @@ int git_attr_fnmatch__parse(
 
 	slash_count = 0;
 	for (scan = pattern; *scan != '\0'; ++scan) {
-		/* scan until (non-escaped) white space */
-		if (git__isspace(*scan) && *(scan - 1) != '\\') {
+		/*
+		 * Scan until a non-escaped whitespace: find a whitespace, then look
+		 * one char backward to ensure that it's not prefixed by a `\`.
+		 * Only look backward if we're not at the first position (`pattern`).
+		 */
+		if (git__isspace(*scan) && scan > pattern && *(scan - 1) != '\\') {
 			if (!allow_space || (*scan != ' ' && *scan != '\t' && *scan != '\r'))
 				break;
 		}