|
17 | 17 | from pre_commit.commands.migrate_config import migrate_config |
18 | 18 | from pre_commit.commands.run import run |
19 | 19 | from pre_commit.commands.sample_config import sample_config |
| 20 | +from pre_commit.commands.try_repo import try_repo |
20 | 21 | from pre_commit.error_handler import error_handler |
21 | 22 | from pre_commit.logging_handler import add_logging_handler |
22 | 23 | from pre_commit.runner import Runner |
@@ -53,6 +54,41 @@ def _add_hook_type_option(parser): |
53 | 54 | ) |
54 | 55 |
|
55 | 56 |
|
| 57 | +def _add_run_options(parser): |
| 58 | + parser.add_argument('hook', nargs='?', help='A single hook-id to run') |
| 59 | + parser.add_argument('--verbose', '-v', action='store_true', default=False) |
| 60 | + parser.add_argument( |
| 61 | + '--origin', '-o', |
| 62 | + help="The origin branch's commit_id when using `git push`.", |
| 63 | + ) |
| 64 | + parser.add_argument( |
| 65 | + '--source', '-s', |
| 66 | + help="The remote branch's commit_id when using `git push`.", |
| 67 | + ) |
| 68 | + parser.add_argument( |
| 69 | + '--commit-msg-filename', |
| 70 | + help='Filename to check when running during `commit-msg`', |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + '--hook-stage', choices=('commit', 'push', 'commit-msg'), |
| 74 | + default='commit', |
| 75 | + help='The stage during which the hook is fired e.g. commit or push.', |
| 76 | + ) |
| 77 | + parser.add_argument( |
| 78 | + '--show-diff-on-failure', action='store_true', |
| 79 | + help='When hooks fail, run `git diff` directly afterward.', |
| 80 | + ) |
| 81 | + mutex_group = parser.add_mutually_exclusive_group(required=False) |
| 82 | + mutex_group.add_argument( |
| 83 | + '--all-files', '-a', action='store_true', default=False, |
| 84 | + help='Run on all the files in the repo.', |
| 85 | + ) |
| 86 | + mutex_group.add_argument( |
| 87 | + '--files', nargs='*', default=[], |
| 88 | + help='Specific filenames to run hooks on.', |
| 89 | + ) |
| 90 | + |
| 91 | + |
56 | 92 | def main(argv=None): |
57 | 93 | argv = argv if argv is not None else sys.argv[1:] |
58 | 94 | argv = [five.to_text(arg) for arg in argv] |
@@ -142,47 +178,32 @@ def main(argv=None): |
142 | 178 | run_parser = subparsers.add_parser('run', help='Run hooks.') |
143 | 179 | _add_color_option(run_parser) |
144 | 180 | _add_config_option(run_parser) |
145 | | - run_parser.add_argument('hook', nargs='?', help='A single hook-id to run') |
146 | | - run_parser.add_argument( |
147 | | - '--verbose', '-v', action='store_true', default=False, |
148 | | - ) |
149 | | - run_parser.add_argument( |
150 | | - '--origin', '-o', |
151 | | - help="The origin branch's commit_id when using `git push`.", |
152 | | - ) |
153 | | - run_parser.add_argument( |
154 | | - '--source', '-s', |
155 | | - help="The remote branch's commit_id when using `git push`.", |
156 | | - ) |
157 | | - run_parser.add_argument( |
158 | | - '--commit-msg-filename', |
159 | | - help='Filename to check when running during `commit-msg`', |
160 | | - ) |
161 | | - run_parser.add_argument( |
162 | | - '--hook-stage', choices=('commit', 'push', 'commit-msg'), |
163 | | - default='commit', |
164 | | - help='The stage during which the hook is fired e.g. commit or push.', |
165 | | - ) |
166 | | - run_parser.add_argument( |
167 | | - '--show-diff-on-failure', action='store_true', |
168 | | - help='When hooks fail, run `git diff` directly afterward.', |
169 | | - ) |
170 | | - run_mutex_group = run_parser.add_mutually_exclusive_group(required=False) |
171 | | - run_mutex_group.add_argument( |
172 | | - '--all-files', '-a', action='store_true', default=False, |
173 | | - help='Run on all the files in the repo.', |
174 | | - ) |
175 | | - run_mutex_group.add_argument( |
176 | | - '--files', nargs='*', default=[], |
177 | | - help='Specific filenames to run hooks on.', |
178 | | - ) |
| 181 | + _add_run_options(run_parser) |
179 | 182 |
|
180 | 183 | sample_config_parser = subparsers.add_parser( |
181 | 184 | 'sample-config', help='Produce a sample {} file'.format(C.CONFIG_FILE), |
182 | 185 | ) |
183 | 186 | _add_color_option(sample_config_parser) |
184 | 187 | _add_config_option(sample_config_parser) |
185 | 188 |
|
| 189 | + try_repo_parser = subparsers.add_parser( |
| 190 | + 'try-repo', |
| 191 | + help='Try the hooks in a repository, useful for developing new hooks.', |
| 192 | + ) |
| 193 | + _add_color_option(try_repo_parser) |
| 194 | + _add_config_option(try_repo_parser) |
| 195 | + try_repo_parser.add_argument( |
| 196 | + 'repo', help='Repository to source hooks from.', |
| 197 | + ) |
| 198 | + try_repo_parser.add_argument( |
| 199 | + '--ref', |
| 200 | + help=( |
| 201 | + 'Manually select a ref to run against, otherwise the `HEAD` ' |
| 202 | + 'revision will be used.' |
| 203 | + ), |
| 204 | + ) |
| 205 | + _add_run_options(try_repo_parser) |
| 206 | + |
186 | 207 | help = subparsers.add_parser( |
187 | 208 | 'help', help='Show help for a specific command.', |
188 | 209 | ) |
@@ -231,6 +252,8 @@ def main(argv=None): |
231 | 252 | return run(runner, args) |
232 | 253 | elif args.command == 'sample-config': |
233 | 254 | return sample_config() |
| 255 | + elif args.command == 'try-repo': |
| 256 | + return try_repo(args) |
234 | 257 | else: |
235 | 258 | raise NotImplementedError( |
236 | 259 | 'Command {} not implemented.'.format(args.command), |
|
0 commit comments