Support pre-commit from inside submodules

This commit is contained in:
Anthony Sottile
2015-12-18 14:21:30 -08:00
parent d6cf62532d
commit c3c98afe4f
7 changed files with 92 additions and 26 deletions

View File

@@ -24,10 +24,18 @@ def get_root():
)
def get_git_dir(git_root):
return os.path.normpath(os.path.join(
git_root,
cmd_output('git', 'rev-parse', '--git-dir', cwd=git_root)[1].strip(),
))
def is_in_merge_conflict():
git_dir = get_git_dir('.')
return (
os.path.exists(os.path.join('.git', 'MERGE_MSG')) and
os.path.exists(os.path.join('.git', 'MERGE_HEAD'))
os.path.exists(os.path.join(git_dir, 'MERGE_MSG')) and
os.path.exists(os.path.join(git_dir, 'MERGE_HEAD'))
)
@@ -46,7 +54,7 @@ def get_conflicted_files():
logger.info('Checking merge-conflict files only.')
# Need to get the conflicted files from the MERGE_MSG because they could
# have resolved the conflict by choosing one side or the other
merge_msg = open(os.path.join('.git', 'MERGE_MSG')).read()
merge_msg = open(os.path.join(get_git_dir('.'), 'MERGE_MSG')).read()
merge_conflict_filenames = parse_merge_msg_for_conflicts(merge_msg)
# This will get the rest of the changes made after the merge.

View File

@@ -25,6 +25,13 @@ os.environ.pop('__PYVENV_LAUNCHER__', None)
# https://github.com/pre-commit/pre-commit/issues/300
# In git 2.6.3 (maybe others), git exports this while running pre-commit hooks
os.environ.pop('GIT_WORK_TREE', None)
# In git 1.9.1 (maybe others), git exports these while running pre-commit hooks
# in submodules. In the general case this causes problems.
# These are covered by test_install_in_submodule_and_run
# Causes git clone to clone wrong thing
os.environ.pop('GIT_DIR', None)
# Causes 'error invalid object ...' during commit
os.environ.pop('GIT_INDEX_FILE', None)
def main(argv=None):

View File

@@ -30,6 +30,10 @@ class Runner(object):
os.chdir(root)
return cls(root)
@cached_property
def git_dir(self):
return git.get_git_dir(self.git_root)
@cached_property
def config_file_path(self):
return os.path.join(self.git_root, C.CONFIG_FILE)
@@ -44,7 +48,7 @@ class Runner(object):
return repositories
def get_hook_path(self, hook_type):
return os.path.join(self.git_root, '.git', 'hooks', hook_type)
return os.path.join(self.git_dir, 'hooks', hook_type)
@cached_property
def pre_commit_path(self):