| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -112,14 +112,14 @@ def _write_new_config_file(path, output): | |||
| 112 | 112 | f.write(to_write) | |
| 113 | 113 | ||
| 114 | 114 | ||
| 115 | - def autoupdate(runner, store, tags_only, repos=()): | ||
| 115 | + def autoupdate(config_file, store, tags_only, repos=()): | ||
| 116 | 116 | """Auto-update the pre-commit config to the latest versions of repos.""" | |
| 117 | - migrate_config(runner, quiet=True) | ||
| 117 | + migrate_config(config_file, quiet=True) | ||
| 118 | 118 | retv = 0 | |
| 119 | 119 | output_repos = [] | |
| 120 | 120 | changed = False | |
| 121 | 121 | ||
| 122 | - input_config = load_config(runner.config_file_path) | ||
| 122 | + input_config = load_config(config_file) | ||
| 123 | 123 | ||
| 124 | 124 | for repo_config in input_config['repos']: | |
| 125 | 125 | if ( | |
@@ -152,6 +152,6 @@ def autoupdate(runner, store, tags_only, repos=()): | |||
| 152 | 152 | if changed: | |
| 153 | 153 | output_config = input_config.copy() | |
| 154 | 154 | output_config['repos'] = output_repos | |
| 155 | - _write_new_config_file(runner.config_file_path, output_config) | ||
| 155 | + _write_new_config_file(config_file, output_config) | ||
| 156 | 156 | ||
| 157 | 157 | return retv | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ | |||
| 8 | 8 | ||
| 9 | 9 | from pre_commit import git | |
| 10 | 10 | from pre_commit import output | |
| 11 | + from pre_commit.clientlib import load_config | ||
| 11 | 12 | from pre_commit.languages import python | |
| 12 | 13 | from pre_commit.repository import repositories | |
| 13 | 14 | from pre_commit.util import cmd_output | |
@@ -31,8 +32,8 @@ | |||
| 31 | 32 | TEMPLATE_END = '# end templated\n' | |
| 32 | 33 | ||
| 33 | 34 | ||
| 34 | - def _hook_paths(git_root, hook_type): | ||
| 35 | - pth = os.path.join(git.get_git_dir(git_root), 'hooks', hook_type) | ||
| 35 | + def _hook_paths(hook_type): | ||
| 36 | + pth = os.path.join(git.get_git_dir(), 'hooks', hook_type) | ||
| 36 | 37 | return pth, '{}.legacy'.format(pth) | |
| 37 | 38 | ||
| 38 | 39 | ||
@@ -55,7 +56,8 @@ def shebang(): | |||
| 55 | 56 | ||
| 56 | 57 | ||
| 57 | 58 | def install( | |
| 58 | - runner, store, overwrite=False, hooks=False, hook_type='pre-commit', | ||
| 59 | + config_file, store, | ||
| 60 | + overwrite=False, hooks=False, hook_type='pre-commit', | ||
| 59 | 61 | skip_on_missing_conf=False, | |
| 60 | 62 | ): | |
| 61 | 63 | """Install the pre-commit hooks.""" | |
@@ -66,7 +68,7 @@ def install( | |||
| 66 | 68 | ) | |
| 67 | 69 | return 1 | |
| 68 | 70 | ||
| 69 | - hook_path, legacy_path = _hook_paths(runner.git_root, hook_type) | ||
| 71 | + hook_path, legacy_path = _hook_paths(hook_type) | ||
| 70 | 72 | ||
| 71 | 73 | mkdirp(os.path.dirname(hook_path)) | |
| 72 | 74 | ||
@@ -84,7 +86,7 @@ def install( | |||
| 84 | 86 | ) | |
| 85 | 87 | ||
| 86 | 88 | params = { | |
| 87 | - 'CONFIG': runner.config_file, | ||
| 89 | + 'CONFIG': config_file, | ||
| 88 | 90 | 'HOOK_TYPE': hook_type, | |
| 89 | 91 | 'INSTALL_PYTHON': sys.executable, | |
| 90 | 92 | 'SKIP_ON_MISSING_CONFIG': skip_on_missing_conf, | |
@@ -108,19 +110,19 @@ def install( | |||
| 108 | 110 | ||
| 109 | 111 | # If they requested we install all of the hooks, do so. | |
| 110 | 112 | if hooks: | |
| 111 | - install_hooks(runner, store) | ||
| 113 | + install_hooks(config_file, store) | ||
| 112 | 114 | ||
| 113 | 115 | return 0 | |
| 114 | 116 | ||
| 115 | 117 | ||
| 116 | - def install_hooks(runner, store): | ||
| 117 | - for repository in repositories(runner.config, store): | ||
| 118 | + def install_hooks(config_file, store): | ||
| 119 | + for repository in repositories(load_config(config_file), store): | ||
| 118 | 120 | repository.require_installed() | |
| 119 | 121 | ||
| 120 | 122 | ||
| 121 | - def uninstall(runner, hook_type='pre-commit'): | ||
| 123 | + def uninstall(hook_type='pre-commit'): | ||
| 122 | 124 | """Uninstall the pre-commit hooks.""" | |
| 123 | - hook_path, legacy_path = _hook_paths(runner.git_root, hook_type) | ||
| 125 | + hook_path, legacy_path = _hook_paths(hook_type) | ||
| 124 | 126 | ||
| 125 | 127 | # If our file doesn't exist or it isn't ours, gtfo. | |
| 126 | 128 | if not os.path.exists(hook_path) or not is_our_script(hook_path): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,15 +45,15 @@ def _migrate_sha_to_rev(contents): | |||
| 45 | 45 | return reg.sub(r'\1rev:', contents) | |
| 46 | 46 | ||
| 47 | 47 | ||
| 48 | - def migrate_config(runner, quiet=False): | ||
| 49 | - with io.open(runner.config_file_path) as f: | ||
| 48 | + def migrate_config(config_file, quiet=False): | ||
| 49 | + with io.open(config_file) as f: | ||
| 50 | 50 | orig_contents = contents = f.read() | |
| 51 | 51 | ||
| 52 | 52 | contents = _migrate_map(contents) | |
| 53 | 53 | contents = _migrate_sha_to_rev(contents) | |
| 54 | 54 | ||
| 55 | 55 | if contents != orig_contents: | |
| 56 | - with io.open(runner.config_file_path, 'w') as f: | ||
| 56 | + with io.open(config_file, 'w') as f: | ||
| 57 | 57 | f.write(contents) | |
| 58 | 58 | ||
| 59 | 59 | print('Configuration has been migrated.') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,6 +11,7 @@ | |||
| 11 | 11 | from pre_commit import color | |
| 12 | 12 | from pre_commit import git | |
| 13 | 13 | from pre_commit import output | |
| 14 | + from pre_commit.clientlib import load_config | ||
| 14 | 15 | from pre_commit.output import get_hook_message | |
| 15 | 16 | from pre_commit.repository import repositories | |
| 16 | 17 | from pre_commit.staged_files_only import staged_files_only | |
@@ -214,16 +215,16 @@ def _has_unmerged_paths(): | |||
| 214 | 215 | return bool(stdout.strip()) | |
| 215 | 216 | ||
| 216 | 217 | ||
| 217 | - def _has_unstaged_config(runner): | ||
| 218 | + def _has_unstaged_config(config_file): | ||
| 218 | 219 | retcode, _, _ = cmd_output( | |
| 219 | - 'git', 'diff', '--no-ext-diff', '--exit-code', runner.config_file_path, | ||
| 220 | + 'git', 'diff', '--no-ext-diff', '--exit-code', config_file, | ||
| 220 | 221 | retcode=None, | |
| 221 | 222 | ) | |
| 222 | 223 | # be explicit, other git errors don't mean it has an unstaged config. | |
| 223 | 224 | return retcode == 1 | |
| 224 | 225 | ||
| 225 | 226 | ||
| 226 | - def run(runner, store, args, environ=os.environ): | ||
| 227 | + def run(config_file, store, args, environ=os.environ): | ||
| 227 | 228 | no_stash = args.all_files or bool(args.files) | |
| 228 | 229 | ||
| 229 | 230 | # Check if we have unresolved merge conflict files and fail fast. | |
@@ -233,10 +234,10 @@ def run(runner, store, args, environ=os.environ): | |||
| 233 | 234 | if bool(args.source) != bool(args.origin): | |
| 234 | 235 | logger.error('Specify both --origin and --source.') | |
| 235 | 236 | return 1 | |
| 236 | - if _has_unstaged_config(runner) and not no_stash: | ||
| 237 | + if _has_unstaged_config(config_file) and not no_stash: | ||
| 237 | 238 | logger.error( | |
| 238 | 239 | 'Your pre-commit configuration is unstaged.\n' | |
| 239 | - '`git add {}` to fix this.'.format(runner.config_file), | ||
| 240 | + '`git add {}` to fix this.'.format(config_file), | ||
| 240 | 241 | ) | |
| 241 | 242 | return 1 | |
| 242 | 243 | ||
@@ -252,7 +253,8 @@ def run(runner, store, args, environ=os.environ): | |||
| 252 | 253 | ||
| 253 | 254 | with ctx: | |
| 254 | 255 | repo_hooks = [] | |
| 255 | - for repo in repositories(runner.config, store): | ||
| 256 | + config = load_config(config_file) | ||
| 257 | + for repo in repositories(config, store): | ||
| 256 | 258 | for _, hook in repo.hooks: | |
| 257 | 259 | if ( | |
| 258 | 260 | (not args.hook or hook['id'] == args.hook) and | |
@@ -267,4 +269,4 @@ def run(runner, store, args, environ=os.environ): | |||
| 267 | 269 | for repo in {repo for repo, _ in repo_hooks}: | |
| 268 | 270 | repo.require_installed() | |
| 269 | 271 | ||
| 270 | - return _run_hooks(runner.config, repo_hooks, args, environ) | ||
| 272 | + return _run_hooks(config, repo_hooks, args, environ) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,7 +11,6 @@ | |||
| 11 | 11 | from pre_commit import output | |
| 12 | 12 | from pre_commit.clientlib import load_manifest | |
| 13 | 13 | from pre_commit.commands.run import run | |
| 14 | - from pre_commit.runner import Runner | ||
| 15 | 14 | from pre_commit.store import Store | |
| 16 | 15 | from pre_commit.util import tmpdir | |
| 17 | 16 | ||
@@ -43,4 +42,4 @@ def try_repo(args): | |||
| 43 | 42 | output.write(config_s) | |
| 44 | 43 | output.write_line('=' * 79) | |
| 45 | 44 | ||
| 46 | - return run(Runner('.', config_filename), store, args) | ||
| 45 | + return run(config_filename, store, args) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,7 +30,7 @@ def get_root(): | |||
| 30 | 30 | ) | |
| 31 | 31 | ||
| 32 | 32 | ||
| 33 | - def get_git_dir(git_root): | ||
| 33 | + def get_git_dir(git_root='.'): | ||
| 34 | 34 | opts = ('--git-common-dir', '--git-dir') | |
| 35 | 35 | _, out, _ = cmd_output('git', 'rev-parse', *opts, cwd=git_root) | |
| 36 | 36 | for line, opt in zip(out.splitlines(), opts): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -88,12 +88,12 @@ def _install_rbenv(prefix, version='default'): # pragma: windows no cover | |||
| 88 | 88 | activate_file.write('export RBENV_VERSION="{}"\n'.format(version)) | |
| 89 | 89 | ||
| 90 | 90 | ||
| 91 | - def _install_ruby(runner, version): # pragma: windows no cover | ||
| 91 | + def _install_ruby(prefix, version): # pragma: windows no cover | ||
| 92 | 92 | try: | |
| 93 | - helpers.run_setup_cmd(runner, ('rbenv', 'download', version)) | ||
| 93 | + helpers.run_setup_cmd(prefix, ('rbenv', 'download', version)) | ||
| 94 | 94 | except CalledProcessError: # pragma: no cover (usually find with download) | |
| 95 | 95 | # Failed to download from mirror for some reason, build it instead | |
| 96 | - helpers.run_setup_cmd(runner, ('rbenv', 'install', version)) | ||
| 96 | + helpers.run_setup_cmd(prefix, ('rbenv', 'install', version)) | ||
| 97 | 97 | ||
| 98 | 98 | ||
| 99 | 99 | def install_environment( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,6 @@ | |||
| 20 | 20 | from pre_commit.commands.try_repo import try_repo | |
| 21 | 21 | from pre_commit.error_handler import error_handler | |
| 22 | 22 | from pre_commit.logging_handler import add_logging_handler | |
| 23 | - from pre_commit.runner import Runner | ||
| 24 | 23 | from pre_commit.store import Store | |
| 25 | 24 | ||
| 26 | 25 | ||
@@ -89,6 +88,20 @@ def _add_run_options(parser): | |||
| 89 | 88 | ) | |
| 90 | 89 | ||
| 91 | 90 | ||
| 91 | + def _adjust_args_and_chdir(args): | ||
| 92 | + # `--config` was specified relative to the non-root working directory | ||
| 93 | + if os.path.exists(args.config): | ||
| 94 | + args.config = os.path.abspath(args.config) | ||
| 95 | + if args.command in {'run', 'try-repo'}: | ||
| 96 | + args.files = [os.path.abspath(filename) for filename in args.files] | ||
| 97 | + | ||
| 98 | + os.chdir(git.get_root()) | ||
| 99 | + | ||
| 100 | + args.config = os.path.relpath(args.config) | ||
| 101 | + if args.command in {'run', 'try-repo'}: | ||
| 102 | + args.files = [os.path.relpath(filename) for filename in args.files] | ||
| 103 | + | ||
| 104 | + | ||
| 92 | 105 | def main(argv=None): | |
| 93 | 106 | argv = argv if argv is not None else sys.argv[1:] | |
| 94 | 107 | argv = [five.to_text(arg) for arg in argv] | |
@@ -222,43 +235,40 @@ def main(argv=None): | |||
| 222 | 235 | parser.parse_args([args.help_cmd, '--help']) | |
| 223 | 236 | elif args.command == 'help': | |
| 224 | 237 | parser.parse_args(['--help']) | |
| 225 | - elif args.command in {'run', 'try-repo'}: | ||
| 226 | - args.files = [ | ||
| 227 | - os.path.relpath(os.path.abspath(filename), git.get_root()) | ||
| 228 | - for filename in args.files | ||
| 229 | - ] | ||
| 230 | 238 | ||
| 231 | 239 | with error_handler(): | |
| 232 | 240 | add_logging_handler(args.color) | |
| 233 | - runner = Runner.create(args.config) | ||
| 241 | + | ||
| 242 | + _adjust_args_and_chdir(args) | ||
| 243 | + | ||
| 234 | 244 | store = Store() | |
| 235 | 245 | git.check_for_cygwin_mismatch() | |
| 236 | 246 | ||
| 237 | 247 | if args.command == 'install': | |
| 238 | 248 | return install( | |
| 239 | - runner, store, | ||
| 249 | + args.config, store, | ||
| 240 | 250 | overwrite=args.overwrite, hooks=args.install_hooks, | |
| 241 | 251 | hook_type=args.hook_type, | |
| 242 | 252 | skip_on_missing_conf=args.allow_missing_config, | |
| 243 | 253 | ) | |
| 244 | 254 | elif args.command == 'install-hooks': | |
| 245 | - return install_hooks(runner, store) | ||
| 255 | + return install_hooks(args.config, store) | ||
| 246 | 256 | elif args.command == 'uninstall': | |
| 247 | - return uninstall(runner, hook_type=args.hook_type) | ||
| 257 | + return uninstall(hook_type=args.hook_type) | ||
| 248 | 258 | elif args.command == 'clean': | |
| 249 | 259 | return clean(store) | |
| 250 | 260 | elif args.command == 'autoupdate': | |
| 251 | 261 | if args.tags_only: | |
| 252 | 262 | logger.warning('--tags-only is the default') | |
| 253 | 263 | return autoupdate( | |
| 254 | - runner, store, | ||
| 264 | + args.config, store, | ||
| 255 | 265 | tags_only=not args.bleeding_edge, | |
| 256 | 266 | repos=args.repos, | |
| 257 | 267 | ) | |
| 258 | 268 | elif args.command == 'migrate-config': | |
| 259 | - return migrate_config(runner) | ||
| 269 | + return migrate_config(args.config) | ||
| 260 | 270 | elif args.command == 'run': | |
| 261 | - return run(runner, store, args) | ||
| 271 | + return run(args.config, store, args) | ||
| 262 | 272 | elif args.command == 'sample-config': | |
| 263 | 273 | return sample_config() | |
| 264 | 274 | elif args.command == 'try-repo': | |
| Back | FazBrowse Home | New Git URL |
0 commit comments