|
|
@ -28,7 +28,6 @@ impl Gitignore { |
|
|
|
/// # 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");
|
|
|
@ -40,11 +39,30 @@ impl Gitignore { |
|
|
|
use std::io::prelude::*;
|
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.read(true)
|
|
|
|
.append(true)
|
|
|
|
.create(true)
|
|
|
|
.open(&self.path)
|
|
|
|
.chain_err(|| "Error while opening gitignore file")?;
|
|
|
|
writeln!(file, "{}", line).chain_err(|| "Error while writing line to gitignore")?;
|
|
|
|
|
|
|
|
let file_len = file.stream_len()?;
|
|
|
|
let newline = if file_len > 0 {
|
|
|
|
file.seek(std::io::SeekFrom::End(-1))?;
|
|
|
|
let mut buf: Vec<u8> = vec![0; 1];
|
|
|
|
file.read_exact(&mut buf)?;
|
|
|
|
if buf[0] == '\n' as u8 {
|
|
|
|
// The last line already ends with a newline
|
|
|
|
""
|
|
|
|
} else {
|
|
|
|
// Add a newline if the last line doesn't end with one
|
|
|
|
"\n"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The file is empty, so we don't need a newline
|
|
|
|
""
|
|
|
|
};
|
|
|
|
writeln!(file, "{}{}", newline, line)
|
|
|
|
.chain_err(|| "Error while writing line to gitignore")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|