|
@ -28,7 +28,6 @@ impl Gitignore { |
|
|
/// # Errors
|
|
|
/// # Errors
|
|
|
///
|
|
|
///
|
|
|
/// - `ErrorKind::NoGitRootFound` when there was .gitignore file found at the default location
|
|
|
/// - `ErrorKind::NoGitRootFound` when there was .gitignore file found at the default location
|
|
|
///
|
|
|
|
|
|
pub fn from_default_path() -> Result<Self> {
|
|
|
pub fn from_default_path() -> Result<Self> {
|
|
|
let mut path = git_dir()?.ok_or(ErrorKind::NoGitRootFound)?;
|
|
|
let mut path = git_dir()?.ok_or(ErrorKind::NoGitRootFound)?;
|
|
|
path.push(".gitignore");
|
|
|
path.push(".gitignore");
|
|
@ -40,11 +39,30 @@ impl Gitignore { |
|
|
use std::io::prelude::*;
|
|
|
use std::io::prelude::*;
|
|
|
let mut file = OpenOptions::new()
|
|
|
let mut file = OpenOptions::new()
|
|
|
.write(true)
|
|
|
.write(true)
|
|
|
|
|
|
.read(true)
|
|
|
.append(true)
|
|
|
.append(true)
|
|
|
.create(true)
|
|
|
.create(true)
|
|
|
.open(&self.path)
|
|
|
.open(&self.path)
|
|
|
.chain_err(|| "Error while opening gitignore file")?;
|
|
|
.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(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
@ -64,4 +82,4 @@ impl std::fmt::Display for Gitignore { |
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
|
write!(f, "gitignore file {}", self.path.to_string_lossy())
|
|
|
write!(f, "gitignore file {}", self.path.to_string_lossy())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|