aboutsummaryrefslogtreecommitdiff
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
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.
-rwxr-xr-x.dev-suite/hooked/commit-msg5
-rw-r--r--Cargo.lock1
-rw-r--r--hooked/Cargo.toml1
-rw-r--r--hooked/src/bin/hooked-commit-msg.rs46
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 $@
diff --git a/Cargo.lock b/Cargo.lock
index 0e50a59..28dce0a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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);
+ }
}