Skip to content

Commit 291d78e

Browse files
committed
Only look at the changed lines
1 parent 2ce3943 commit 291d78e

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

src/handlers/mentions.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub(super) async fn parse_input(
8080
// Only mentions byte-for-byte matching content inside the patch.
8181
files
8282
.iter()
83-
.filter(|f| f.patch.contains(&**entry))
83+
.filter(|f| patch_contains(&f.patch, &**entry))
8484
.map(|f| PathBuf::from(&f.filename))
8585
.collect()
8686
}
@@ -156,3 +156,64 @@ pub(super) async fn handle_input(
156156
}
157157
Ok(())
158158
}
159+
160+
fn patch_contains(patch: &str, needle: &str) -> bool {
161+
for line in patch.lines() {
162+
if (!line.starts_with("+++") && line.starts_with('+'))
163+
|| (!line.starts_with("---") && line.starts_with('-'))
164+
{
165+
if line.contains(needle) {
166+
return true;
167+
}
168+
}
169+
}
170+
171+
false
172+
}
173+
174+
#[cfg(test)]
175+
mod tests {
176+
use super::*;
177+
178+
#[test]
179+
fn finds_added_line() {
180+
let patch = "\
181+
--- a/file.txt
182+
+++ b/file.txt
183+
+hello world
184+
context line
185+
";
186+
assert!(patch_contains(patch, "hello"));
187+
}
188+
189+
#[test]
190+
fn finds_removed_line() {
191+
let patch = "\
192+
--- a/file.txt
193+
+++ b/file.txt
194+
-old value
195+
+new value
196+
";
197+
assert!(patch_contains(patch, "old value"));
198+
}
199+
200+
#[test]
201+
fn ignores_diff_headers() {
202+
let patch = "\
203+
--- a/file.txt
204+
+++ b/file.txt
205+
context line
206+
";
207+
assert!(!patch_contains(patch, "file.txt")); // should *not* match header
208+
}
209+
210+
#[test]
211+
fn needle_not_present() {
212+
let patch = "\
213+
--- a/file.txt
214+
+++ b/file.txt
215+
+added line
216+
";
217+
assert!(!patch_contains(patch, "missing"));
218+
}
219+
}

0 commit comments

Comments
 (0)