From 758faa4ae782e6a3987e762f5071f299101919c3 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 23 Jun 2016 08:29:33 -0700 Subject: [PATCH] Autoupdate to tags when available --- pre_commit/commands/autoupdate.py | 12 +++++++++--- tests/commands/autoupdate_test.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pre_commit/commands/autoupdate.py b/pre_commit/commands/autoupdate.py index 5dd97219..b72ab289 100644 --- a/pre_commit/commands/autoupdate.py +++ b/pre_commit/commands/autoupdate.py @@ -15,6 +15,7 @@ from pre_commit.jsonschema_extensions import remove_defaults from pre_commit.logging_handler import LoggingHandler from pre_commit.ordereddict import OrderedDict from pre_commit.repository import Repository +from pre_commit.util import CalledProcessError from pre_commit.util import cmd_output from pre_commit.util import cwd @@ -38,15 +39,20 @@ def _update_repository(repo_config, runner): with cwd(repo.repo_path_getter.repo_path): cmd_output('git', 'fetch') - head_sha = cmd_output('git', 'rev-parse', 'origin/master')[1].strip() + try: + rev = cmd_output( + 'git', 'describe', 'origin/master', '--tags', '--exact', + )[1].strip() + except CalledProcessError: + rev = cmd_output('git', 'rev-parse', 'origin/master')[1].strip() # Don't bother trying to update if our sha is the same - if head_sha == repo_config['sha']: + if rev == repo_config['sha']: return repo_config # Construct a new config with the head sha new_config = OrderedDict(repo_config) - new_config['sha'] = head_sha + new_config['sha'] = rev new_repo = Repository.create(new_config, runner.store) # See if any of our hooks were deleted with the new commits diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index bd8fbe80..62a0269f 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -96,6 +96,26 @@ def test_autoupdate_out_of_date_repo( assert out_of_date_repo.head_sha in after +@pytest.yield_fixture +def tagged_repo(out_of_date_repo): + with cwd(out_of_date_repo.path): + cmd_output('git', 'tag', 'v1.2.3') + yield out_of_date_repo + + +def test_autoupdate_tagged_repo( + tagged_repo, in_tmpdir, mock_out_store_directory, +): + config = make_config_from_repo( + tagged_repo.path, sha=tagged_repo.original_sha, + ) + write_config('.', config) + + ret = autoupdate(Runner('.')) + assert ret == 0 + assert 'v1.2.3' in open(C.CONFIG_FILE).read() + + @pytest.yield_fixture def hook_disappearing_repo(tempdir_factory): path = make_repo(tempdir_factory, 'python_hooks_repo')