diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2019-06-06 09:48:30 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-06 09:48:30 -0400 |
commit | e50d138e898dd034161663873f75716086266f86 (patch) | |
tree | f792c67b58bc976ef824307e62d02280e39114c6 | |
parent | 4de6eb5b8ba559c1f18a6e7631d7800e294849c8 (diff) | |
parent | d81e7866aba52625aa3100764d77c73adba58c8e (diff) | |
download | libgit2-e50d138e898dd034161663873f75716086266f86.tar.gz |
Merge pull request #5095 from pks-t/pks/ignore-escaped-trailing-space
ignore: handle escaped trailing whitespace
-rw-r--r-- | src/attr_file.c | 22 | ||||
-rw-r--r-- | tests/attr/ignore.c | 46 |
2 files changed, 65 insertions, 3 deletions
diff --git a/src/attr_file.c b/src/attr_file.c index 51ecfbb8e..673f9a406 100644 --- a/src/attr_file.c +++ b/src/attr_file.c @@ -560,6 +560,21 @@ void git_attr_path__free(git_attr_path *info) */ /* + * Determine the length of trailing spaces. Escaped spaces do not count as + * trailing whitespace. + */ +static size_t trailing_space_length(const char *p, size_t len) +{ + size_t n; + for (n = len; n; n--) { + if ((p[n-1] != ' ' && p[n-1] != '\t') || + (n > 1 && p[n-2] == '\\')) + break; + } + return len - n; +} + +/* * This will return 0 if the spec was filled out, * GIT_ENOTFOUND if the fnmatch does not require matching, or * another error code there was an actual problem. @@ -647,9 +662,10 @@ int git_attr_fnmatch__parse( return GIT_ENOTFOUND; /* Remove trailing spaces. */ - while (pattern[spec->length - 1] == ' ' || pattern[spec->length - 1] == '\t') - if (--spec->length == 0) - return GIT_ENOTFOUND; + spec->length -= trailing_space_length(pattern, spec->length); + + if (spec->length == 0) + return GIT_ENOTFOUND; if (pattern[spec->length - 1] == '/') { spec->length--; diff --git a/tests/attr/ignore.c b/tests/attr/ignore.c index 1bf06fc1f..ea8a14192 100644 --- a/tests/attr/ignore.c +++ b/tests/attr/ignore.c @@ -61,6 +61,52 @@ void test_attr_ignore__ignore_space(void) assert_is_ignored(true, "NewFolder/NewFolder/File.txt"); } +void test_attr_ignore__intermittent_space(void) +{ + cl_git_rewritefile("attr/.gitignore", "foo bar\n"); + + assert_is_ignored(false, "foo"); + assert_is_ignored(false, "bar"); + assert_is_ignored(true, "foo bar"); +} + +void test_attr_ignore__trailing_space(void) +{ + cl_git_rewritefile( + "attr/.gitignore", + "foo \n" + "bar \n" + ); + + assert_is_ignored(true, "foo"); + assert_is_ignored(false, "foo "); + assert_is_ignored(true, "bar"); + assert_is_ignored(false, "bar "); + assert_is_ignored(false, "bar "); +} + +void test_attr_ignore__escaped_trailing_spaces(void) +{ + cl_git_rewritefile( + "attr/.gitignore", + "foo\\ \n" + "bar\\ \\ \n" + "baz \\ \n" + "qux\\ \n" + ); + + assert_is_ignored(false, "foo"); + assert_is_ignored(true, "foo "); + assert_is_ignored(false, "bar"); + assert_is_ignored(false, "bar "); + assert_is_ignored(true, "bar "); + assert_is_ignored(true, "baz "); + assert_is_ignored(false, "baz "); + assert_is_ignored(true, "qux "); + assert_is_ignored(false, "qux"); + assert_is_ignored(false, "qux "); +} + void test_attr_ignore__ignore_dir(void) { cl_git_rewritefile("attr/.gitignore", "dir/\n"); |