Skip to content

Commit 5b6f968

Browse files
authored
Merge pull request #1931 from snprajwal/optimise-no-mentions
refactor: reduce allocations in `no_mentions`
2 parents 28c61ba + e839883 commit 5b6f968

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

src/handlers/check_commits/no_mentions.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
11
//! Purpose: When opening a PR, or pushing new changes, check for github mentions
22
//! in commits and notify the user of our no-mentions in commits policy.
33
4-
use std::fmt::Write;
5-
64
use crate::{config::NoMentionsConfig, github::GithubCommit};
75

86
pub(super) fn mentions_in_commits(
97
_conf: &NoMentionsConfig,
108
commits: &[GithubCommit],
119
) -> Option<String> {
12-
let mut mentions_commits = Vec::new();
13-
14-
for commit in commits {
15-
if !parser::get_mentions(&commit.commit.message).is_empty() {
16-
mentions_commits.push(&*commit.sha);
17-
}
18-
}
10+
let mentions_commits = commits
11+
.into_iter()
12+
.filter(|c| !parser::get_mentions(&c.commit.message).is_empty())
13+
.map(|c| format!(" - {}\n", c.sha))
14+
.collect::<String>();
1915

2016
if mentions_commits.is_empty() {
2117
None
2218
} else {
23-
Some(mentions_in_commits_warn(mentions_commits))
24-
}
25-
}
26-
27-
fn mentions_in_commits_warn(commits: Vec<&str>) -> String {
28-
let mut warning = format!("There are username mentions (such as `@user`) in the commit messages of the following commits.\n *Please remove the mentions to avoid spamming these users.*\n");
29-
30-
for commit in commits {
31-
let _ = writeln!(warning, " - {commit}");
19+
Some(format!(
20+
r"There are username mentions (such as `@user`) in the commit messages of the following commits.
21+
*Please remove the mentions to avoid spamming these users.*
22+
{mentions_commits}",
23+
))
3224
}
33-
34-
warning
3525
}
3626

3727
#[test]
@@ -78,10 +68,10 @@ Co-authored-by: Baz Qux <[email protected]>",
7868
assert_eq!(
7969
mentions_in_commits(&NoMentionsConfig {}, &commits),
8070
Some(
81-
r#"There are username mentions (such as `@user`) in the commit messages of the following commits.
71+
r"There are username mentions (such as `@user`) in the commit messages of the following commits.
8272
*Please remove the mentions to avoid spamming these users.*
8373
- d7daa17bc97df9377640b0d33cbd0bbeed703c3a
84-
"#.to_string()
74+
".to_string()
8575
)
8676
);
8777
}

0 commit comments

Comments
 (0)