Skip to content

Commit

Permalink
new: template path can now be specified in git config. (fixes #73)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaab committed Mar 17, 2017
1 parent 0406f7b commit f53d1cc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 37 deletions.
35 changes: 21 additions & 14 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,20 @@ of gitchangelog. These can be called by providing a simple label to the

output_engine = mustache("markdown")

Or you could provide your own mustache template by specifying an absolute
path (or a relative one, starting from the git toplevel of your project) to
your template file, for instance::
Or you could provide your own mustache template by specifying an
absolute path (or a relative one, starting from the git toplevel of
your project by default, or if set, the
``git config gitchangelog.template-path``
location) to your template file, for instance::

output_engine = mustache(".gitchangelog.tpl")

And feel free to copy the bundled templates to use them as bases for your
own variations. These are located in ``src/gitchangelog/templates/mustache``
directory in the source and are installed in ``templates/mustache`` directory
starting from where your ``gitchangelog.py`` was installed.
And feel free to copy the bundled templates to use them as bases for
your own variations. In the source code, these are located in
``src/gitchangelog/templates/mustache`` directory, once installed they
are in ``templates/mustache`` directory starting from where your
``gitchangelog.py`` was installed.


.. _mustache: http://mustache.github.io
.. _pystache: https://pypi.python.org/pypi/pystache
Expand All @@ -302,16 +306,19 @@ of gitchangelog. These can be called by providing a simple label to the

output_engine = makotemplate("markdown")

Or you could provide your own mako template by specifying an absolute
path (or a relative one, starting from the git toplevel of your project) to
your template file, for instance::
Or you could provide your own mustache template by specifying an
absolute path (or a relative one, starting from the git toplevel of
your project by default, or if set, the
``git config gitchangelog.template-path``
location) to your template file, for instance::

output_engine = makotemplate(".gitchangelog.tpl")

And feel free to copy the bundled templates to use them as bases for your
own variations. These are located in ``src/gitchangelog/templates/mako``
directory in the source and are installed in ``templates/mako`` directory
starting from where your ``gitchangelog.py`` was installed.
And feel free to copy the bundled templates to use them as bases for
your own variations. In the source code, these are located in
``src/gitchangelog/templates/mako`` directory, once installed they
are in ``templates/mako`` directory starting from where your
``gitchangelog.py`` was installed.

.. _mako: http://www.makotemplates.org

Expand Down
56 changes: 33 additions & 23 deletions src/gitchangelog/gitchangelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,30 +1253,40 @@ def ensure_template_file_exists(label, template_name):
"""

if os.path.isfile(template_name):
return template_name

template_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"templates", label)

template_path = os.path.join(template_dir, "%s.tpl" % template_name)

if not os.path.exists(template_path):
templates = glob.glob(os.path.join(template_dir, "*.tpl"))
if len(templates) > 0:
msg = ("These are the available %s templates:" % label)
msg += "\n - " + \
"\n - ".join(os.path.basename(f).split(".")[0]
for f in templates)
msg += "\nTemplates are located in %r" % template_dir
else:
msg = "No available %s templates found in %r." \
% (label, template_dir)
die("Error: Invalid %s template name %r.\n" % (label, template_name) +
"%s" % msg)
try:
template_path = GitRepos(os.getcwd()).config.get(
"gitchangelog.template-path")
except ShellError as e:
stderr(
"Error parsing git config: %s."
" Won't be able to read 'template-path' if defined."
% (str(e)))
template_path = None

return template_path
if template_path:
path_file = path_label = template_path
else:
path_file = os.getcwd()
path_label = os.path.join(os.path.dirname(os.path.realpath(__file__)),
"templates", label)

for ftn in [os.path.join(path_file, template_name),
os.path.join(path_label, "%s.tpl" % template_name)]:
if os.path.isfile(ftn):
return ftn

templates = glob.glob(os.path.join(path_label, "*.tpl"))
if len(templates) > 0:
msg = ("These are the available %s templates:" % label)
msg += "\n - " + \
"\n - ".join(os.path.basename(f).split(".")[0]
for f in templates)
msg += "\nTemplates are located in %r" % path_label
else:
msg = "No available %s templates found in %r." \
% (label, path_label)
die("Error: Invalid %s template name %r.\n" % (label, template_name) +
"%s" % msg)


##
Expand Down
25 changes: 25 additions & 0 deletions test/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import unicode_literals

import os
import textwrap

from .common import BaseGitReposTest, cmd, gitchangelog
Expand Down Expand Up @@ -73,6 +74,30 @@ def test_file_template_name(self):
self.assertNoDiff(
reference, out)

def test_file_template_name_with_git_config_path(self):

os.mkdir('XYZ')
gitchangelog.file_put_contents(
os.path.join('XYZ', "mytemplate.tpl"),
"check: {{{title}}}")
gitchangelog.file_put_contents(
".gitchangelog.rc",
"output_engine = mustache('mytemplate.tpl')")
self.git.config('gitchangelog.template-path', 'XYZ')

reference = """check: Changelog"""

out, err, errlvl = cmd('$tprog --debug')
self.assertEqual(
err, "",
msg="There should be no error messages. "
"Current stderr:\n%s" % err)
self.assertEqual(
errlvl, 0,
msg="Should succeed to find template")
self.assertNoDiff(
reference, out)

def test_template_has_access_to_full_commit(self):
"""Existing files should be accepted as valid templates"""

Expand Down

0 comments on commit f53d1cc

Please sign in to comment.