-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(check): add
--strip
flag when checking from stdin
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
Showing
5 changed files
with
54 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ mod conventional; | |
mod error; | ||
mod git; | ||
mod semver; | ||
mod strip; | ||
|
||
use std::process::exit; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |