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

WIP: --author-format and --omit-author options #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 40 additions & 6 deletions src/gitchangelog/gitchangelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
usage_msg = """
%(exname)s {-h|--help}
%(exname)s {-v|--version}
%(exname)s [--debug|-d] [REVLIST]"""
%(exname)s [--debug|-d] [--author-format=name|email|both|none] [--omit-author authorname ...] [REVLIST]"""

description_msg = """\
Run this command in a git repository to output a formatted changelog
Expand Down Expand Up @@ -912,6 +912,11 @@ def author_names(self):
return [re.sub(r'^([^<]+)<[^>]+>\s*$', r'\1', author).strip()
for author in self.authors]

@property
def author_emails(self):
return [re.sub(r'^[^<]+<([^>]+)>\s*$', r'\1', author).strip()
for author in self.authors]

@property
def authors(self):
co_authors = getattr(self, 'trailer_co_authored_by', [])
Expand Down Expand Up @@ -1327,7 +1332,27 @@ def render_version(version):

def render_commit(commit, opts=opts):
subject = commit["subject"]
subject += " [%s]" % (", ".join(commit["authors"]), )
if opts["author_format"] != "none":
if opts["author_format"] == "both":
authors = commit["commit"].authors
elif opts["author_format"] == "name":
authors = commit["commit"].author_names
elif opts["author_format"] == "email":
authors = commit["commit"].author_emails
else:
assert(not "Unhandled author_format")
authors = []

if authors:
for omit in [o.lower() for o in opts["omitted_authors"]]:
for author in list(authors):
if omit in author.lower():
authors.remove(author)

if authors:
start, end = ("<", ">") \
if opts["author_format"] == "email" else ("[", "]")
subject += " %s%s%s" % (start, ", ".join(authors), end)

entry = indent('\n'.join(textwrap.wrap(subject)),
first="- ").strip() + "\n"
Expand Down Expand Up @@ -1597,6 +1622,7 @@ def versions_data_iter(repository, revlist=None,
sections[matched_section].append({
"author": commit.author_name,
"authors": commit.author_names,
"author_email": commit.author_email,
"subject": subject_process(commit.subject),
"body": body_process(commit.body),
"commit": commit,
Expand All @@ -1611,7 +1637,7 @@ def versions_data_iter(repository, revlist=None,
versions_done[tag] = current_version


def changelog(output_engine=rest_py,
def changelog(opts, output_engine=rest_py,
unreleased_version_label="unreleased",
warn=warn, ## Mostly used for test
**kwargs):
Expand All @@ -1633,9 +1659,7 @@ def changelog(output_engine=rest_py,

"""

opts = {
'unreleased_version_label': unreleased_version_label,
}
opts['unreleased_version_label'] = unreleased_version_label

## Setting main container of changelog elements
title = None if kwargs.get("revlist") else "Changelog"
Expand Down Expand Up @@ -1724,6 +1748,14 @@ def parse_cmd_line(usage, description, epilog, exname, version):
help="Enable debug mode (show full tracebacks).",
action="store_true", dest="debug")
parser.add_argument('revlist', nargs='*', action="store", default=[])
parser.add_argument('--author-format', action="store", default="name",
choices=["name", "email", "both", "none"])
parser.add_argument('--omit-author', action="append", default=[],
dest="omitted_authors",
help="If the author contains the argument value "
"it is not displayed along with the "
"changelog entry. The option may be specified "
"multiple times.")

## Remove "show" as first argument for compatibility reason.

Expand Down Expand Up @@ -1893,6 +1925,7 @@ def main():
exname=basename,
version=__version__)
DEBUG = DEBUG or opts.debug
opts_dict = dict(opts.__dict__)

try:
repository = GitRepos(".")
Expand Down Expand Up @@ -1951,6 +1984,7 @@ def main():

try:
content = changelog(
opts_dict,
repository=repository, revlist=revlist,
ignore_regexps=config['ignore_regexps'],
section_regexps=config['section_regexps'],
Expand Down