From 4ab18d02f7cbfbc62e1f5fdbbc6722b0088e7462 Mon Sep 17 00:00:00 2001 From: Max Hacking Date: Wed, 29 Apr 2015 00:01:22 +0100 Subject: [PATCH] chg: dev: Refactored code as per suggestions from Vaab. split_regex is now named split_event_regex and defaults to None leaving default behaviour as upstream. --- gitchangelog.py | 53 ++++++++++++++++++--------------------- gitchangelog.rc.reference | 5 ++-- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/gitchangelog.py b/gitchangelog.py index 5950474..fce6f2f 100755 --- a/gitchangelog.py +++ b/gitchangelog.py @@ -814,7 +814,7 @@ def makotemplate(template_name): ## def changelog(repository, - split_regex=r'(?s)(.+?)(?=[Cc]hg|[Ff]ix|[Nn]ew|$)', + split_event_regex=None, ignore_regexps=[], section_regexps=[(None,'')], unreleased_version_label="unreleased", @@ -883,42 +883,37 @@ def changelog(repository, ## Loop through all the commits for commit in commits: - ## Split the combined subject and body if using a split_regex otherwise just + ## Split the subject if using a split_event_regex otherwise just ## use the subject - if (len(strip(split_regex)) > 0): - events = re.split(split_regex, commit.subject + '\n\n' + commit.body) + if split_event_regex: + events = re.split(split_event_regex, commit.subject) else: - events = commit.subject + events = [commit.subject] ## Loop through the resulting event list for event in events: ## Check that the event is not just an empty string (presumably the split ## regex is not perfect) - if (len(strip(event)) > 0): + event = strip(event) + if not event: + continue - ## If this event matches an ignore regex then skip it - if any(re.search(pattern, event) is not None - for pattern in ignore_regexps): - continue - - ## Find the first matching section (not sure why this is certain to work - ## or what happens if it fails) - matched_section = first_matching(section_regexps, event) - - ## Finally store the commit in the matching section - if (len(strip(split_regex)) > 0): - sections[matched_section].append({ - "author": commit.author_name, - "subject": subject_process(event), - "body": '', - }) - else: - sections[matched_section].append({ - "author": commit.author_name, - "subject": subject_process(event), - "body": body_process(commit.body), - }) + ## If this event matches an ignore regex then skip it + if any(re.search(pattern, event) is not None + for pattern in ignore_regexps): + continue + + ## Find the first matching section (not sure why this is certain to work + ## or what happens if it fails) + matched_section = first_matching(section_regexps, event) + + ## Finally store the commit in the matching section + sections[matched_section].append({ + "author": commit.author_name, + "subject": subject_process(event), + "body": '' if split_event_regex else body_process(commit.body), + }) ## Flush current version current_version["sections"] = [{"label": k, "commits": sections[k]} @@ -1050,7 +1045,7 @@ def main(): content = changelog( repository, - split_regex=config['split_regex'], + split_event_regex=config['split_event_regex'], ignore_regexps=config['ignore_regexps'], section_regexps=config['section_regexps'], unreleased_version_label=config['unreleased_version_label'], diff --git a/gitchangelog.rc.reference b/gitchangelog.rc.reference index cb0a423..1a01a4f 100644 --- a/gitchangelog.rc.reference +++ b/gitchangelog.rc.reference @@ -50,11 +50,12 @@ ## message will be displayed in the changelog without reformatting. ## -## ``split_regex`` is a regex +## ``split_event_regex`` is a regex ## ## It is used to split a commit log entry into events before processing each event. ## -split_regex = r'(?s)(.+?)(?=[Cc]hg|[Ff]ix|[Nn]ew|$)' +#split_event_regex = r'(?s)(.+?)(?=(?:[Cc]hg|[Ff]ix|[Nn]ew)\s*:|\s*$)' +split_event_regex = None ##