From 51da2a2dc17a033be109b391b7f8dfd88e4441f7 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 9 Mar 2022 23:15:02 -0800 Subject: [PATCH] gitignore: move function for chaining .gitignore to central place (#65, #87) I want to be able to reuse the code for chaining two `.gitignore` files outside of `working_copy.rs`. --- lib/src/gitignore.rs | 18 ++++++++++++++++++ lib/src/working_copy.rs | 25 +++---------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/lib/src/gitignore.rs b/lib/src/gitignore.rs index d231c4747..88c257db8 100644 --- a/lib/src/gitignore.rs +++ b/lib/src/gitignore.rs @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::fs::File; +use std::io::Read; +use std::path::PathBuf; use std::sync::Arc; use itertools::Itertools; @@ -183,6 +186,21 @@ impl GitIgnoreFile { }) } + pub fn chain_with_file( + self: &Arc, + prefix: &str, + file: PathBuf, + ) -> Arc { + if file.is_file() { + let mut file = File::open(file).unwrap(); + let mut buf = Vec::new(); + file.read_to_end(&mut buf).unwrap(); + self.chain(prefix, &buf) + } else { + self.clone() + } + } + fn all_lines_reversed<'a>(&'a self) -> Box + 'a> { if let Some(parent) = &self.parent { Box::new(self.lines.iter().rev().chain(parent.all_lines_reversed())) diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index cdfa84577..492c0d55c 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -291,21 +291,6 @@ impl TreeState { self.store.write_symlink(path, str_target).unwrap() } - fn try_chain_gitignore( - base: &Arc, - prefix: &str, - file: PathBuf, - ) -> Arc { - if file.is_file() { - let mut file = File::open(file).unwrap(); - let mut buf = Vec::new(); - file.read_to_end(&mut buf).unwrap(); - base.chain(prefix, &buf) - } else { - base.clone() - } - } - // Look for changes to the working copy. If there are any changes, create // a new tree from it and return it, and also update the dirstate on disk. pub fn write_tree(&mut self) -> TreeId { @@ -316,8 +301,7 @@ impl TreeState { let mut git_ignore = GitIgnoreFile::empty(); if let Ok(home_dir) = std::env::var("HOME") { let home_dir_path = PathBuf::from(home_dir); - git_ignore = - TreeState::try_chain_gitignore(&git_ignore, "", home_dir_path.join(".gitignore")); + git_ignore = git_ignore.chain_with_file("", home_dir_path.join(".gitignore")); } let mut work = vec![(RepoPath::root(), self.working_copy_path.clone(), git_ignore)]; @@ -325,11 +309,8 @@ impl TreeState { let mut deleted_files: HashSet<_> = self.file_states.keys().cloned().collect(); while !work.is_empty() { let (dir, disk_dir, git_ignore) = work.pop().unwrap(); - let git_ignore = TreeState::try_chain_gitignore( - &git_ignore, - &dir.to_internal_dir_string(), - disk_dir.join(".gitignore"), - ); + let git_ignore = git_ignore + .chain_with_file(&dir.to_internal_dir_string(), disk_dir.join(".gitignore")); for maybe_entry in disk_dir.read_dir().unwrap() { let entry = maybe_entry.unwrap(); let file_type = entry.file_type().unwrap();