Skip to content

Commit 1bdbf5c

Browse files
authored
Merge pull request #2011 from Urgau/submodule_handler_improvements
Submodule handler improvements
2 parents 7604864 + 85e3956 commit 1bdbf5c

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed
Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,69 @@
1+
use std::sync::LazyLock;
2+
3+
use regex::{Regex, RegexBuilder};
4+
15
use crate::github::FileDiff;
26

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

9+
static SUBPROJECT_COMMIT_RE: LazyLock<Regex> = LazyLock::new(|| {
10+
RegexBuilder::new(r"^\+Subproject commit [a-zA-Z0-9]+$")
11+
.multi_line(true)
12+
.build()
13+
.unwrap()
14+
});
15+
516
/// Returns a message if the PR modifies a git submodule.
617
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)) {
18+
if diff
19+
.iter()
20+
.any(|fd| SUBPROJECT_COMMIT_RE.is_match(&fd.patch))
21+
{
922
Some(SUBMODULE_WARNING_MSG.to_string())
1023
} else {
1124
None
1225
}
1326
}
27+
28+
#[test]
29+
fn no_submodule_update() {
30+
let filediff = FileDiff {
31+
filename: "src/lib.rs".to_string(),
32+
patch: "@@ -1 +1 @@\
33+
-let mut my_var = 5;\
34+
+let mut my_var = \"tmp\";"
35+
.to_string(),
36+
};
37+
38+
assert_eq!(modifies_submodule(&[filediff]), None)
39+
}
40+
41+
#[test]
42+
fn simple_submodule_update() {
43+
// Taken from https://api.github.com/repos/rust-lang/rust/compare/5af801b687e6e8b860ae970e725c8b9a3820d0ce...d6c4ab81be200855df856468ddedde057958441a
44+
let filediff = FileDiff {
45+
filename: "src/tools/rustc-perf".to_string(),
46+
patch: "@@ -1 +1 @@\n\
47+
-Subproject commit c0f3b53c8e5de87714d18a5f42998859302ae03a\n\
48+
+Subproject commit 8158f78f738715c060d230351623a7f7cc01bf97"
49+
.to_string(),
50+
};
51+
52+
assert_eq!(
53+
modifies_submodule(&[filediff]),
54+
Some(SUBMODULE_WARNING_MSG.to_string())
55+
)
56+
}
57+
58+
#[test]
59+
fn no_submodule_update_tricky_case() {
60+
let filediff = FileDiff {
61+
filename: "src/tools.sh".to_string(),
62+
patch: "@@ -1 +1 @@\
63+
-let mut subproject_commit = 5;\
64+
+let mut subproject_commit = \"+Subproject commit \";"
65+
.to_string(),
66+
};
67+
68+
assert_eq!(modifies_submodule(&[filediff]), None)
69+
}

0 commit comments

Comments
 (0)