Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff parser bugfixes #577

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions plugins/git/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,18 @@ mod test {
let (leftover, _parsed) = crate::parse::patch(input).unwrap();
assert!(leftover.is_empty());
}

#[test]
fn test_hyphens_in_diff_stats() {
let input = "0\t4\tsite/content/_index.md\n136\t2\tsite/content/install/_index.md\n-\t-\tsite/static/images/homepage-bg.png\n2\t2\tsite/tailwind.config.js\n2\t0\tsite/templates/bases/base.tera.html\n82\t1\tsite/templates/index.html\n3\t3\tsite/templates/shortcodes/info.html\n15\t14\txtask/src/task/site/serve.rs\n";
let (leftover, _) = crate::parse::stats(input).unwrap();
assert!(leftover.is_empty());
}

#[test]
fn test_patch_with_only_meta() {
let input = "diff --git a/hipcheck/src/analysis/session/spdx.rs b/hipcheck/src/session/spdx.rs\nsimilarity index 100%\nrename from hipcheck/src/analysis/session/spdx.rs\nrename to hipcheck/src/session/spdx.rs\n";
let (leftover, _) = crate::parse::patch(input).unwrap();
assert!(leftover.is_empty());
}
}
42 changes: 30 additions & 12 deletions plugins/git/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use jiff::Timestamp;
use nom::{
branch::alt,
character::complete::{char as character, digit1, not_line_ending, one_of, space1},
combinator::{opt, peek, recognize},
combinator::{map, opt, peek, recognize},
error::{Error as NomError, ErrorKind},
multi::{fold_many0, many0, many1, many_m_n},
sequence::{preceded, terminated, tuple},
Expand Down Expand Up @@ -149,25 +149,41 @@ fn num(input: &str) -> IResult<&str, i64> {
})
}

fn stat(input: &str) -> IResult<&str, Stat<'_>> {
tuple((num, space1, num, space1, line))(input).map(
fn num_or_dash(input: &str) -> IResult<&str, Option<i64>> {
let some_num = map(num, Some);
let dash = map(character('-'), |_| None);
alt((some_num, dash))(input)
}

fn stat(input: &str) -> IResult<&str, Option<Stat<'_>>> {
tuple((num_or_dash, space1, num_or_dash, space1, line))(input).map(
|(i, (lines_added, _, lines_deleted, _, file_name))| {
let Some(lines_added) = lines_added else {
return (i, None);
};

let Some(lines_deleted) = lines_deleted else {
return (i, None);
};

let stat = Stat {
lines_added,
lines_deleted,
file_name,
};

(i, stat)
(i, Some(stat))
},
)
}

fn stats(input: &str) -> IResult<&str, Vec<Stat<'_>>> {
many0(stat)(input)
pub(crate) fn stats(input: &str) -> IResult<&str, Vec<Stat<'_>>> {
map(many0(stat), |vec| {
vec.into_iter().flatten().collect::<Vec<_>>()
})(input)
}

fn diff(input: &str) -> IResult<&str, Diff> {
pub(crate) fn diff(input: &str) -> IResult<&str, Diff> {
log::trace!("input is {:#?}", input);
tuple((stats, line, patches))(input).map(|(i, (stats, _, patches))| {
log::trace!("patches are {:#?}", patches);
Expand Down Expand Up @@ -238,7 +254,7 @@ fn gh_diff(input: &str) -> IResult<&str, Diff> {
})
}

fn diffs(input: &str) -> IResult<&str, Vec<Diff>> {
pub(crate) fn diffs(input: &str) -> IResult<&str, Vec<Diff>> {
many0(diff)(input)
}

Expand All @@ -251,7 +267,7 @@ fn meta(input: &str) -> IResult<&str, &str> {
recognize(tuple((single_alpha, line)))(input)
}

fn metas(input: &str) -> IResult<&str, Vec<&str>> {
pub(crate) fn metas(input: &str) -> IResult<&str, Vec<&str>> {
many1(meta)(input)
}

Expand All @@ -261,8 +277,8 @@ fn single_alpha(input: &str) -> IResult<&str, &str> {
))(input)
}

fn patch_header(input: &str) -> IResult<&str, &str> {
recognize(tuple((metas, line, line)))(input)
pub(crate) fn patch_header(input: &str) -> IResult<&str, &str> {
recognize(tuple((metas, opt(line), opt(line))))(input)
}

fn chunk_prefix(input: &str) -> IResult<&str, &str> {
Expand Down Expand Up @@ -312,7 +328,8 @@ fn patch_footer(input: &str) -> IResult<&str, Option<&str>> {
}

pub(crate) fn patch(input: &str) -> IResult<&str, String> {
tuple((patch_header, chunks, patch_footer))(input).map(|(i, (_, chunks, _))| (i, chunks))
tuple((patch_header, opt(chunks), patch_footer))(input)
.map(|(i, (_, chunks, _))| (i, chunks.unwrap_or_else(String::new)))
}

fn gh_meta(input: &str) -> IResult<&str, &str> {
Expand Down Expand Up @@ -416,6 +433,7 @@ mod test {
let line = "7 0 Cargo.toml\n";

let (remaining, stat) = stat(line).unwrap();
let stat = stat.unwrap();

assert_eq!("", remaining);
assert_eq!(7, stat.lines_added);
Expand Down