|
@ -1,5 +1,6 @@ |
|
|
//! This module contains an abstraction for gitignore files
|
|
|
//! This module contains an abstraction for gitignore files
|
|
|
use crate::errors::*;
|
|
|
use crate::errors::*;
|
|
|
|
|
|
use crate::helpers::git_dir;
|
|
|
use log::trace;
|
|
|
use log::trace;
|
|
|
use std::fs::OpenOptions;
|
|
|
use std::fs::OpenOptions;
|
|
|
use std::path::PathBuf;
|
|
|
use std::path::PathBuf;
|
|
@ -19,6 +20,21 @@ impl Gitignore { |
|
|
Gitignore { path: path.clone() }
|
|
|
Gitignore { path: path.clone() }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Return a new `Gitignore` object from the default location
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This function gets the default .gitignore in the git root folder for the current working
|
|
|
|
|
|
/// directory
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # Errors
|
|
|
|
|
|
///
|
|
|
|
|
|
/// - `ErrorKind::NoGitRootFound` when there was .gitignore file found at the default location
|
|
|
|
|
|
///
|
|
|
|
|
|
pub fn from_default_path() -> Result<Self> {
|
|
|
|
|
|
let mut path = git_dir()?.ok_or(ErrorKind::NoGitRootFound)?;
|
|
|
|
|
|
path.push(".gitignore");
|
|
|
|
|
|
Ok(Self::from_path(&path))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Append a line to the file
|
|
|
/// Append a line to the file
|
|
|
pub fn add_line(&self, line: &str) -> Result<()> {
|
|
|
pub fn add_line(&self, line: &str) -> Result<()> {
|
|
|
use std::io::prelude::*;
|
|
|
use std::io::prelude::*;
|
|
@ -31,6 +47,17 @@ impl Gitignore { |
|
|
writeln!(file, "{}", line).chain_err(|| "Error while writing line to gitignore")?;
|
|
|
writeln!(file, "{}", line).chain_err(|| "Error while writing line to gitignore")?;
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Reads the contents of the gitignore file and adds them to buf
|
|
|
|
|
|
pub fn contents(&self, buf: &mut String) -> Result<()> {
|
|
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
|
|
.read(true)
|
|
|
|
|
|
.open(&self.path)
|
|
|
|
|
|
.chain_err(|| "Error while opening gitignore file")?;
|
|
|
|
|
|
file.read_to_string(buf)?;
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl std::fmt::Display for Gitignore {
|
|
|
impl std::fmt::Display for Gitignore {
|
|
|