History

[config #185] add test for handling windows formatted files…

…which occour implicitly when checking out the repository fixtures on windows, which are then windows formatted. From there the unholy carriage return \r enters the compiled binary using include_bytes! as a template, which then is used to initialize a repository. In case of bare repositories, the implementation has to check if it really is bare (i.e. bare = true) or just looks bare (because there is no index, but that's the case in a new repository as well). This check would use the config parser, which wouldn't consider carriage returns and fail to parse the config file, making the opening of repositories impossible on windows.

Sebastian Thiel committed 5 years ago

Commit 2a2a89f1 parent 212c44c3 files · +23 −1Browse files

Unified · Split

3 files
  1. git-config/src/parser.rs +1 −1
  2. git-config/tests/fixtures/repo-config.crlf +14 −0
  3. git-config/tests/integration_tests/file_integeration_test.rs +8 −0
git-config/src/parser.rs+1−1
@@ -1367,7 +1367,7 @@ fn take_spaces(i: &[u8]) -> IResult<&[u8], &str> { · 1366 unmodified lines
13671367
13681368 fn take_newline(i: &[u8]) -> IResult<&[u8], (&str, usize)> {
13691369 let mut counter = 0;
1370− let (i, v) = take_while(|c| (c as char).is_ascii() && is_newline(c))(i)?;
1370+ let (i, v) = take_while(|c| (c as char).is_ascii() && (c == b'\r' || is_newline(c)))(i)?;
13711371 counter += v.len();
13721372 if v.is_empty() {
13731373 Err(nom::Err::Error(NomError {
git-config/tests/fixtures/repo-config.crlf+14−0
@@ -0,0 +1,14 @@
1+; hello
2+# world
3+
4+[core]
5+ repositoryformatversion = 0
6+ bare = true
7+[other]
8+ repositoryformatversion = 0
9+
10+ bare = true
11+
12+# before section with newline
13+[foo]
14+ repositoryformatversion = 0
git-config/tests/integration_tests/file_integeration_test.rs+8−0
@@ -1,6 +1,14 @@
11 use std::{borrow::Cow, convert::TryFrom};
22
33 use git_config::{file::GitConfig, values::*};
4+use std::path::Path;
5+
6+#[test]
7+fn parse_config_with_windows_line_endings_successfully() {
8+ GitConfig::open(Path::new("tests").join("fixtures").join("repo-config.crlf"))
9+ .map_err(|err| err.to_string())
10+ .unwrap();
11+}
412
513 /// Asserts we can cast into all variants of our type
614 #[test]