From 17162074d9b24471e1f66f7dc5cd288f17167969 Mon Sep 17 00:00:00 2001 From: makimov Date: Wed, 26 Sep 2018 17:06:58 +0300 Subject: [PATCH 1/6] Added the ability to receive a short version of the commit --- README.md | 10 ++++++++++ jinja2_git.py | 17 +++++++++++++---- tests/test_gitcommit.py | 6 ++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2a2153b..701d6df 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,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 ------------ diff --git a/jinja2_git.py b/jinja2_git.py index c41e831..efa5def 100644 --- a/jinja2_git.py +++ b/jinja2_git.py @@ -11,14 +11,23 @@ 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) diff --git a/tests/test_gitcommit.py b/tests/test_gitcommit.py index 2ad6c39..071cf02 100644 --- a/tests/test_gitcommit.py +++ b/tests/test_gitcommit.py @@ -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( From 6f321675a54a1e56751a82ea61332d7aa3b1601d Mon Sep 17 00:00:00 2001 From: makimov Date: Wed, 26 Sep 2018 17:35:28 +0300 Subject: [PATCH 2/6] Fixing FLAKE8-check error (line 16) --- jinja2_git.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/jinja2_git.py b/jinja2_git.py index efa5def..29ae62e 100644 --- a/jinja2_git.py +++ b/jinja2_git.py @@ -13,7 +13,11 @@ class GitExtension(Extension): def _commit_hash(self, *args): if args[0]: - output = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']) + output = subprocess.check_output(['git', + 'rev-parse', + '--short', + 'HEAD' + ]) else: output = subprocess.check_output(['git', 'rev-parse', 'HEAD']) return output.decode('utf-8').strip() From 0f058459e86627794a42d07130958d9f1c4575b7 Mon Sep 17 00:00:00 2001 From: makimov Date: Wed, 26 Sep 2018 17:39:51 +0300 Subject: [PATCH 3/6] fixing FLAKE8-check (missing trailing comma at line 19) --- jinja2_git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jinja2_git.py b/jinja2_git.py index 29ae62e..effef25 100644 --- a/jinja2_git.py +++ b/jinja2_git.py @@ -16,7 +16,7 @@ def _commit_hash(self, *args): output = subprocess.check_output(['git', 'rev-parse', '--short', - 'HEAD' + 'HEAD', ]) else: output = subprocess.check_output(['git', 'rev-parse', 'HEAD']) From 120a200b0407d7ca7bd82f09483098d433660b93 Mon Sep 17 00:00:00 2001 From: makimov Date: Thu, 27 Sep 2018 12:05:05 +0300 Subject: [PATCH 4/6] Fixed after review. --- jinja2_git.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/jinja2_git.py b/jinja2_git.py index effef25..d59cf2a 100644 --- a/jinja2_git.py +++ b/jinja2_git.py @@ -11,15 +11,13 @@ class GitExtension(Extension): tags = {'gitcommit'} - 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']) + def _commit_hash(self, short=False): + params = ['git', 'rev-parse', 'HEAD'] + + if short: + params.insert(2, '--short') + + output = subprocess.check_output(params) return output.decode('utf-8').strip() def parse(self, parser): From 748c590fcd72de919dc8691772b1493888cd9bce Mon Sep 17 00:00:00 2001 From: makimov Date: Thu, 27 Sep 2018 12:31:34 +0300 Subject: [PATCH 5/6] Added the ability to receive a short version of the commit --- jinja2_git.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/jinja2_git.py b/jinja2_git.py index d59cf2a..a1d4bbc 100644 --- a/jinja2_git.py +++ b/jinja2_git.py @@ -30,6 +30,5 @@ def parse(self, parser): else: short = nodes.Const(False) - args = [short] - result = self.call_method('_commit_hash', args, [], lineno=lineno) + result = self.call_method('_commit_hash', [short], [], lineno=lineno) return nodes.Output([result], lineno=lineno) From 90a10a60587bfb327184952cd49d9359f769f946 Mon Sep 17 00:00:00 2001 From: makimov Date: Thu, 27 Sep 2018 13:04:53 +0300 Subject: [PATCH 6/6] Removing default value for param 'short' in _commit_hash() --- jinja2_git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jinja2_git.py b/jinja2_git.py index a1d4bbc..7f92916 100644 --- a/jinja2_git.py +++ b/jinja2_git.py @@ -11,7 +11,7 @@ class GitExtension(Extension): tags = {'gitcommit'} - def _commit_hash(self, short=False): + def _commit_hash(self, short): params = ['git', 'rev-parse', 'HEAD'] if short: