aboutsummaryrefslogtreecommitdiff
path: root/hooked/src/bin/hooked-commit-msg.rs
blob: 7f2a9960850c412149e4654338508786122b3db5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use shared::find_root;
use std::{
  env::args,
  error::Error,
  fs,
  process,
};
use unicode_segmentation::UnicodeSegmentation;

fn main() {
  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);
  }
}