aboutsummaryrefslogtreecommitdiff
path: root/hooked
diff options
context:
space:
mode:
authorMichael Gattozzi <mgattozzi@gmail.com>2019-11-22 15:52:31 -0500
committerMichael Gattozzi <mgattozzi@gmail.com>2019-11-22 15:52:31 -0500
commit7f3d02a01bc5174cd9e2a4b1c44be64534cb1a37 (patch)
treeaaa99c265ea78293bb225bc8ce12ded124a59b4e /hooked
parent98e0a821f819fe371f8f642ece6500c40d374bcc (diff)
Add commit message linting hook to the repo
This adds a commit to handle git commit linting to enforce style by not allowing less than 10 or more than 50 chars for titles and less than or equal to 72 chars for the body. Chars are measured in number of graphemes as 50 chars represented in the terminal is what we want to use not 50 bytes. This will eventually be an installable hook for end users if they want it.
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);
+ }
}