From 46c0fd4dc5c9433568d025172c69980470e19586 Mon Sep 17 00:00:00 2001 From: Scott Johnson Date: Sat, 26 Jun 2021 16:26:12 -0500 Subject: [PATCH 1/2] Fixed issue where tags at the beginning of summary not parsed correctly. If tags appeared before the category when parsing, the tags would not be added to the list of tags in the commit message. This was due, in part, to a regular expression that did not consider the possibility that the tag could be at the beginning of a line, and partially due to the fact that tags were only parsed from the remainder of the line AFTER categories had been removed. Fixes #928. --- .gitmodules | 3 +++ src/lib.rs | 14 ++++++++++++++ src/parser.rs | 19 +++++++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index c621c1db..0e2903a7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "tests/test_repo2"] path = tests/test_repo2 url = https://github.com/saschagrunert/test2 +[submodule "tests/test_repo3"] + path = tests/test_repo3 + url = git@github.com:jwir3/ghp-release-cleaner diff --git a/src/lib.rs b/src/lib.rs index f4f80b89..4c590840 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1010,6 +1010,20 @@ mod tests { assert!(journal.generate_template().is_ok()); } + #[test] + fn output_correctly_categorized() { + let mut journal = GitJournal::new("./tests/test_repo3").unwrap(); + assert!(journal + .parse_log("444b3a64355557844beb848d80051ada2c00176a..HEAD", "rc", 0, true, false, None, None) + .is_ok()); + assert_eq!(journal.parser.result.len(), 4); + assert_eq!(journal.parser.result[0].name, "Unreleased"); + assert_eq!(journal.parser.result[0].commits.len(), 3); + assert_eq!(journal.parser.result[0].commits[0].summary.tags.len(), 2); + assert!(journal.print_log(false, None, Some("CHANGELOG.md")).is_ok()); + assert!(journal.print_log(true, None, Some("CHANGELOG.md")).is_ok()); + } + #[test] fn path_failure() { assert!(GitJournal::new("/etc/").is_err()); diff --git a/src/parser.rs b/src/parser.rs index 540292b3..6780eca8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -776,7 +776,7 @@ pub struct FooterElement { } lazy_static! { - static ref RE_TAGS: Regex = Regex::new(r"[ \n]:(.*?):").unwrap(); + static ref RE_TAGS: Regex = Regex::new(r"[ \n]*:(.*?):").unwrap(); static ref RE_FOOTER: Regex = RegexBuilder::new(r"^([\w-]+):\s(.*)$") .multi_line(true) .build() @@ -827,12 +827,25 @@ impl Parser { } fn parse_summary<'a>(&mut self, input: &'a [u8]) -> ParserResult<'a, SummaryElement> { + // Before we do anything, let's parse the entire summary line for tags so that we don't + // skip any tags that happen before, say, the category. + let entire_string = str_or_empty(input); + let mut tags = vec![]; + for cap in RE_TAGS.captures_iter(entire_string) { + tags.push((&cap[1]).to_string()); + } + let (input, p_prefix) = opt(separated_pair(alpha1, char('-'), digit1))(input)?; let (input, _) = space0(input)?; let (input, p_category) = self.parse_category(input)?; let (input, _) = space1(input)?; let (input, p_tags_rest) = map(rest, Self::parse_and_consume_tags)(input)?; + let p_tags_rest_vec = p_tags_rest.0; + tags.extend(p_tags_rest_vec); + tags.sort(); + tags.dedup(); + Ok(( input, SummaryElement { @@ -841,7 +854,7 @@ impl Parser { format!("{}-{}", str_or_empty(p.0), str_or_empty(p.1)) }), category: p_category.to_owned(), - tags: p_tags_rest.0, + tags: tags, text: p_tags_rest.1, }, )) @@ -850,6 +863,7 @@ impl Parser { fn parse_and_consume_tags(input: &[u8]) -> (Vec, String) { let string = str_or_empty(input); let mut tags = vec![]; + for cap in RE_TAGS.captures_iter(string) { if let Some(tag) = cap.get(1) { tags.extend( @@ -888,6 +902,7 @@ impl Parser { .next() .ok_or_else(|| format_err!("Summar line parsing: Commit message length too small."))? .trim(); + let mut parsed_summary = match self.clone().parse_summary(summary_line.as_bytes()) { Ok((_, parsed)) => parsed, _ => bail!("Summary parsing failed: '{}'", summary_line), From 53757f2c3da497cd308b0daa8de826f2971d2016 Mon Sep 17 00:00:00 2001 From: Scott Johnson Date: Sat, 26 Jun 2021 16:41:37 -0500 Subject: [PATCH 2/2] Fixed minor issue with tests where newly added tests were failing. --- src/lib.rs | 4 ++-- tests/test_repo3 | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) create mode 160000 tests/test_repo3 diff --git a/src/lib.rs b/src/lib.rs index 4c590840..997d1b46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1018,8 +1018,8 @@ mod tests { .is_ok()); assert_eq!(journal.parser.result.len(), 4); assert_eq!(journal.parser.result[0].name, "Unreleased"); - assert_eq!(journal.parser.result[0].commits.len(), 3); - assert_eq!(journal.parser.result[0].commits[0].summary.tags.len(), 2); + assert_eq!(journal.parser.result[0].commits.len(), 2); + assert_eq!(journal.parser.result[0].commits[0].summary.tags.len(), 1); assert!(journal.print_log(false, None, Some("CHANGELOG.md")).is_ok()); assert!(journal.print_log(true, None, Some("CHANGELOG.md")).is_ok()); } diff --git a/tests/test_repo3 b/tests/test_repo3 new file mode 160000 index 00000000..85aab38d --- /dev/null +++ b/tests/test_repo3 @@ -0,0 +1 @@ +Subproject commit 85aab38d7b2a80f02225672cec815448caafbc88