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`.
This commit is contained in:
Martin von Zweigbergk 2022-03-09 23:15:02 -08:00 committed by Martin von Zweigbergk
parent e7a7cb8ea5
commit 51da2a2dc1
2 changed files with 21 additions and 22 deletions

View file

@ -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<GitIgnoreFile>,
prefix: &str,
file: PathBuf,
) -> Arc<GitIgnoreFile> {
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<dyn Iterator<Item = &GitIgnoreLine> + 'a> {
if let Some(parent) = &self.parent {
Box::new(self.lines.iter().rev().chain(parent.all_lines_reversed()))

View file

@ -291,21 +291,6 @@ impl TreeState {
self.store.write_symlink(path, str_target).unwrap()
}
fn try_chain_gitignore(
base: &Arc<GitIgnoreFile>,
prefix: &str,
file: PathBuf,
) -> Arc<GitIgnoreFile> {
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();