3 Commits

  1. 805
      Cargo.lock
  2. 2
      Cargo.toml
  3. 9
      src/app.rs
  4. 22
      src/gitignore.rs
  5. 1
      src/main.rs

805
Cargo.lock
File diff suppressed because it is too large
View File

2
Cargo.toml

@ -1,6 +1,6 @@
[package]
name = "gitig"
version = "0.3.0"
version = "0.3.1"
authors = ["Marcel Schneider <gitig@webschneider.org>"]
description = "A cli utility to manage gitignore files easily"
edition = "2018"

9
src/app.rs

@ -65,7 +65,7 @@ pub enum Command {
/// Add a line to the gitignore
Add {
/// The glob string that should be added
glob: Vec<String>,
glob: Vec<String>,
/// Add the entry to the repo local ignore file
#[structopt(short)]
local: bool,
@ -78,7 +78,7 @@ pub enum Command {
/// The language for which the gitignore should be downloaded
///
/// A list with all available languages and projects can be printed with `list-templates`.
lang: String,
lang: String,
},
/// List all available templates that can be downloaded
ListTemplates,
@ -107,7 +107,10 @@ fn add(glob: &str, local: bool) -> Result<()> {
info!("Working with git root in {:?}", root);
let mut file_path = PathBuf::from(&root);
if local {
let override_path = std::env::var("GITIG_OVERRIDE_PATH").ok();
if let Some(override_path) = override_path {
file_path = PathBuf::from(override_path);
} else if local {
file_path.push(".git/info/exclude")
} else {
file_path.push(".gitignore");

22
src/gitignore.rs

@ -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(())
}

1
src/main.rs

@ -29,6 +29,7 @@ This project used [rust-cli-boilerplate](https://github.com/ssokolow/rust-cli-bo
clippy::wildcard_imports,
clippy::integer_arithmetic
)]
#![feature(seek_stream_len)]
#[macro_use]
extern crate error_chain;

Loading…
Cancel
Save