Skip to content

Commit

Permalink
Improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jun 27, 2018
1 parent 013fb69 commit 5e590f6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 33 deletions.
26 changes: 13 additions & 13 deletions src/gitolog/build.py
Expand Up @@ -114,7 +114,7 @@ def is_minor(self):
return self.tag.split('.', 2)[2]


class Gitolog:
class Changelog:
MARKER = '--GITOLOG MARKER--'
FORMAT = (
'%H%n' # commit hash
Expand Down Expand Up @@ -177,10 +177,11 @@ def __init__(self, repository, provider=None, style=None):
self.versions_dict = versions['as_dict']

# guess the next version number based on last version and recent commits
if not self.versions_list[0].tag and len(self.versions_list) > 1:
last_tag = self.versions_list[1].tag
last_version = self.versions_list[0]
if not last_version.tag and last_version.previous_version:
last_tag = last_version.previous_version.tag
major = minor = False
for commit in self.versions_list[0].commits:
for commit in last_version.commits:
if commit.style['is_major']:
major = True
break
Expand All @@ -192,7 +193,10 @@ def __init__(self, repository, provider=None, style=None):
planned_tag = bump(last_tag, 'minor')
else:
planned_tag = bump(last_tag, 'patch')
self.versions_list[0].planned_tag = planned_tag
last_version.planned_tag = planned_tag
last_version.url = self.provider.get_tag_url(tag=planned_tag)
last_version.compare_url = self.provider.get_compare_url(
base=last_version.previous_version.tag, target=last_version.planned_tag)

def get_remote_url(self):
git_url = str(check_output(
Expand Down Expand Up @@ -276,10 +280,8 @@ def group_commits_by_version(self, dates):
if next_version:
version.next_version = next_version
next_version.previous_version = version
next_version.compare_url = self.provider.build_ref_url(
# FIXME: hardcoded 'commits_ranges'
'commits_ranges', {'ref': '%s...%s' % (
version.tag, next_version.tag or 'HEAD')})
next_version.compare_url = self.provider.get_compare_url(
base=version.tag, target=next_version.tag or 'HEAD')
next_version = version
versions_list.append(version)
versions_types_dict[commit.version] = {}
Expand All @@ -291,9 +293,7 @@ def group_commits_by_version(self, dates):
versions_dict[commit.version].sections_list.append(section)
versions_dict[commit.version].sections_dict = versions_types_dict[commit.version]
versions_types_dict[commit.version][commit.style['type']].commits.append(commit)
next_version.compare_url = self.provider.build_ref_url(
# FIXME: hardcoded 'commits_ranges'
'commits_ranges', {'ref': '%s...%s' % (
versions_list[-1].commits[-1].hash, next_version.tag or 'HEAD')})
next_version.compare_url = self.provider.get_compare_url(
base=versions_list[-1].commits[-1].hash, target=next_version.tag or 'HEAD')
return {'as_list': versions_list, 'as_dict': versions_dict}

6 changes: 3 additions & 3 deletions src/gitolog/cli.py
Expand Up @@ -19,7 +19,7 @@
import sys

from . import __version__, templates
from .build import Gitolog
from .build import Changelog


STYLES = ('angular', 'atom', 'basic')
Expand Down Expand Up @@ -78,10 +78,10 @@ def main(args=None):
template = templates.get_template(args.template)

# build data
gitolog = Gitolog(args.repository, style=args.style)
changelog = Changelog(args.repository, style=args.style)

# get rendered contents
rendered = template.render(gitolog=gitolog)
rendered = template.render(changelog=changelog)

# write result in specified output
if args.output is sys.stdout:
Expand Down
6 changes: 6 additions & 0 deletions src/gitolog/providers.py
Expand Up @@ -80,6 +80,9 @@ def get_tag_url(self, tag=''):
return self.tag_url.format(
base_url=self.url, namespace=self.namespace, project=self.project, ref=tag)

def get_compare_url(self, base, target):
return self.build_ref_url('commits_ranges', {'ref': '%s...%s' % (base, target)})


class GitLab(ProviderRefParser):
url = 'https://gitlab.com'
Expand Down Expand Up @@ -165,3 +168,6 @@ def build_ref_url(self, ref_type, match_dict):
def get_tag_url(self, tag=''):
return self.tag_url.format(
base_url=self.url, namespace=self.namespace, project=self.project, ref=tag)

def get_compare_url(self, base, target):
return self.build_ref_url('commits_ranges', {'ref': '%s...%s' % (base, target)})
8 changes: 1 addition & 7 deletions src/gitolog/templates/angular/changelog.md
@@ -1,9 +1,3 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

{% for version in gitolog.versions_list -%}
{% for version in changelog.versions_list -%}
{% include 'version.md' with context %}
{% endfor -%}
14 changes: 8 additions & 6 deletions src/gitolog/templates/angular/version.md
@@ -1,12 +1,14 @@
{%- if version.tag -%}
<a name="{{ version.tag }}"></a>
## [{{ version.tag }}]({{ version.compare_url }})
{%- if version.tag or version.planned_tag -%}
<a name="{{ version.tag or version.planned_tag }}"></a>
## [{{ version.tag or version.planned_tag }}]({{ version.compare_url }})
{%- else -%}
<a name="{{ version.planned_tag or "Unrealeased" }}"></a>
## {{ version.planned_tag or "Unrealeased" }} ([compare]({{ version.compare_url }})){%- endif -%}{% if version.date %} ({{ version.date }}){% endif %}
<a name="Unrealeased"></a>
## Unrealeased ([compare]({{ version.compare_url }}))
{%- endif -%}
{% if version.date %} ({{ version.date }}){% endif %}

{% for type, section in version.sections_dict|dictsort -%}
{%- if type and type != 'Merged' -%}
{%- if type -%}
{% include 'section.md' with context %}
{% endif -%}
{%- endfor -%}
2 changes: 1 addition & 1 deletion src/gitolog/templates/keepachangelog/changelog.md
Expand Up @@ -4,6 +4,6 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

{% for version in gitolog.versions_list -%}
{% for version in changelog.versions_list -%}
{% include 'version.md' with context %}
{% endfor -%}
8 changes: 5 additions & 3 deletions src/gitolog/templates/keepachangelog/version.md
@@ -1,7 +1,9 @@
{%- if version.tag -%}
## [{{ version.tag }}]({{ version.url }}) ([compare]({{ version.compare_url }}))
{%- if version.tag or version.planned_tag -%}
## [{{ version.tag or version.planned_tag }}]({{ version.url }}) ([compare]({{ version.compare_url }}))
{%- else -%}
## {{ version.planned_tag or "Unrealeased" }} ([compare]({{ version.compare_url }})){%- endif -%}{% if version.date %} - {{ version.date }}{% endif %}
## Unrealeased ([compare]({{ version.compare_url }}))
{%- endif -%}
{% if version.date %} - {{ version.date }}{% endif %}

{% for type, section in version.sections_dict|dictsort -%}
{%- if type and type != 'Merged' -%}
Expand Down

0 comments on commit 5e590f6

Please sign in to comment.