Skip to content

Commit

Permalink
feat(check): add --strip flag when checking from stdin
Browse files Browse the repository at this point in the history
Removes leading and trailing newlines and comments and trailing whitespace from the commit message read from stdin. It also colapses multiple empty lines into one.

Refs: #114
  • Loading branch information
hdevalke committed Feb 6, 2023
1 parent 493b6e4 commit f46b4be
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ pub struct CheckCommand {
/// Read a single commit message from stdin
#[clap(long)]
pub from_stdin: bool,
/// String comments and whitespace from commit message
/// This is similar to `git commit --cleanup=strip`
#[clap(long, requires("from_stdin"))]
pub strip: bool,
}

#[derive(Debug, Parser)]
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
cmd::Command,
conventional::{self, Type},
git::{filter_merge_commits, filter_revert_commits},
strip::Strip,
Error,
};

Expand Down Expand Up @@ -67,6 +68,9 @@ impl Command for CheckCommand {
let mut stdin = stdin().lock();
let mut commit_msg = String::new();
stdin.read_to_string(&mut commit_msg)?;
if self.strip {
commit_msg = commit_msg.strip();
}
parser.parse(&commit_msg)?;
return Ok(());
}
Expand Down
8 changes: 2 additions & 6 deletions src/cmd/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde::Serialize;
use crate::{
cli::CommitCommand,
conventional::{config::Type, CommitParser, Config, ParseError},
strip::Strip,
Command, Error,
};

Expand Down Expand Up @@ -82,12 +83,7 @@ fn edit_message(msg: &str) -> Result<String, Error> {
.require_save(false)
.edit(msg)?
.unwrap_or_default()
.lines()
.filter(|line| !line.starts_with('#'))
.collect::<Vec<&str>>()
.join("\n")
.trim()
.to_owned())
.strip())
}

#[derive(Serialize)]
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod conventional;
mod error;
mod git;
mod semver;
mod strip;

use std::process::exit;

Expand Down
43 changes: 43 additions & 0 deletions src/strip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pub trait Strip {
fn strip(&self) -> String;
}

impl<T> Strip for T
where
T: AsRef<str>,
{
fn strip(&self) -> String {
fn strip(s: &str) -> String {
let iter = s
.lines()
.filter(|line| !line.starts_with('#'))
.map(|line| line.trim_end());
let mut lines: Vec<&str> = iter.collect();
lines.dedup_by(|a, b| a.trim().is_empty() && b.trim().is_empty());
lines.join("\n").trim().to_string()
}
strip(self.as_ref())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_name() {
let s = "#
1
2
3
4
# 5
";
assert_eq!("1\n2\n3\n\n4", s.strip());
}
}

0 comments on commit f46b4be

Please sign in to comment.