Merge pull request #444 from snakescott/config

Add option to run from alternate config file
This commit is contained in:
Anthony Sottile
2016-12-03 11:04:39 -08:00
committed by GitHub
8 changed files with 136 additions and 63 deletions

View File

@@ -34,6 +34,13 @@ def _add_color_option(parser):
)
def _add_config_option(parser):
parser.add_argument(
'-c', '--config', default='.pre-commit-config.yaml',
help='Path to alternate config file'
)
def main(argv=None):
argv = argv if argv is not None else sys.argv[1:]
argv = [five.to_text(arg) for arg in argv]
@@ -54,6 +61,7 @@ def main(argv=None):
'install', help='Install the pre-commit script.',
)
_add_color_option(install_parser)
_add_config_option(install_parser)
install_parser.add_argument(
'-f', '--overwrite', action='store_true',
help='Overwrite existing hooks / remove migration mode.',
@@ -74,6 +82,7 @@ def main(argv=None):
'uninstall', help='Uninstall the pre-commit script.',
)
_add_color_option(uninstall_parser)
_add_config_option(uninstall_parser)
uninstall_parser.add_argument(
'-t', '--hook-type', choices=('pre-commit', 'pre-push'),
default='pre-commit',
@@ -83,15 +92,17 @@ def main(argv=None):
'clean', help='Clean out pre-commit files.',
)
_add_color_option(clean_parser)
_add_config_option(clean_parser)
autoupdate_parser = subparsers.add_parser(
'autoupdate',
help="Auto-update pre-commit config to the latest repos' versions.",
)
_add_color_option(autoupdate_parser)
_add_config_option(autoupdate_parser)
run_parser = subparsers.add_parser('run', help='Run hooks.')
_add_color_option(run_parser)
_add_config_option(run_parser)
run_parser.add_argument('hook', nargs='?', help='A single hook-id to run')
run_parser.add_argument(
'--no-stash', default=False, action='store_true',
@@ -152,7 +163,7 @@ def main(argv=None):
with error_handler():
add_logging_handler(args.color)
runner = Runner.create()
runner = Runner.create(args.config)
git.check_for_cygwin_mismatch()
if args.command == 'install':

View File

@@ -4,7 +4,6 @@ import os.path
from cached_property import cached_property
import pre_commit.constants as C
from pre_commit import git
from pre_commit.clientlib.validate_config import load_config
from pre_commit.repository import Repository
@@ -16,18 +15,19 @@ class Runner(object):
repository under test.
"""
def __init__(self, git_root):
def __init__(self, git_root, config_file):
self.git_root = git_root
self.config_file = config_file
@classmethod
def create(cls):
def create(cls, config_file):
"""Creates a PreCommitRunner by doing the following:
- Finds the root of the current git repository
- chdir to that directory
"""
root = git.get_root()
os.chdir(root)
return cls(root)
return cls(root, config_file)
@cached_property
def git_dir(self):
@@ -35,7 +35,7 @@ class Runner(object):
@cached_property
def config_file_path(self):
return os.path.join(self.git_root, C.CONFIG_FILE)
return os.path.join(self.git_root, self.config_file)
@cached_property
def repositories(self):