From c5daf286e5ccb3439b5082bbea33d678065bd565 Mon Sep 17 00:00:00 2001 From: Finkelman Date: Fri, 20 Jul 2018 11:35:36 -0400 Subject: [PATCH 1/2] work around for windows path separator for is_path_ignored --- src/repo.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/repo.rs b/src/repo.rs index 4edc5f3616..c558d747ba 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -2043,6 +2043,20 @@ impl Repository { } /// Test if the ignore rules apply to a given path. + #[cfg(windows)] + pub fn is_path_ignored>(&self, path: P) -> Result { + // `git_ignore_path_is_ignored` dose not work with windows path separator + // so we convert \ to / + let path = try!(::std::ffi::CString::new(path.as_ref().to_string_lossy().replace('\\', "/"))); + let mut ignored: c_int = 0; + unsafe { + try_call!(raw::git_ignore_path_is_ignored(&mut ignored, self.raw, path)); + } + Ok(ignored == 1) + } + + /// Test if the ignore rules apply to a given path. + #[cfg(not(windows))] pub fn is_path_ignored>(&self, path: P) -> Result { let path = try!(path.as_ref().into_c_string()); let mut ignored: c_int = 0; @@ -2540,4 +2554,18 @@ mod tests { let _ = repo.clear_ignore_rules(); assert!(!repo.is_path_ignored(Path::new("/foo")).unwrap()); } + + #[test] + #[cfg(windows)] + fn windows_is_path_ignored() { + let (_td, repo) = graph_repo_init(); + + assert!(!repo.is_path_ignored(Path::new("/foo")).unwrap()); + + let _ = repo.add_ignore_rule("/foo"); + assert!(repo.is_path_ignored(Path::new("\\foo\\thing")).unwrap()); + + let _ = repo.clear_ignore_rules(); + assert!(!repo.is_path_ignored(Path::new("\\foo\\thing")).unwrap()); + } } From 4de36b5c764e7515d13596e46668890d6d38ebfd Mon Sep 17 00:00:00 2001 From: Finkelman Date: Fri, 20 Jul 2018 14:06:39 -0400 Subject: [PATCH 2/2] use cfg! --- src/repo.rs | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/src/repo.rs b/src/repo.rs index c558d747ba..43421a61da 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -2043,22 +2043,14 @@ impl Repository { } /// Test if the ignore rules apply to a given path. - #[cfg(windows)] pub fn is_path_ignored>(&self, path: P) -> Result { - // `git_ignore_path_is_ignored` dose not work with windows path separator - // so we convert \ to / - let path = try!(::std::ffi::CString::new(path.as_ref().to_string_lossy().replace('\\', "/"))); - let mut ignored: c_int = 0; - unsafe { - try_call!(raw::git_ignore_path_is_ignored(&mut ignored, self.raw, path)); - } - Ok(ignored == 1) - } - - /// Test if the ignore rules apply to a given path. - #[cfg(not(windows))] - pub fn is_path_ignored>(&self, path: P) -> Result { - let path = try!(path.as_ref().into_c_string()); + let path = if cfg!(windows) { + // `git_ignore_path_is_ignored` dose not work with windows path separator + // so we convert \ to / + try!(::std::ffi::CString::new(path.as_ref().to_string_lossy().replace('\\', "/"))) + } else { + try!(path.as_ref().into_c_string()) + }; let mut ignored: c_int = 0; unsafe { try_call!(raw::git_ignore_path_is_ignored(&mut ignored, self.raw, path)); @@ -2550,22 +2542,15 @@ mod tests { let _ = repo.add_ignore_rule("/foo"); assert!(repo.is_path_ignored(Path::new("/foo")).unwrap()); + if cfg!(windows){ + assert!(repo.is_path_ignored(Path::new("\\foo\\thing")).unwrap()); + } - let _ = repo.clear_ignore_rules(); - assert!(!repo.is_path_ignored(Path::new("/foo")).unwrap()); - } - - #[test] - #[cfg(windows)] - fn windows_is_path_ignored() { - let (_td, repo) = graph_repo_init(); - - assert!(!repo.is_path_ignored(Path::new("/foo")).unwrap()); - - let _ = repo.add_ignore_rule("/foo"); - assert!(repo.is_path_ignored(Path::new("\\foo\\thing")).unwrap()); let _ = repo.clear_ignore_rules(); - assert!(!repo.is_path_ignored(Path::new("\\foo\\thing")).unwrap()); + assert!(!repo.is_path_ignored(Path::new("/foo")).unwrap()); + if cfg!(windows){ + assert!(!repo.is_path_ignored(Path::new("\\foo\\thing")).unwrap()); + } } }