Skip to content

Commit

Permalink
Merge 0f05845 into 23172f3
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-akimov committed Sep 26, 2018
2 parents 23172f3 + 0f05845 commit 70dcb97
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -29,6 +29,16 @@ template = env.from_string('Commit is: {% gitcommit %}')
# => Commit is: c644682f4899d7e98147ce3a61a11bb13c52b3a0
```

Or short version:

```python
from jinja2 import Environment

env = Environment(extensions=['jinja2_git.GitExtension'])
template = env.from_string('Commit is: {% gitcommit short=True %}')
# => Commit is: c644682
```

## Installation

```bash
Expand Down
21 changes: 17 additions & 4 deletions jinja2_git.py
Expand Up @@ -11,14 +11,27 @@ class GitExtension(Extension):

tags = {'gitcommit'}

def _commit_hash(self):
output = subprocess.check_output(['git', 'rev-parse', 'HEAD'])
def _commit_hash(self, *args):
if args[0]:
output = subprocess.check_output(['git',
'rev-parse',
'--short',
'HEAD',
])
else:
output = subprocess.check_output(['git', 'rev-parse', 'HEAD'])
return output.decode('utf-8').strip()

def parse(self, parser):
"""Main method to render data into the template."""
lineno = next(parser.stream).lineno

# TODO: add {% gitcommit 'short' %} feature
result = self.call_method('_commit_hash', [], lineno=lineno)
if parser.stream.skip_if('name:short'):
parser.stream.skip(1)
short = parser.parse_expression()
else:
short = nodes.Const(False)

args = [short]
result = self.call_method('_commit_hash', args, [], lineno=lineno)
return nodes.Output([result], lineno=lineno)
6 changes: 6 additions & 0 deletions tests/test_gitcommit.py
Expand Up @@ -9,6 +9,12 @@ def test_commit_length(environment):
assert len(template.render()) == 40


def test_short_commit_length(environment):
template = environment.from_string('{% gitcommit short=True %}')

assert len(template.render()) == 7


def test_is_commit(environment):
template = environment.from_string('{% gitcommit %}')
output = subprocess.check_output(
Expand Down

0 comments on commit 70dcb97

Please sign in to comment.