mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Implement merge-files only using @bukzor's method.
This commit is contained in:
parent
94d626691f
commit
5810ee4315
5 changed files with 115 additions and 23 deletions
|
|
@ -147,6 +147,8 @@ def clean(runner):
|
|||
def _run_single_hook(runner, repository, hook_id, args, write):
|
||||
if args.all_files:
|
||||
get_filenames = git.get_all_files_matching
|
||||
elif git.is_in_merge_conflict():
|
||||
get_filenames = git.get_conflicted_files_matching
|
||||
else:
|
||||
get_filenames = git.get_staged_files_matching
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import functools
|
||||
import logging
|
||||
import os
|
||||
import os.path
|
||||
import re
|
||||
|
|
@ -7,7 +8,11 @@ from plumbum import local
|
|||
from pre_commit.util import memoize_by_cwd
|
||||
|
||||
|
||||
def _get_root_new():
|
||||
logger = logging.getLogger('pre_commit')
|
||||
|
||||
|
||||
@memoize_by_cwd
|
||||
def get_root():
|
||||
path = os.getcwd()
|
||||
while len(path) > 1:
|
||||
if os.path.exists(os.path.join(path, '.git')):
|
||||
|
|
@ -17,9 +22,32 @@ def _get_root_new():
|
|||
raise AssertionError('called from outside of the gits')
|
||||
|
||||
|
||||
def is_in_merge_conflict():
|
||||
return os.path.exists(os.path.join('.git', 'MERGE_MSG'))
|
||||
|
||||
|
||||
def parse_merge_msg_for_conflicts(merge_msg):
|
||||
# Conflicted files start with tabs
|
||||
return [
|
||||
line.strip() for line in merge_msg.splitlines() if line.startswith('\t')
|
||||
]
|
||||
|
||||
|
||||
@memoize_by_cwd
|
||||
def get_root():
|
||||
return _get_root_new()
|
||||
def get_conflicted_files():
|
||||
# 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_conflict_filenames = parse_merge_msg_for_conflicts(merge_msg)
|
||||
|
||||
# This will get the rest of the changes made after the merge.
|
||||
# If they resolved the merge conflict by choosing a mesh of both sides
|
||||
# this will also include the conflicted files
|
||||
tree_hash = local['git']['write-tree']().strip()
|
||||
merge_diff_filenames = local['git'][
|
||||
'diff', '-m', tree_hash, 'HEAD', 'MERGE_HEAD', '--name-only',
|
||||
]().splitlines()
|
||||
return set(merge_conflict_filenames) | set(merge_diff_filenames)
|
||||
|
||||
|
||||
@memoize_by_cwd
|
||||
|
|
@ -52,3 +80,4 @@ def get_files_matching(all_file_list_strategy):
|
|||
|
||||
get_staged_files_matching = get_files_matching(get_staged_files)
|
||||
get_all_files_matching = get_files_matching(get_all_files)
|
||||
get_conflicted_files_matching = get_files_matching(get_conflicted_files)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue