aboutsummaryrefslogtreecommitdiff
path: root/hooked
diff options
context:
space:
mode:
Diffstat (limited to 'hooked')
-rw-r--r--hooked/Cargo.toml1
-rw-r--r--hooked/src/bin/hooked-commit-msg.rs46
2 files changed, 46 insertions, 1 deletions
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);
+ }
}