diff options
Diffstat (limited to '')
-rwxr-xr-x | .dev-suite/hooked/commit-msg | 5 | ||||
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | hooked/Cargo.toml | 1 | ||||
-rw-r--r-- | hooked/src/bin/hooked-commit-msg.rs | 46 |
4 files changed, 51 insertions, 2 deletions
diff --git a/.dev-suite/hooked/commit-msg b/.dev-suite/hooked/commit-msg index efcbaec..cc072bf 100755 --- a/.dev-suite/hooked/commit-msg +++ b/.dev-suite/hooked/commit-msg @@ -1 +1,4 @@ -#! /bin/bash
\ No newline at end of file +#! /bin/bash + +# Check that the commit message is up to spec +hooked-commit-msg $@ @@ -197,6 +197,7 @@ dependencies = [ "paw 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "shared 0.1.0", "structopt 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/hooked/Cargo.toml b/hooked/Cargo.toml index acde074..d0781e0 100644 --- a/hooked/Cargo.toml +++ b/hooked/Cargo.toml @@ -11,3 +11,4 @@ anyhow = "1.0" paw = "1.0" shared = { path = "../shared" } structopt = { version = "0.3", features = ["paw"] } +unicode-segmentation = "1.3" diff --git a/hooked/src/bin/hooked-commit-msg.rs b/hooked/src/bin/hooked-commit-msg.rs index 80a1832..7f2a996 100644 --- a/hooked/src/bin/hooked-commit-msg.rs +++ b/hooked/src/bin/hooked-commit-msg.rs @@ -1,3 +1,47 @@ +use shared::find_root; +use std::{ + env::args, + error::Error, + fs, + process, +}; +use unicode_segmentation::UnicodeSegmentation; + fn main() { - println!("Hello, world!"); + if let Err(e) = || -> Result<(), Box<dyn Error>> { + let path = find_root()?.join(args().last().ok_or_else(|| { + "Expected to be passed a path to the git commit message" + })?); + + let file = fs::read_to_string(path)?; + let mut lines = file.lines(); + + if let Some(ref first_line) = lines.next() { + let length = first_line.graphemes(true).count(); + if length > 50 { + return Err( + "Your commit header is over 50 characters (i.e. graphemes) in length.\n\ + Commit messages titles should be between 10 to 50 characters".into()); + } + if length < 10 { + return Err( + "Your commit header is less than 10 characters (i.e. graphemes) in length.\n\ + Commit messages titles should be between 10 to 50 characters".into()); + } + } + + for line in lines { + let length = line.graphemes(true).count(); + if length > 72 { + return Err( + "One of the lines in the body of the commit is over 72 characters (i.e. graphemes) in \n\ + length. Commit messages titles should be between 10 to 50 characters".into()); + } + } + + Ok(()) + }() { + eprintln!("{}", e); + process::exit(1); + } } |