Skip to content

Commit

Permalink
Fixed issue where tags at the beginning of summary not parsed correctly.
Browse files Browse the repository at this point in the history
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 saschagrunert#928.
  • Loading branch information
jwir3 committed Jun 26, 2021
1 parent 63a4ce4 commit 46c0fd4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
19 changes: 17 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
},
))
Expand All @@ -850,6 +863,7 @@ impl Parser {
fn parse_and_consume_tags(input: &[u8]) -> (Vec<String>, 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(
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 46c0fd4

Please sign in to comment.