use mit_commit::CommitMessage;
use crate::model::{Code, Problem, ProblemBuilder};
pub const CONFIG: &str = "subject-longer-than-72-characters";
pub const ERROR: &str = "Your subject is longer than 72 characters";
pub const HELP_MESSAGE: &str = "It's important to keep the subject of the commit less than 72 \
characters because when you look at the git log, that's where it \
truncates the message. This means that people won't get the entirety \
of the information in your commit.\n\nPlease keep the subject line 72 \
characters or under";
const LIMIT: usize = 72;
pub struct SubjectLengthConfig {
pub character_limit: usize,
}
impl Default for SubjectLengthConfig {
fn default() -> Self {
Self {
character_limit: LIMIT,
}
}
}
pub fn lint(commit: &CommitMessage<'_>) -> Option<Problem> {
lint_with_config(commit, &SubjectLengthConfig::default())
}
fn lint_with_config(commit: &CommitMessage<'_>, config: &SubjectLengthConfig) -> Option<Problem> {
Some(commit)
.filter(|commit| has_problem(commit, config.character_limit))
.map(|commit| create_problem(commit, config.character_limit))
}
fn has_problem(commit: &CommitMessage<'_>, limit: usize) -> bool {
subject_length(commit) > limit
}
fn create_problem(commit: &CommitMessage, limit: usize) -> Problem {
let subject_length = subject_length(commit);
ProblemBuilder::new(
ERROR,
HELP_MESSAGE,
Code::SubjectLongerThan72Characters,
commit,
)
.with_label("Too long", limit, subject_length - limit)
.with_url("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines")
.build()
}
fn subject_length(commit: &CommitMessage<'_>) -> usize {
commit
.get_subject()
.chars()
.position(is_newline)
.unwrap_or_else(|| commit.get_subject().chars().count())
}
const fn is_newline(character: char) -> bool {
character == '\n'
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{Code, Problem};
use miette::{Diagnostic, GraphicalReportHandler, GraphicalTheme, Report};
use mit_commit::CommitMessage;
use quickcheck::TestResult;
#[test]
fn shorter_than_72_characters() {
test_subject_longer_than_72_characters(&"x".repeat(72), None);
}
#[test]
fn shorter_than_72_characters_with_a_new_line() {
test_subject_longer_than_72_characters(&format!("{}\n", "x".repeat(72)), None);
}
#[test]
fn shorter_than_72_characters_with_a_new_line_then_characters_directly_afterwards() {
test_subject_longer_than_72_characters(
&format!("{}\nsome more content", "x".repeat(72)),
None,
);
}
#[test]
fn shorter_than_72_characters_with_realistic_trailer_and_a_body() {
let message = "Remove duplicated function
# Short (50 chars or less) summary of changes
#
# More detailed explanatory text, if necessary. Wrap it to
# about 72 characters or so. In some contexts, the first
# line is treated as the subject of an email and the rest of
# the text as the body. The blank line separating the
# summary from the body is critical (unless you omit the body
# entirely); tools like rebase can get confused if you run
# the two together.
#
# Further paragraphs come after blank lines.
#
# - Bullet points are okay, too
#
# - Typically a hyphen or asterisk is used for the bullet,
# preceded by a single space, with blank lines in
# between, but conventions vary here
# Bitte geben Sie eine Commit-Beschreibung f\u{00FC}r Ihre \u{00C4}nderungen ein. \
Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch character-limit
# Zum Commit vorgemerkte \u{00C4}nderungen:
# ge\u{00E4}ndert: \
mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
#
# ------------------------ >8 ------------------------
# \u{00C4}ndern oder entfernen Sie nicht die obige Zeile.
# Alles unterhalb von ihr wird ignoriert.
diff --git a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs \
b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
index 5a83784..ebaee48 100644
--- a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
+++ b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
-fn has_missing_pivotal_tracker_id(commit_message: &CommitMessage) -> bool {
- has_no_pivotal_tracker_id(commit_message)
-}
-
fn has_no_pivotal_tracker_id(text: &CommitMessage) -> bool {
let re = Regex::new(REGEX_PIVOTAL_TRACKER_ID).unwrap();
!text.matches_pattern(&re)
}
pub(crate) fn lint(commit_message: &CommitMessage) -> Option<Problem> {
- if has_missing_pivotal_tracker_id(commit_message) {
+ if has_no_pivotal_tracker_id(commit_message) {
Some(Problem::new(
PIVOTAL_TRACKER_HELP.into(),
Code::PivotalTrackerIdMissing,
";
test_subject_longer_than_72_characters(&format!("{}\n\n{message}", "x".repeat(72)), None);
}
#[test]
fn shorter_than_72_characters_with_realistic_trailer() {
let message = "# Short (50 chars or less) summary of changes
#
# More detailed explanatory text, if necessary. Wrap it to
# about 72 characters or so. In some contexts, the first
# line is treated as the subject of an email and the rest of
# the text as the body. The blank line separating the
# summary from the body is critical (unless you omit the body
# entirely); tools like rebase can get confused if you run
# the two together.
#
# Further paragraphs come after blank lines.
#
# - Bullet points are okay, too
#
# - Typically a hyphen or asterisk is used for the bullet,
# preceded by a single space, with blank lines in
# between, but conventions vary here
# Bitte geben Sie eine Commit-Beschreibung f\u{00FC}r Ihre \u{00C4}nderungen ein. \
Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch character-limit
# Zum Commit vorgemerkte \u{00C4}nderungen:
# ge\u{00E4}ndert: \
mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
#
# ------------------------ >8 ------------------------
# \u{00C4}ndern oder entfernen Sie nicht die obige Zeile.
# Alles unterhalb von ihr wird ignoriert.
diff --git a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs \
b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
index 5a83784..ebaee48 100644
--- a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
+++ b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
-fn has_missing_pivotal_tracker_id(commit_message: &CommitMessage) -> bool {
- has_no_pivotal_tracker_id(commit_message)
-}
-
fn has_no_pivotal_tracker_id(text: &CommitMessage) -> bool {
let re = Regex::new(REGEX_PIVOTAL_TRACKER_ID).unwrap();
!text.matches_pattern(&re)
}
pub(crate) fn lint(commit_message: &CommitMessage) -> Option<Problem> {
- if has_missing_pivotal_tracker_id(commit_message) {
+ if has_no_pivotal_tracker_id(commit_message) {
Some(Problem::new(
PIVOTAL_TRACKER_HELP.into(),
Code::PivotalTrackerIdMissing,
";
test_subject_longer_than_72_characters(&format!("{}\n\n{message}", "x".repeat(72)), None);
}
#[test]
fn longer_than_72_characters() {
let message = "x".repeat(73);
test_subject_longer_than_72_characters(
&message.clone(),
Some(Problem::new(
ERROR.into(),
HELP_MESSAGE.into(),
Code::SubjectLongerThan72Characters,
&message.into(),
Some(vec![("Too long".to_string(), 72_usize, 1_usize)]),
Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap()),
)).as_ref(),
);
}
#[test]
fn longer_than_72_characters_and_a_newline() {
let message = format!("{}\n", "x".repeat(73));
test_subject_longer_than_72_characters(
&message.clone(),
Some(Problem::new(
ERROR.into(),
HELP_MESSAGE.into(),
Code::SubjectLongerThan72Characters,
&message.into(),
Some(vec![("Too long".to_string(), 72_usize, 1_usize)]),
Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap()),
)).as_ref(),
);
}
#[test]
fn longer_than_72_characters_and_a_trailer_right_next_to_the_subject() {
let message = format!("{}\nSome-Trailer: This is a trailer", "x".repeat(73));
test_subject_longer_than_72_characters(
&message.clone(),
Some(Problem::new(
ERROR.into(),
HELP_MESSAGE.into(),
Code::SubjectLongerThan72Characters,
&message.into(),
Some(vec![("Too long".to_string(), 72_usize, 1_usize)]),
Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap()),
)).as_ref(),
);
}
#[test]
fn longer_than_72_characters_and_a_body() {
let message = "Remove duplicated function
# Short (50 chars or less) summary of changes
#
# More detailed explanatory text, if necessary. Wrap it to
# about 72 characters or so. In some contexts, the first
# line is treated as the subject of an email and the rest of
# the text as the body. The blank line separating the
# summary from the body is critical (unless you omit the body
# entirely); tools like rebase can get confused if you run
# the two together.
#
# Further paragraphs come after blank lines.
#
# - Bullet points are okay, too
#
# - Typically a hyphen or asterisk is used for the bullet,
# preceded by a single space, with blank lines in
# between, but conventions vary here
# Bitte geben Sie eine Commit-Beschreibung f\u{00FC}r Ihre \u{00C4}nderungen ein. \
Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch character-limit
# Zum Commit vorgemerkte \u{00C4}nderungen:
# ge\u{00E4}ndert: \
mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
#
# ------------------------ >8 ------------------------
# \u{00C4}ndern oder entfernen Sie nicht die obige Zeile.
# Alles unterhalb von ihr wird ignoriert.
diff --git a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs \
b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
index 5a83784..ebaee48 100644
--- a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
+++ b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
-fn has_missing_pivotal_tracker_id(commit_message: &CommitMessage) -> bool {
- has_no_pivotal_tracker_id(commit_message)
-}
-
fn has_no_pivotal_tracker_id(text: &CommitMessage) -> bool {
let re = Regex::new(REGEX_PIVOTAL_TRACKER_ID).unwrap();
!text.matches_pattern(&re)
}
pub(crate) fn lint(commit_message: &CommitMessage) -> Option<Problem> {
- if has_missing_pivotal_tracker_id(commit_message) {
+ if has_no_pivotal_tracker_id(commit_message) {
Some(Problem::new(
PIVOTAL_TRACKER_HELP.into(),
Code::PivotalTrackerIdMissing,
";
let message = format!("{}\n\n{message}", "x".repeat(73));
test_subject_longer_than_72_characters(
&message.clone(),
Some(&Problem::new(
ERROR.into(),
HELP_MESSAGE.into(),
Code::SubjectLongerThan72Characters,
&message.into(),
Some(vec![("Too long".to_string(), 72_usize, 1_usize)]),
Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".to_string()),
)),
);
}
#[test]
fn longer_than_72_characters_with_realistic_tail() {
let message = "# Short (50 chars or less) summary of changes
#
# More detailed explanatory text, if necessary. Wrap it to
# about 72 characters or so. In some contexts, the first
# line is treated as the subject of an email and the rest of
# the text as the body. The blank line separating the
# summary from the body is critical (unless you omit the body
# entirely); tools like rebase can get confused if you run
# the two together.
#
# Further paragraphs come after blank lines.
#
# - Bullet points are okay, too
#
# - Typically a hyphen or asterisk is used for the bullet,
# preceded by a single space, with blank lines in
# between, but conventions vary here
# Bitte geben Sie eine Commit-Beschreibung f\u{00FC}r Ihre \u{00C4}nderungen ein. \
Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch character-limit
# Zum Commit vorgemerkte \u{00C4}nderungen:
# ge\u{00E4}ndert: \
mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
#
# ------------------------ >8 ------------------------
# \u{00C4}ndern oder entfernen Sie nicht die obige Zeile.
# Alles unterhalb von ihr wird ignoriert.
diff --git a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs \
b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
index 5a83784..ebaee48 100644
--- a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
+++ b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
-fn has_missing_pivotal_tracker_id(commit_message: &CommitMessage) -> bool {
- has_no_pivotal_tracker_id(commit_message)
-}
-
fn has_no_pivotal_tracker_id(text: &CommitMessage) -> bool {
let re = Regex::new(REGEX_PIVOTAL_TRACKER_ID).unwrap();
!text.matches_pattern(&re)
}
pub(crate) fn lint(commit_message: &CommitMessage) -> Option<Problem> {
- if has_missing_pivotal_tracker_id(commit_message) {
+ if has_no_pivotal_tracker_id(commit_message) {
Some(Problem::new(
PIVOTAL_TRACKER_HELP.into(),
Code::PivotalTrackerIdMissing,
";
test_subject_longer_than_72_characters(&format!("{}\n\n{message}", "x".repeat(72)), None);
}
#[test]
fn formatting() {
let message = "x".repeat(73);
let problem = lint(&CommitMessage::from(message.to_string()));
let actual = fmt_report(&Report::new(problem.unwrap()));
let expected = "SubjectLongerThan72Characters (https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines)
x Your subject is longer than 72 characters
,----
1 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
: |
: `-- Too long
`----
help: It's important to keep the subject of the commit less than 72
characters because when you look at the git log, that's where it
truncates the message. This means that people won't get the entirety
of the information in your commit.
Please keep the subject line 72 characters or under
"
.to_string();
assert_eq!(
actual, expected,
"Message {message:?} should have returned {expected:?}, found {actual:?}"
);
}
fn fmt_report(diag: &Report) -> String {
let mut out = String::new();
GraphicalReportHandler::new_themed(GraphicalTheme::none())
.with_width(80)
.with_links(false)
.render_report(&mut out, diag.as_ref())
.unwrap();
out
}
fn test_subject_longer_than_72_characters(message: &str, expected: Option<&Problem>) {
let actual = &lint(&CommitMessage::from(message));
assert_eq!(
actual.as_ref(),
expected,
"Message {message:?} should have returned {expected:?}, found {actual:?}"
);
}
#[allow(clippy::needless_pass_by_value)]
#[quickcheck]
fn failure_check(commit_message_body: String) -> TestResult {
if commit_message_body
.chars()
.take_while(|x| *x != '\n' && *x != '\r')
.count()
<= 72
{
return TestResult::discard();
}
if commit_message_body.starts_with('#') {
return TestResult::discard();
}
let message = CommitMessage::from(format!("{commit_message_body}\n# comment"));
let result = lint(&message);
TestResult::from_bool(result.is_some())
}
#[allow(clippy::needless_pass_by_value)]
#[quickcheck]
fn success_check(subject: String, commit_message_body: Option<String>) -> TestResult {
if subject.chars().count() > 72 {
return TestResult::discard();
}
let message = CommitMessage::from(format!(
"{}{}\n# comment",
subject,
commit_message_body
.map(|x| format!("\n\n{x}"))
.unwrap_or_default()
));
let result = lint(&message);
TestResult::from_bool(result.is_none())
}
#[test]
fn shorter_than_72_characters_a() {
test_subject_longer_than_72_characters(&"x".repeat(72), None);
}
#[test]
fn shorter_than_72_characters_with_a_new_line_a() {
test_subject_longer_than_72_characters(&format!("{}\n", "x".repeat(72)), None);
}
#[test]
fn shorter_than_72_characters_with_realistic_trailer_and_a_body_a() {
let message = "Remove duplicated function
# Short (50 chars or less) summary of changes
#
# More detailed explanatory text, if necessary. Wrap it to
# about 72 characters or so. In some contexts, the first
# line is treated as the subject of an email and the rest of
# the text as the body. The blank line separating the
# summary from the body is critical (unless you omit the body
# entirely); tools like rebase can get confused if you run
# the two together.
#
# Further paragraphs come after blank lines.
#
# - Bullet points are okay, too
#
# - Typically a hyphen or asterisk is used for the bullet,
# preceded by a single space, with blank lines in
# between, but conventions vary here
# Bitte geben Sie eine Commit-Beschreibung f\u{00FC}r Ihre \u{00C4}nderungen ein. \
Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch character-limit
# Zum Commit vorgemerkte \u{00C4}nderungen:
# ge\u{00E4}ndert: \
mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
#
# ------------------------ >8 ------------------------
# \u{00C4}ndern oder entfernen Sie nicht die obige Zeile.
# Alles unterhalb von ihr wird ignoriert.
diff --git a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs \
b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
index 5a83784..ebaee48 100644
--- a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
+++ b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
-fn has_missing_pivotal_tracker_id(commit_message: &CommitMessage) -> bool {
- has_no_pivotal_tracker_id(commit_message)
-}
-
fn has_no_pivotal_tracker_id(text: &CommitMessage) -> bool {
let re = Regex::new(REGEX_PIVOTAL_TRACKER_ID).unwrap();
!text.matches_pattern(&re)
}
pub(crate) fn lint(commit_message: &CommitMessage) -> Option<Problem> {
- if has_missing_pivotal_tracker_id(commit_message) {
+ if has_no_pivotal_tracker_id(commit_message) {
Some(Problem::new(
PIVOTAL_TRACKER_HELP.into(),
Code::PivotalTrackerIdMissing,
";
test_subject_longer_than_72_characters(&format!("{}\n\n{message}", "x".repeat(72)), None);
}
#[test]
fn a_shorter_than_72_characters_with_realistic_trailer() {
let message = "# Short (50 chars or less) summary of changes
#
# More detailed explanatory text, if necessary. Wrap it to
# about 72 characters or so. In some contexts, the first
# line is treated as the subject of an email and the rest of
# the text as the body. The blank line separating the
# summary from the body is critical (unless you omit the body
# entirely); tools like rebase can get confused if you run
# the two together.
#
# Further paragraphs come after blank lines.
#
# - Bullet points are okay, too
#
# - Typically a hyphen or asterisk is used for the bullet,
# preceded by a single space, with blank lines in
# between, but conventions vary here
# Bitte geben Sie eine Commit-Beschreibung f\u{00FC}r Ihre \u{00C4}nderungen ein. \
Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch character-limit
# Zum Commit vorgemerkte \u{00C4}nderungen:
# ge\u{00E4}ndert: \
mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
#
# ------------------------ >8 ------------------------
# \u{00C4}ndern oder entfernen Sie nicht die obige Zeile.
# Alles unterhalb von ihr wird ignoriert.
diff --git a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs \
b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
index 5a83784..ebaee48 100644
--- a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
+++ b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
-fn has_missing_pivotal_tracker_id(commit_message: &CommitMessage) -> bool {
- has_no_pivotal_tracker_id(commit_message)
-}
-
fn has_no_pivotal_tracker_id(text: &CommitMessage) -> bool {
let re = Regex::new(REGEX_PIVOTAL_TRACKER_ID).unwrap();
!text.matches_pattern(&re)
}
pub(crate) fn lint(commit_message: &CommitMessage) -> Option<Problem> {
- if has_missing_pivotal_tracker_id(commit_message) {
+ if has_no_pivotal_tracker_id(commit_message) {
Some(Problem::new(
PIVOTAL_TRACKER_HELP.into(),
Code::PivotalTrackerIdMissing,
";
test_subject_longer_than_72_characters(&format!("{}\n\n{message}", "x".repeat(72)), None);
}
#[test]
fn a_longer_than_72_characters() {
let message = "x".repeat(73);
test_subject_longer_than_72_characters(
&message.clone(),
Some(Problem::new(
ERROR.into(),
HELP_MESSAGE.into(),
Code::SubjectLongerThan72Characters,
&message.into(),
Some(vec![("Too long".to_string(), 72_usize, 1_usize)]),
Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap()),
)).as_ref(),
);
}
#[test]
fn longer_than_72_characters_and_a_newline_a() {
let message = format!("{}\n", "x".repeat(73));
test_subject_longer_than_72_characters(
&message.clone(),
Some(Problem::new(
ERROR.into(),
HELP_MESSAGE.into(),
Code::SubjectLongerThan72Characters,
&message.into(),
Some(vec![("Too long".to_string(), 72_usize, 1_usize)]),
Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap()),
)).as_ref(),
);
}
#[test]
fn a_longer_than_72_characters_and_a_body() {
let message = "Remove duplicated function
# Short (50 chars or less) summary of changes
#
# More detailed explanatory text, if necessary. Wrap it to
# about 72 characters or so. In some contexts, the first
# line is treated as the subject of an email and the rest of
# the text as the body. The blank line separating the
# summary from the body is critical (unless you omit the body
# entirely); tools like rebase can get confused if you run
# the two together.
#
# Further paragraphs come after blank lines.
#
# - Bullet points are okay, too
#
# - Typically a hyphen or asterisk is used for the bullet,
# preceded by a single space, with blank lines in
# between, but conventions vary here
# Bitte geben Sie eine Commit-Beschreibung f\u{00FC}r Ihre \u{00C4}nderungen ein. \
Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch character-limit
# Zum Commit vorgemerkte \u{00C4}nderungen:
# ge\u{00E4}ndert: \
mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
#
# ------------------------ >8 ------------------------
# \u{00C4}ndern oder entfernen Sie nicht die obige Zeile.
# Alles unterhalb von ihr wird ignoriert.
diff --git a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs \
b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
index 5a83784..ebaee48 100644
--- a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
+++ b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
-fn has_missing_pivotal_tracker_id(commit_message: &CommitMessage) -> bool {
- has_no_pivotal_tracker_id(commit_message)
-}
-
fn has_no_pivotal_tracker_id(text: &CommitMessage) -> bool {
let re = Regex::new(REGEX_PIVOTAL_TRACKER_ID).unwrap();
!text.matches_pattern(&re)
}
pub(crate) fn lint(commit_message: &CommitMessage) -> Option<Problem> {
- if has_missing_pivotal_tracker_id(commit_message) {
+ if has_no_pivotal_tracker_id(commit_message) {
Some(Problem::new(
PIVOTAL_TRACKER_HELP.into(),
Code::PivotalTrackerIdMissing,
";
let message = format!("{}\n\n{message}", "x".repeat(73));
test_subject_longer_than_72_characters(
&message.clone(),
Some(Problem::new(
ERROR.into(),
HELP_MESSAGE.into(),
Code::SubjectLongerThan72Characters,
&message.into(),
Some(vec![("Too long".to_string(), 72_usize, 1_usize)]),
Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".to_string()),
)).as_ref(),
);
}
#[test]
fn longer_than_72_characters_with_realistic_tail_a() {
let message = "# Short (50 chars or less) summary of changes
#
# More detailed explanatory text, if necessary. Wrap it to
# about 72 characters or so. In some contexts, the first
# line is treated as the subject of an email and the rest of
# the text as the body. The blank line separating the
# summary from the body is critical (unless you omit the body
# entirely); tools like rebase can get confused if you run
# the two together.
#
# Further paragraphs come after blank lines.
#
# - Bullet points are okay, too
#
# - Typically a hyphen or asterisk is used for the bullet,
# preceded by a single space, with blank lines in
# between, but conventions vary here
# Bitte geben Sie eine Commit-Beschreibung f\u{00FC}r Ihre \u{00C4}nderungen ein. \
Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch character-limit
# Zum Commit vorgemerkte \u{00C4}nderungen:
# ge\u{00E4}ndert: \
mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
#
# ------------------------ >8 ------------------------
# \u{00C4}ndern oder entfernen Sie nicht die obige Zeile.
# Alles unterhalb von ihr wird ignoriert.
diff --git a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs \
b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
index 5a83784..ebaee48 100644
--- a/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
+++ b/mit-commit-message-lints/src/lints/model/missing_pivotal_tracker_id.rs
-fn has_missing_pivotal_tracker_id(commit_message: &CommitMessage) -> bool {
- has_no_pivotal_tracker_id(commit_message)
-}
-
fn has_no_pivotal_tracker_id(text: &CommitMessage) -> bool {
let re = Regex::new(REGEX_PIVOTAL_TRACKER_ID).unwrap();
!text.matches_pattern(&re)
}
pub(crate) fn lint(commit_message: &CommitMessage) -> Option<Problem> {
- if has_missing_pivotal_tracker_id(commit_message) {
+ if has_no_pivotal_tracker_id(commit_message) {
Some(Problem::new(
PIVOTAL_TRACKER_HELP.into(),
Code::PivotalTrackerIdMissing,
";
test_subject_longer_than_72_characters(&format!("{}\n\n{message}", "x".repeat(72)), None);
}
#[test]
fn test_create_problem_length_calculation() {
let subject_length = 80;
let subject = "x".repeat(subject_length);
let commit = CommitMessage::from(subject);
let problem = create_problem(&commit, LIMIT);
let labels = problem.labels().unwrap().collect::<Vec<_>>();
assert_eq!(labels.len(), 1, "There should be exactly one label");
let expected_length = subject_length - LIMIT;
assert_eq!(
labels[0].len(),
expected_length,
"Length calculation is incorrect"
);
let incorrect_length = if LIMIT != 0 {
subject_length / LIMIT
} else {
0
};
assert_ne!(
labels[0].len(),
incorrect_length,
"Length calculation appears to be using division instead of subtraction"
);
}
#[test]
fn formatting_a() {
let message = "x".repeat(73);
let problem = lint(&CommitMessage::from(message.to_string()));
let actual = fmt_report(&Report::new(problem.unwrap()));
let expected = "SubjectLongerThan72Characters (https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines)
x Your subject is longer than 72 characters
,----
1 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
: |
: `-- Too long
`----
help: It's important to keep the subject of the commit less than 72
characters because when you look at the git log, that's where it
truncates the message. This means that people won't get the entirety
of the information in your commit.
Please keep the subject line 72 characters or under
"
.to_string();
assert_eq!(
actual, expected,
"Message {message:?} should have returned {expected:?}, found {actual:?}"
);
}
fn _afmt_report(diag: &Report) -> String {
let mut out = String::new();
GraphicalReportHandler::new_themed(GraphicalTheme::none())
.with_width(80)
.with_links(false)
.render_report(&mut out, diag.as_ref())
.unwrap();
out
}
#[allow(clippy::needless_pass_by_value)]
#[quickcheck]
fn failure_check_a(commit_message_body: String) -> TestResult {
if commit_message_body
.chars()
.take_while(|x| *x != '\n' && *x != '\r')
.count()
<= 72
{
return TestResult::discard();
}
if commit_message_body.starts_with('#') {
return TestResult::discard();
}
let message = CommitMessage::from(format!("{commit_message_body}\n# comment"));
let result = lint(&message);
TestResult::from_bool(result.is_some())
}
#[allow(clippy::needless_pass_by_value)]
#[quickcheck]
fn success_check_a(subject: String, commit_message_body: Option<String>) -> TestResult {
if subject.chars().count() > 72 {
return TestResult::discard();
}
let message = CommitMessage::from(format!(
"{}{}\n# comment",
subject,
commit_message_body
.map(|x| format!("\n\n{x}"))
.unwrap_or_default()
));
let result = lint(&message);
TestResult::from_bool(result.is_none())
}
}