def test_install_pre_commit(tempdir_factory): path = git_dir(tempdir_factory) runner = Runner(path, C.CONFIG_FILE) ret = install(runner) assert ret == 0 assert os.path.exists(runner.pre_commit_path) pre_commit_contents = io.open(runner.pre_commit_path).read() pre_commit_script = resource_filename('hook-tmpl') expected_contents = io.open(pre_commit_script).read().format( sys_executable=pipes.quote(sys.executable), hook_type='pre-commit', hook_specific='', config_file=runner.config_file, skip_on_missing_conf='false', ) assert pre_commit_contents == expected_contents assert os.access(runner.pre_commit_path, os.X_OK) ret = install(runner, hook_type='pre-push') assert ret == 0 assert os.path.exists(runner.pre_push_path) pre_push_contents = io.open(runner.pre_push_path).read() pre_push_tmpl = resource_filename('pre-push-tmpl') pre_push_template_contents = io.open(pre_push_tmpl).read() expected_contents = io.open(pre_commit_script).read().format( sys_executable=pipes.quote(sys.executable), hook_type='pre-push', hook_specific=pre_push_template_contents, config_file=runner.config_file, skip_on_missing_conf='false', ) assert pre_push_contents == expected_contents
def test_install_pre_commit(tmpdir_factory): path = git_dir(tmpdir_factory) runner = Runner(path) ret = install(runner) assert ret == 0 assert os.path.exists(runner.pre_commit_path) pre_commit_contents = io.open(runner.pre_commit_path).read() pre_commit_script = resource_filename('hook-tmpl') expected_contents = io.open(pre_commit_script).read().format( sys_executable=sys.executable, hook_type='pre-commit', pre_push='' ) assert pre_commit_contents == expected_contents assert os.access(runner.pre_commit_path, os.X_OK) ret = install(runner, hook_type='pre-push') assert ret == 0 assert os.path.exists(runner.pre_push_path) pre_push_contents = io.open(runner.pre_push_path).read() pre_push_tmpl = resource_filename('pre-push-tmpl') pre_push_template_contents = io.open(pre_push_tmpl).read() expected_contents = io.open(pre_commit_script).read().format( sys_executable=sys.executable, hook_type='pre-push', pre_push=pre_push_template_contents, ) assert pre_push_contents == expected_contents
def _install_rbenv(repo_cmd_runner, version='default'): with tarfile_open(resource_filename('rbenv.tar.gz')) as tf: tf.extractall(repo_cmd_runner.path('.')) # Only install ruby-build if the version is specified if version != 'default': # ruby-download with tarfile_open(resource_filename('ruby-download.tar.gz')) as tf: tf.extractall(repo_cmd_runner.path('rbenv', 'plugins')) # ruby-build with tarfile_open(resource_filename('ruby-build.tar.gz')) as tf: tf.extractall(repo_cmd_runner.path('rbenv', 'plugins')) activate_path = repo_cmd_runner.path('rbenv', 'bin', 'activate') with io.open(activate_path, 'w') as activate_file: # This is similar to how you would install rbenv to your home directory # However we do a couple things to make the executables exposed and # configure it to work in our directory. # We also modify the PS1 variable for manual debugging sake. activate_file.write( '#!/usr/bin/env bash\n' "export RBENV_ROOT='{0}'\n" 'export PATH="$RBENV_ROOT/bin:$PATH"\n' 'eval "$(rbenv init -)"\n' 'export PS1="(rbenv)$PS1"\n' # This lets us install gems in an isolated and repeatable # directory "export GEM_HOME='{0}/gems'\n" 'export PATH="$GEM_HOME/bin:$PATH"\n' '\n'.format(repo_cmd_runner.path('rbenv'))) # If we aren't using the system ruby, add a version here if version != 'default': activate_file.write('export RBENV_VERSION="{0}"\n'.format(version))
def install( runner, overwrite=False, hooks=False, hook_type='pre-commit', skip_on_missing_conf=False, ): """Install the pre-commit hooks.""" hook_path = runner.get_hook_path(hook_type) legacy_path = hook_path + '.legacy' mkdirp(os.path.dirname(hook_path)) # If we have an existing hook, move it to pre-commit.legacy if os.path.lexists(hook_path) and not is_our_script(hook_path): os.rename(hook_path, legacy_path) # If we specify overwrite, we simply delete the legacy file if overwrite and os.path.exists(legacy_path): os.remove(legacy_path) elif os.path.exists(legacy_path): output.write_line( 'Running in migration mode with existing hooks at {}\n' 'Use -f to use only pre-commit.'.format( legacy_path, ), ) with io.open(hook_path, 'w') as pre_commit_file_obj: if hook_type == 'pre-push': with io.open(resource_filename('pre-push-tmpl')) as f: hook_specific_contents = f.read() elif hook_type == 'commit-msg': with io.open(resource_filename('commit-msg-tmpl')) as f: hook_specific_contents = f.read() elif hook_type == 'pre-commit': hook_specific_contents = '' else: raise AssertionError('Unknown hook type: {}'.format(hook_type)) skip_on_missing_conf = 'true' if skip_on_missing_conf else 'false' contents = io.open(resource_filename('hook-tmpl')).read().format( sys_executable=pipes.quote(sys.executable), hook_type=hook_type, hook_specific=hook_specific_contents, config_file=runner.config_file, skip_on_missing_conf=skip_on_missing_conf, ) pre_commit_file_obj.write(contents) make_executable(hook_path) output.write_line('pre-commit installed at {}'.format(hook_path)) # If they requested we install all of the hooks, do so. if hooks: install_hooks(runner) return 0
def install( runner, overwrite=False, hooks=False, hook_type='pre-commit', skip_on_missing_conf=False, ): """Install the pre-commit hooks.""" hook_path = runner.get_hook_path(hook_type) legacy_path = hook_path + '.legacy' mkdirp(os.path.dirname(hook_path)) # If we have an existing hook, move it to pre-commit.legacy if os.path.lexists(hook_path) and not is_our_script(hook_path): os.rename(hook_path, legacy_path) # If we specify overwrite, we simply delete the legacy file if overwrite and os.path.exists(legacy_path): os.remove(legacy_path) elif os.path.exists(legacy_path): output.write_line( 'Running in migration mode with existing hooks at {}\n' 'Use -f to use only pre-commit.'.format(legacy_path, ), ) with io.open(hook_path, 'w') as pre_commit_file_obj: if hook_type == 'pre-push': with io.open(resource_filename('pre-push-tmpl')) as f: hook_specific_contents = f.read() elif hook_type == 'commit-msg': with io.open(resource_filename('commit-msg-tmpl')) as f: hook_specific_contents = f.read() elif hook_type == 'pre-commit': hook_specific_contents = '' else: raise AssertionError('Unknown hook type: {}'.format(hook_type)) skip_on_missing_conf = 'true' if skip_on_missing_conf else 'false' contents = io.open(resource_filename('hook-tmpl')).read().format( sys_executable=sys.executable, hook_type=hook_type, hook_specific=hook_specific_contents, skip_on_missing_conf=skip_on_missing_conf, ) pre_commit_file_obj.write(contents) make_executable(hook_path) output.write_line('pre-commit installed at {}'.format(hook_path)) # If they requested we install all of the hooks, do so. if hooks: install_hooks(runner) return 0
def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'): """Install the pre-commit hooks.""" hook_path = runner.get_hook_path(hook_type) legacy_path = hook_path + '.legacy' if not os.path.exists(os.path.dirname(hook_path)): os.makedirs(os.path.dirname(hook_path)) # If we have an existing hook, move it to pre-commit.legacy if ( os.path.lexists(hook_path) and not is_our_pre_commit(hook_path) and not is_previous_pre_commit(hook_path) ): os.rename(hook_path, legacy_path) # If we specify overwrite, we simply delete the legacy file if overwrite and os.path.exists(legacy_path): os.remove(legacy_path) elif os.path.exists(legacy_path): print( 'Running in migration mode with existing hooks at {0}\n' 'Use -f to use only pre-commit.'.format( legacy_path, ) ) with io.open(hook_path, 'w') as pre_commit_file_obj: if hook_type == 'pre-push': with io.open(resource_filename('pre-push-tmpl')) as fp: pre_push_contents = fp.read() else: pre_push_contents = '' contents = io.open(resource_filename('hook-tmpl')).read().format( sys_executable=sys.executable, hook_type=hook_type, pre_push=pre_push_contents, ) pre_commit_file_obj.write(contents) make_executable(hook_path) print('pre-commit installed at {0}'.format(hook_path)) # If they requested we install all of the hooks, do so. if hooks: # Set up our logging handler logger.addHandler(LoggingHandler(False)) logger.setLevel(logging.INFO) for repository in runner.repositories: repository.require_installed() return 0
def install(runner, overwrite=False, hooks=False): """Install the pre-commit hooks.""" pre_commit_file = resource_filename('pre-commit-hook') # If we have an existing hook, move it to pre-commit.legacy if (os.path.exists(runner.pre_commit_path) and not is_our_pre_commit(runner.pre_commit_path) and not is_previous_pre_commit(runner.pre_commit_path)): os.rename(runner.pre_commit_path, runner.pre_commit_legacy_path) # If we specify overwrite, we simply delete the legacy file if overwrite and os.path.exists(runner.pre_commit_legacy_path): os.remove(runner.pre_commit_legacy_path) elif os.path.exists(runner.pre_commit_legacy_path): print('Running in migration mode with existing hooks at {0}\n' 'Use -f to use only pre-commit.'.format( runner.pre_commit_legacy_path, )) with io.open(runner.pre_commit_path, 'w') as pre_commit_file_obj: contents = io.open(pre_commit_file).read().format( sys_executable=sys.executable, ) pre_commit_file_obj.write(contents) make_executable(runner.pre_commit_path) print('pre-commit installed at {0}'.format(runner.pre_commit_path)) # If they requested we install all of the hooks, do so. if hooks: # Set up our logging handler logger.addHandler(LoggingHandler(False)) logger.setLevel(logging.INFO) for repository in runner.repositories: repository.require_installed() return 0
def install( runner, store, overwrite=False, hooks=False, hook_type='pre-commit', skip_on_missing_conf=False, ): """Install the pre-commit hooks.""" if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip(): logger.error( 'Cowardly refusing to install hooks with `core.hooksPath` set.\n' 'hint: `git config --unset-all core.hooksPath`', ) return 1 hook_path = runner.get_hook_path(hook_type) legacy_path = hook_path + '.legacy' mkdirp(os.path.dirname(hook_path)) # If we have an existing hook, move it to pre-commit.legacy if os.path.lexists(hook_path) and not is_our_script(hook_path): os.rename(hook_path, legacy_path) # If we specify overwrite, we simply delete the legacy file if overwrite and os.path.exists(legacy_path): os.remove(legacy_path) elif os.path.exists(legacy_path): output.write_line( 'Running in migration mode with existing hooks at {}\n' 'Use -f to use only pre-commit.'.format(legacy_path), ) params = { 'CONFIG': runner.config_file, 'HOOK_TYPE': hook_type, 'INSTALL_PYTHON': sys.executable, 'SKIP_ON_MISSING_CONFIG': skip_on_missing_conf, } with io.open(hook_path, 'w') as hook_file: with io.open(resource_filename('hook-tmpl')) as f: contents = f.read() before, rest = contents.split(TEMPLATE_START) to_template, after = rest.split(TEMPLATE_END) hook_file.write(before + TEMPLATE_START) for line in to_template.splitlines(): var = line.split()[0] hook_file.write('{} = {!r}\n'.format(var, params[var])) hook_file.write(TEMPLATE_END + after) make_executable(hook_path) output.write_line('pre-commit installed at {}'.format(hook_path)) # If they requested we install all of the hooks, do so. if hooks: install_hooks(runner, store) return 0
def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'): """Install the pre-commit hooks.""" hook_path = runner.get_hook_path(hook_type) legacy_path = hook_path + '.legacy' mkdirp(os.path.dirname(hook_path)) # If we have an existing hook, move it to pre-commit.legacy if (os.path.lexists(hook_path) and not is_our_pre_commit(hook_path) and not is_previous_pre_commit(hook_path)): os.rename(hook_path, legacy_path) # If we specify overwrite, we simply delete the legacy file if overwrite and os.path.exists(legacy_path): os.remove(legacy_path) elif os.path.exists(legacy_path): print('Running in migration mode with existing hooks at {0}\n' 'Use -f to use only pre-commit.'.format(legacy_path, )) with io.open(hook_path, 'w') as pre_commit_file_obj: if hook_type == 'pre-push': with io.open(resource_filename('pre-push-tmpl')) as fp: pre_push_contents = fp.read() else: pre_push_contents = '' contents = io.open(resource_filename('hook-tmpl')).read().format( sys_executable=sys.executable, hook_type=hook_type, pre_push=pre_push_contents, ) pre_commit_file_obj.write(contents) make_executable(hook_path) print('pre-commit installed at {0}'.format(hook_path)) # If they requested we install all of the hooks, do so. if hooks: # Set up our logging handler logger.addHandler(LoggingHandler(False)) logger.setLevel(logging.INFO) for repository in runner.repositories: repository.require_installed() return 0
def main(argv=None): parser = argparse.ArgumentParser() parser.add_argument('--dest', default=resource_filename()) args = parser.parse_args(argv) for archive_name, repo, ref in REPOS: output.write_line('Making {}.tar.gz for {}@{}'.format( archive_name, repo, ref, )) make_archive(archive_name, repo, ref, args.dest)
def _install_rbenv( repo_cmd_runner, version='default', ): # pragma: windows no cover directory = helpers.environment_dir(ENVIRONMENT_DIR, version) with tarfile.open(resource_filename('rbenv.tar.gz')) as tf: tf.extractall(repo_cmd_runner.path('.')) shutil.move( repo_cmd_runner.path('rbenv'), repo_cmd_runner.path(directory), ) # Only install ruby-build if the version is specified if version != 'default': # ruby-download with tarfile.open(resource_filename('ruby-download.tar.gz')) as tf: tf.extractall(repo_cmd_runner.path(directory, 'plugins')) # ruby-build with tarfile.open(resource_filename('ruby-build.tar.gz')) as tf: tf.extractall(repo_cmd_runner.path(directory, 'plugins')) activate_path = repo_cmd_runner.path(directory, 'bin', 'activate') with io.open(activate_path, 'w') as activate_file: # This is similar to how you would install rbenv to your home directory # However we do a couple things to make the executables exposed and # configure it to work in our directory. # We also modify the PS1 variable for manual debugging sake. activate_file.write( '#!/usr/bin/env bash\n' "export RBENV_ROOT='{directory}'\n" 'export PATH="$RBENV_ROOT/bin:$PATH"\n' 'eval "$(rbenv init -)"\n' 'export PS1="(rbenv)$PS1"\n' # This lets us install gems in an isolated and repeatable # directory "export GEM_HOME='{directory}/gems'\n" 'export PATH="$GEM_HOME/bin:$PATH"\n' '\n'.format(directory=repo_cmd_runner.path(directory)) ) # If we aren't using the system ruby, add a version here if version != 'default': activate_file.write('export RBENV_VERSION="{}"\n'.format(version))
def test_install_pre_commit(tmpdir_factory): path = git_dir(tmpdir_factory) runner = Runner(path) ret = install(runner) assert ret == 0 assert os.path.exists(runner.pre_commit_path) pre_commit_contents = io.open(runner.pre_commit_path).read() pre_commit_script = resource_filename('pre-commit-hook') expected_contents = io.open(pre_commit_script).read() assert pre_commit_contents == expected_contents stat_result = os.stat(runner.pre_commit_path) assert stat_result.st_mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
def test_install_pre_commit(tmpdir_factory): path = git_dir(tmpdir_factory) runner = Runner(path) ret = install(runner) assert ret == 0 assert os.path.exists(runner.pre_commit_path) pre_commit_contents = io.open(runner.pre_commit_path).read() pre_commit_script = resource_filename('pre-commit-hook') expected_contents = io.open(pre_commit_script).read().format( sys_executable=sys.executable, ) assert pre_commit_contents == expected_contents stat_result = os.stat(runner.pre_commit_path) assert stat_result.st_mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
def _install_rbenv(repo_cmd_runner, version="default"): with tarfile_open(resource_filename("rbenv.tar.gz")) as tf: tf.extractall(repo_cmd_runner.path(".")) # Only install ruby-build if the version is specified if version != "default": # ruby-download with tarfile_open(resource_filename("ruby-download.tar.gz")) as tf: tf.extractall(repo_cmd_runner.path("rbenv", "plugins")) # ruby-build with tarfile_open(resource_filename("ruby-build.tar.gz")) as tf: tf.extractall(repo_cmd_runner.path("rbenv", "plugins")) activate_path = repo_cmd_runner.path("rbenv", "bin", "activate") with io.open(activate_path, "w") as activate_file: # This is similar to how you would install rbenv to your home directory # However we do a couple things to make the executables exposed and # configure it to work in our directory. # We also modify the PS1 variable for manual debugging sake. activate_file.write( "#!/usr/bin/env bash\n" "export RBENV_ROOT='{0}'\n" 'export PATH="$RBENV_ROOT/bin:$PATH"\n' 'eval "$(rbenv init -)"\n' 'export PS1="(rbenv)$PS1"\n' # This lets us install gems in an isolated and repeatable # directory "export GEM_HOME='{0}/gems'\n" 'export PATH="$GEM_HOME/bin:$PATH"\n' "\n".format(repo_cmd_runner.path("rbenv")) ) # If we aren't using the system ruby, add a version here if version != "default": activate_file.write('export RBENV_VERSION="{0}"\n'.format(version))
def make_local_strategy(directory): copy_tree_to_path(resource_filename('empty_template'), directory) env = no_git_env() name, email = 'pre-commit', '*****@*****.**' env['GIT_AUTHOR_NAME'] = env['GIT_COMMITTER_NAME'] = name env['GIT_AUTHOR_EMAIL'] = env['GIT_COMMITTER_EMAIL'] = email # initialize the git repository so it looks more like cloned repos def _git_cmd(*args): cmd_output('git', '-C', directory, *args, env=env) _git_cmd('init', '.') _git_cmd('config', 'remote.origin.url', '<<unknown>>') _git_cmd('add', '.') _git_cmd('commit', '--no-edit', '--no-gpg-sign', '-n', '-minit')
def test_replace_old_commit_script(tmpdir_factory): path = make_consuming_repo(tmpdir_factory, "script_hooks_repo") with cwd(path): runner = Runner(path) # Install a script that looks like our old script pre_commit_contents = io.open(resource_filename("hook-tmpl")).read() new_contents = pre_commit_contents.replace(IDENTIFYING_HASH, PREVIOUS_IDENTIFYING_HASHES[-1]) with io.open(runner.pre_commit_path, "w") as pre_commit_file: pre_commit_file.write(new_contents) make_executable(runner.pre_commit_path) # Install normally assert install(runner) == 0 ret, output = _get_commit_output(tmpdir_factory) assert ret == 0 assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_replace_old_commit_script(tmpdir_factory): path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') with cwd(path): runner = Runner(path) # Install a script that looks like our old script pre_commit_contents = io.open(resource_filename('hook-tmpl'), ).read() new_contents = pre_commit_contents.replace( IDENTIFYING_HASH, PREVIOUS_IDENTIFYING_HASHES[-1], ) with io.open(runner.pre_commit_path, 'w') as pre_commit_file: pre_commit_file.write(new_contents) make_executable(runner.pre_commit_path) # Install normally assert install(runner) == 0 ret, output = _get_commit_output(tmpdir_factory) assert ret == 0 assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_replace_old_commit_script(tempdir_factory, store): path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(path): runner = Runner(path, C.CONFIG_FILE) # Install a script that looks like our old script pre_commit_contents = io.open(resource_filename('hook-tmpl')).read() new_contents = pre_commit_contents.replace( CURRENT_HASH, PRIOR_HASHES[-1], ) mkdirp(os.path.join(path, '.git/hooks')) with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f: f.write(new_contents) make_executable(f.name) # Install normally assert install(runner, store) == 0 ret, output = _get_commit_output(tempdir_factory) assert ret == 0 assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_replace_old_commit_script(tempdir_factory): path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(path): runner = Runner(path, C.CONFIG_FILE) # Install a script that looks like our old script pre_commit_contents = io.open( resource_filename('hook-tmpl'), ).read() new_contents = pre_commit_contents.replace( CURRENT_HASH, PRIOR_HASHES[-1], ) mkdirp(os.path.dirname(runner.pre_commit_path)) with io.open(runner.pre_commit_path, 'w') as pre_commit_file: pre_commit_file.write(new_contents) make_executable(runner.pre_commit_path) # Install normally assert install(runner) == 0 ret, output = _get_commit_output(tempdir_factory) assert ret == 0 assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_is_script(): assert is_our_script(resource_filename('hook-tmpl'))
def test_is_also_not_previous_pre_commit(): assert not is_previous_pre_commit(resource_filename('pre-commit-hook'))
def test_is_our_pre_commit(): assert is_our_pre_commit(resource_filename('hook-tmpl'))
def make_local_strategy(directory): copy_tree_to_path(resource_filename('empty_template'), directory)
def test_is_also_not_previous_pre_commit(): assert not is_previous_pre_commit(resource_filename('hook-tmpl'))
def test_is_our_pre_commit(): assert is_our_pre_commit(resource_filename('pre-commit-hook'))
def test_pre_template(): runner = Runner('foo/bar') assert runner.pre_template == resource_filename('hook-tmpl')
def test_pre_push_template(): runner = Runner('foo/bar') assert runner.pre_push_template == resource_filename('pre-push-tmpl')