Skip to content

Commit f1c00ee

Browse files
author
Jacob Scott
committed
Add option to run from alternate config file
1 parent da3458c commit f1c00ee

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

pre_commit/main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def _add_color_option(parser):
3434
)
3535

3636

37+
def _add_config_option(parser):
38+
parser.add_argument('-c', '--config', help='Path to alternate config file')
39+
40+
3741
def main(argv=None):
3842
argv = argv if argv is not None else sys.argv[1:]
3943
argv = [five.to_text(arg) for arg in argv]
@@ -89,6 +93,7 @@ def main(argv=None):
8993
help="Auto-update pre-commit config to the latest repos' versions.",
9094
)
9195
_add_color_option(autoupdate_parser)
96+
_add_config_option(autoupdate_parser)
9297

9398
run_parser = subparsers.add_parser('run', help='Run hooks.')
9499
_add_color_option(run_parser)
@@ -119,6 +124,7 @@ def main(argv=None):
119124
'--hook-stage', choices=('commit', 'push'), default='commit',
120125
help='The stage during which the hook is fired e.g. commit or push.',
121126
)
127+
_add_config_option(run_parser)
122128
run_mutex_group = run_parser.add_mutually_exclusive_group(required=False)
123129
run_mutex_group.add_argument(
124130
'--all-files', '-a', action='store_true', default=False,
@@ -152,7 +158,10 @@ def main(argv=None):
152158

153159
with error_handler():
154160
add_logging_handler(args.color)
155-
runner = Runner.create()
161+
runner_kwargs = {}
162+
if hasattr(args, 'config_file'):
163+
runner_kwargs['config_file'] = args.config_file
164+
runner = Runner.create(**runner_kwargs)
156165
git.check_for_cygwin_mismatch()
157166

158167
if args.command == 'install':

pre_commit/runner.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,27 @@ class Runner(object):
1616
repository under test.
1717
"""
1818

19-
def __init__(self, git_root):
19+
def __init__(self, git_root, config_file=None):
2020
self.git_root = git_root
21+
self.config_file = config_file or C.CONFIG_FILE
2122

2223
@classmethod
23-
def create(cls):
24+
def create(cls, config_file=None):
2425
"""Creates a PreCommitRunner by doing the following:
2526
- Finds the root of the current git repository
2627
- chdir to that directory
2728
"""
2829
root = git.get_root()
2930
os.chdir(root)
30-
return cls(root)
31+
return cls(root, config_file=config_file)
3132

3233
@cached_property
3334
def git_dir(self):
3435
return git.get_git_dir(self.git_root)
3536

3637
@cached_property
3738
def config_file_path(self):
38-
return os.path.join(self.git_root, C.CONFIG_FILE)
39+
return os.path.join(self.git_root, self.config_file)
3940

4041
@cached_property
4142
def repositories(self):

0 commit comments

Comments
 (0)