Skip to content

Commit 98ad725

Browse files
committed
Lazily create the modified submodule regex
1 parent 44c66c9 commit 98ad725

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/handlers/check_commits/modified_submodule.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
use std::sync::LazyLock;
2+
3+
use regex::Regex;
4+
15
use crate::github::FileDiff;
26

37
const SUBMODULE_WARNING_MSG: &str = "Some commits in this PR modify **submodules**.";
48

9+
static SUBPROJECT_UPDATE_RE: LazyLock<Regex> =
10+
LazyLock::new(|| Regex::new(r"\+Subproject\scommit\s").unwrap());
11+
512
/// Returns a message if the PR modifies a git submodule.
613
pub(super) fn modifies_submodule(diff: &[FileDiff]) -> Option<String> {
7-
let re = regex::Regex::new(r"\+Subproject\scommit\s").unwrap();
8-
if diff.iter().any(|fd| re.is_match(&fd.patch)) {
14+
if diff
15+
.iter()
16+
.any(|fd| SUBPROJECT_UPDATE_RE.is_match(&fd.patch))
17+
{
918
Some(SUBMODULE_WARNING_MSG.to_string())
1019
} else {
1120
None
@@ -15,8 +24,8 @@ pub(super) fn modifies_submodule(diff: &[FileDiff]) -> Option<String> {
1524
#[test]
1625
fn no_submodule_update() {
1726
let filediff = FileDiff {
18-
path: "src/lib.rs".to_string(),
19-
diff: "@@ -1 +1 @@\
27+
filename: "src/lib.rs".to_string(),
28+
patch: "@@ -1 +1 @@\
2029
-let mut my_var = 5;\
2130
+let mut my_var = \"tmp\";"
2231
.to_string(),
@@ -29,8 +38,8 @@ fn no_submodule_update() {
2938
fn simple_submodule_update() {
3039
// Taken from https://api.github.com/repos/rust-lang/rust/compare/5af801b687e6e8b860ae970e725c8b9a3820d0ce...d6c4ab81be200855df856468ddedde057958441a
3140
let filediff = FileDiff {
32-
path: "src/tools/rustc-perf".to_string(),
33-
diff: "@@ -1 +1 @@\n\
41+
filename: "src/tools/rustc-perf".to_string(),
42+
patch: "@@ -1 +1 @@\n\
3443
-Subproject commit c0f3b53c8e5de87714d18a5f42998859302ae03a\n\
3544
+Subproject commit 8158f78f738715c060d230351623a7f7cc01bf97"
3645
.to_string(),
@@ -45,8 +54,8 @@ fn simple_submodule_update() {
4554
#[test]
4655
fn no_submodule_update_tricky_case() {
4756
let filediff = FileDiff {
48-
path: "src/tools.sh".to_string(),
49-
diff: "@@ -1 +1 @@\
57+
filename: "src/tools.sh".to_string(),
58+
patch: "@@ -1 +1 @@\
5059
-let mut subproject_commit = 5;\
5160
+let mut subproject_commit = \"+Subproject commit \";"
5261
.to_string(),

0 commit comments

Comments
 (0)