Teach pre-commit try-repo to clone uncommitted changes

This commit is contained in:
Anthony Sottile
2019-01-01 13:12:02 -08:00
parent e04505a669
commit bdc58cc33f
10 changed files with 148 additions and 62 deletions

View File

@@ -199,11 +199,7 @@ def _run_hooks(config, hooks, args, environ):
retval |= _run_single_hook(filenames, hook, args, skips, cols)
if retval and config['fail_fast']:
break
if (
retval and
args.show_diff_on_failure and
subprocess.call(('git', 'diff', '--quiet', '--no-ext-diff')) != 0
):
if retval and args.show_diff_on_failure and git.has_diff():
output.write_line('All changes made by hooks:')
subprocess.call(('git', '--no-pager', 'diff', '--no-ext-diff'))
return retval

View File

@@ -2,6 +2,7 @@ from __future__ import absolute_import
from __future__ import unicode_literals
import collections
import logging
import os.path
from aspy.yaml import ordered_dump
@@ -12,23 +13,50 @@ from pre_commit import output
from pre_commit.clientlib import load_manifest
from pre_commit.commands.run import run
from pre_commit.store import Store
from pre_commit.util import cmd_output
from pre_commit.util import tmpdir
logger = logging.getLogger(__name__)
def _repo_ref(tmpdir, repo, ref):
# if `ref` is explicitly passed, use it
if ref:
return repo, ref
ref = git.head_rev(repo)
# if it exists on disk, we'll try and clone it with the local changes
if os.path.exists(repo) and git.has_diff('HEAD', repo=repo):
logger.warning('Creating temporary repo with uncommitted changes...')
shadow = os.path.join(tmpdir, 'shadow-repo')
cmd_output('git', 'clone', repo, shadow)
cmd_output('git', 'checkout', ref, '-b', '_pc_tmp', cwd=shadow)
idx = git.git_path('index', repo=shadow)
objs = git.git_path('objects', repo=shadow)
env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs)
cmd_output('git', 'add', '-u', cwd=repo, env=env)
git.commit(repo=shadow)
return shadow, git.head_rev(shadow)
else:
return repo, ref
def try_repo(args):
ref = args.ref or git.head_rev(args.repo)
with tmpdir() as tempdir:
repo, ref = _repo_ref(tempdir, args.repo, args.ref)
store = Store(tempdir)
if args.hook:
hooks = [{'id': args.hook}]
else:
repo_path = store.clone(args.repo, ref)
repo_path = store.clone(repo, ref)
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
manifest = sorted(manifest, key=lambda hook: hook['id'])
hooks = [{'id': hook['id']} for hook in manifest]
items = (('repo', args.repo), ('rev', ref), ('hooks', hooks))
items = (('repo', repo), ('rev', ref), ('hooks', hooks))
config = {'repos': [collections.OrderedDict(items)]}
config_s = ordered_dump(config, **C.YAML_DUMP_KWARGS)

View File

@@ -4,12 +4,10 @@ import logging
import os.path
import sys
from pre_commit.error_handler import FatalError
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
logger = logging.getLogger('pre_commit')
logger = logging.getLogger(__name__)
def zsplit(s):
@@ -20,14 +18,23 @@ def zsplit(s):
return []
def no_git_env():
# Too many bugs dealing with environment variables and GIT:
# https://github.com/pre-commit/pre-commit/issues/300
# In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running
# pre-commit hooks
# In git 1.9.1 (maybe others), git exports GIT_DIR and GIT_INDEX_FILE
# while running pre-commit hooks in submodules.
# GIT_DIR: Causes git clone to clone wrong thing
# GIT_INDEX_FILE: Causes 'error invalid object ...' during commit
return {
k: v for k, v in os.environ.items()
if not k.startswith('GIT_') or k in {'GIT_SSH'}
}
def get_root():
try:
return cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip()
except CalledProcessError:
raise FatalError(
'git failed. Is it installed, and are you in a Git repository '
'directory?',
)
return cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip()
def get_git_dir(git_root='.'):
@@ -106,6 +113,27 @@ def head_rev(remote):
return out.split()[0]
def has_diff(*args, **kwargs):
repo = kwargs.pop('repo', '.')
assert not kwargs, kwargs
cmd = ('git', 'diff', '--quiet', '--no-ext-diff') + args
return cmd_output(*cmd, cwd=repo, retcode=None)[0]
def commit(repo='.'):
env = no_git_env()
name, email = 'pre-commit', 'asottile+pre-commit@umich.edu'
env['GIT_AUTHOR_NAME'] = env['GIT_COMMITTER_NAME'] = name
env['GIT_AUTHOR_EMAIL'] = env['GIT_COMMITTER_EMAIL'] = email
cmd = ('git', 'commit', '--no-edit', '--no-gpg-sign', '-n', '-minit')
cmd_output(*cmd, cwd=repo, env=env)
def git_path(name, repo='.'):
_, out, _ = cmd_output('git', 'rev-parse', '--git-path', name, cwd=repo)
return os.path.join(repo, out.strip())
def check_for_cygwin_mismatch():
"""See https://github.com/pre-commit/pre-commit/issues/354"""
if sys.platform in ('cygwin', 'win32'): # pragma: no cover (windows)

View File

@@ -19,8 +19,10 @@ from pre_commit.commands.run import run
from pre_commit.commands.sample_config import sample_config
from pre_commit.commands.try_repo import try_repo
from pre_commit.error_handler import error_handler
from pre_commit.error_handler import FatalError
from pre_commit.logging_handler import add_logging_handler
from pre_commit.store import Store
from pre_commit.util import CalledProcessError
logger = logging.getLogger('pre_commit')
@@ -97,7 +99,13 @@ def _adjust_args_and_chdir(args):
if args.command == 'try-repo' and os.path.exists(args.repo):
args.repo = os.path.abspath(args.repo)
os.chdir(git.get_root())
try:
os.chdir(git.get_root())
except CalledProcessError:
raise FatalError(
'git failed. Is it installed, and are you in a Git repository '
'directory?',
)
args.config = os.path.relpath(args.config)
if args.command in {'run', 'try-repo'}:

View File

@@ -9,9 +9,9 @@ import tempfile
import pre_commit.constants as C
from pre_commit import file_lock
from pre_commit import git
from pre_commit.util import clean_path_on_failure
from pre_commit.util import cmd_output
from pre_commit.util import no_git_env
from pre_commit.util import resource_text
@@ -135,7 +135,7 @@ class Store(object):
def clone(self, repo, ref, deps=()):
"""Clone the given url and checkout the specific ref."""
def clone_strategy(directory):
env = no_git_env()
env = git.no_git_env()
cmd = ('git', 'clone', '--no-checkout', repo, directory)
cmd_output(*cmd, env=env)
@@ -160,10 +160,7 @@ class Store(object):
with io.open(os.path.join(directory, resource), 'w') as f:
f.write(contents)
env = no_git_env()
name, email = 'pre-commit', 'asottile+pre-commit@umich.edu'
env['GIT_AUTHOR_NAME'] = env['GIT_COMMITTER_NAME'] = name
env['GIT_AUTHOR_EMAIL'] = env['GIT_COMMITTER_EMAIL'] = email
env = git.no_git_env()
# initialize the git repository so it looks more like cloned repos
def _git_cmd(*args):
@@ -172,7 +169,7 @@ class Store(object):
_git_cmd('init', '.')
_git_cmd('config', 'remote.origin.url', '<<unknown>>')
_git_cmd('add', '.')
_git_cmd('commit', '--no-edit', '--no-gpg-sign', '-n', '-minit')
git.commit(repo=directory)
return self._new_repo(
'local', C.LOCAL_REPO_VERSION, deps, make_local_strategy,

View File

@@ -64,21 +64,6 @@ def noop_context():
yield
def no_git_env():
# Too many bugs dealing with environment variables and GIT:
# https://github.com/pre-commit/pre-commit/issues/300
# In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running
# pre-commit hooks
# In git 1.9.1 (maybe others), git exports GIT_DIR and GIT_INDEX_FILE
# while running pre-commit hooks in submodules.
# GIT_DIR: Causes git clone to clone wrong thing
# GIT_INDEX_FILE: Causes 'error invalid object ...' during commit
return {
k: v for k, v in os.environ.items()
if not k.startswith('GIT_') or k in {'GIT_SSH'}
}
@contextlib.contextmanager
def tmpdir():
"""Contextmanager to create a temporary directory. It will be cleaned up