From ad1a46b817f6573545fc7039e6b0de1d83c23ae3 Mon Sep 17 00:00:00 2001 From: calebcartwright Date: Mon, 17 Jun 2019 19:38:59 -0500 Subject: [PATCH 1/4] fix: windows path support in ignore patterns --- src/ignore_path.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ignore_path.rs b/src/ignore_path.rs index d8974e12b8f..0fb04a02e8e 100644 --- a/src/ignore_path.rs +++ b/src/ignore_path.rs @@ -11,7 +11,7 @@ impl IgnorePathSet { let mut ignore_builder = gitignore::GitignoreBuilder::new(ignore_list.rustfmt_toml_path()); for ignore_path in ignore_list { - ignore_builder.add_line(None, ignore_path.to_str().unwrap())?; + ignore_builder.add_line(None, &ignore_path.to_str().unwrap().replace("\\", "/"))?; } Ok(IgnorePathSet { @@ -42,14 +42,19 @@ mod test { match option_env!("CFG_RELEASE_CHANNEL") { // this test requires nightly None | Some("nightly") => { - let config = - Config::from_toml(r#"ignore = ["foo.rs", "bar_dir/*"]"#, Path::new("")) - .unwrap(); + let config = Config::from_toml( + r#"ignore = ["foo.rs", "bar_dir/*", "foo\bar"]"#, + Path::new(""), + ) + .unwrap(); let ignore_path_set = IgnorePathSet::from_ignore_list(&config.ignore()).unwrap(); assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/foo.rs")))); assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz.rs")))); assert!(!ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/bar.rs")))); + assert!( + !ignore_path_set.is_match(&FileName::Real(PathBuf::from("foo/bar/true.rs"))) + ); } _ => (), }; From 16ef56f5a24f91a248652d57a2cdce7bf1c87887 Mon Sep 17 00:00:00 2001 From: calebcartwright Date: Tue, 18 Jun 2019 17:32:15 -0500 Subject: [PATCH 2/4] tests: update assertions for windows-style ignore paths --- src/ignore_path.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ignore_path.rs b/src/ignore_path.rs index 0fb04a02e8e..85f8b1d87e9 100644 --- a/src/ignore_path.rs +++ b/src/ignore_path.rs @@ -43,7 +43,7 @@ mod test { // this test requires nightly None | Some("nightly") => { let config = Config::from_toml( - r#"ignore = ["foo.rs", "bar_dir/*", "foo\bar"]"#, + r#"ignore = ["foo.rs", "bar_dir/*", "foo\\bar\\abc"]"#, Path::new(""), ) .unwrap(); @@ -53,7 +53,10 @@ mod test { assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz.rs")))); assert!(!ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/bar.rs")))); assert!( - !ignore_path_set.is_match(&FileName::Real(PathBuf::from("foo/bar/true.rs"))) + ignore_path_set.is_match(&FileName::Real(PathBuf::from("foo/bar/abc/true.rs"))) + ); + assert!( + !ignore_path_set.is_match(&FileName::Real(PathBuf::from("foo/bar/false.rs"))) ); } _ => (), From 71f57b2bfc8a0c9a561bd7dac2c46e3de4485fc4 Mon Sep 17 00:00:00 2001 From: calebcartwright Date: Wed, 26 Jun 2019 19:05:52 -0500 Subject: [PATCH 3/4] fix: only replace backslash in ignore paths on windows --- src/ignore_path.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/ignore_path.rs b/src/ignore_path.rs index 85f8b1d87e9..cf42c4c8ca0 100644 --- a/src/ignore_path.rs +++ b/src/ignore_path.rs @@ -11,7 +11,12 @@ impl IgnorePathSet { let mut ignore_builder = gitignore::GitignoreBuilder::new(ignore_list.rustfmt_toml_path()); for ignore_path in ignore_list { - ignore_builder.add_line(None, &ignore_path.to_str().unwrap().replace("\\", "/"))?; + let ignore_line = ignore_path.to_str().unwrap(); + if cfg!(windows) { + ignore_builder.add_line(None, &ignore_line.replace("\\", "/"))?; + } else { + ignore_builder.add_line(None, ignore_line)?; + }; } Ok(IgnorePathSet { @@ -52,12 +57,16 @@ mod test { assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/foo.rs")))); assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz.rs")))); assert!(!ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/bar.rs")))); - assert!( - ignore_path_set.is_match(&FileName::Real(PathBuf::from("foo/bar/abc/true.rs"))) - ); - assert!( - !ignore_path_set.is_match(&FileName::Real(PathBuf::from("foo/bar/false.rs"))) - ); + if cfg!(windows) { + assert!( + ignore_path_set + .is_match(&FileName::Real(PathBuf::from("foo/bar/abc/true.rs"))) + ); + assert!( + !ignore_path_set + .is_match(&FileName::Real(PathBuf::from("foo/bar/false.rs"))) + ); + } } _ => (), }; From 5b056ef809b53f5e8cffc96f37ec17634dedee94 Mon Sep 17 00:00:00 2001 From: calebcartwright Date: Fri, 28 Jun 2019 16:30:37 -0500 Subject: [PATCH 4/4] docs: update ignore setting docs to include path style info --- Configurations.md | 2 +- src/ignore_path.rs | 25 ++++--------------------- 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/Configurations.md b/Configurations.md index 304bc690034..17ac0467fc3 100644 --- a/Configurations.md +++ b/Configurations.md @@ -2326,7 +2326,7 @@ Copyright 2018 The Rust Project Developers.`, etc.: ## `ignore` Skip formatting files and directories that match the specified pattern. -The pattern format is the same as [.gitignore](https://git-scm.com/docs/gitignore#_pattern_format). +The pattern format is the same as [.gitignore](https://git-scm.com/docs/gitignore#_pattern_format). Be sure to use Unix/forwardslash `/` style paths. This path style will work on all platforms. Windows style paths with backslashes `\` are not supported. - **Default value**: format every file - **Possible values**: See an example below diff --git a/src/ignore_path.rs b/src/ignore_path.rs index cf42c4c8ca0..d8974e12b8f 100644 --- a/src/ignore_path.rs +++ b/src/ignore_path.rs @@ -11,12 +11,7 @@ impl IgnorePathSet { let mut ignore_builder = gitignore::GitignoreBuilder::new(ignore_list.rustfmt_toml_path()); for ignore_path in ignore_list { - let ignore_line = ignore_path.to_str().unwrap(); - if cfg!(windows) { - ignore_builder.add_line(None, &ignore_line.replace("\\", "/"))?; - } else { - ignore_builder.add_line(None, ignore_line)?; - }; + ignore_builder.add_line(None, ignore_path.to_str().unwrap())?; } Ok(IgnorePathSet { @@ -47,26 +42,14 @@ mod test { match option_env!("CFG_RELEASE_CHANNEL") { // this test requires nightly None | Some("nightly") => { - let config = Config::from_toml( - r#"ignore = ["foo.rs", "bar_dir/*", "foo\\bar\\abc"]"#, - Path::new(""), - ) - .unwrap(); + let config = + Config::from_toml(r#"ignore = ["foo.rs", "bar_dir/*"]"#, Path::new("")) + .unwrap(); let ignore_path_set = IgnorePathSet::from_ignore_list(&config.ignore()).unwrap(); assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/foo.rs")))); assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz.rs")))); assert!(!ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/bar.rs")))); - if cfg!(windows) { - assert!( - ignore_path_set - .is_match(&FileName::Real(PathBuf::from("foo/bar/abc/true.rs"))) - ); - assert!( - !ignore_path_set - .is_match(&FileName::Real(PathBuf::from("foo/bar/false.rs"))) - ); - } } _ => (), };