Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deal with () in filenames by escaping them (NO_JIRA) #27

Merged
merged 9 commits into from
Mar 14, 2025
20 changes: 14 additions & 6 deletions main/githooks.py
Original file line number Diff line number Diff line change
@@ -56,6 +56,14 @@
# File types that need a terminating newline
TERMINATING_NEWLINE_EXTS = ['.c', '.cpp', '.h', '.inl']

esc_re = re.compile(r'\s|[]()[]')
def _esc_char(match):
return '\\' + match.group(0)


def _escape_filename(filename):
return esc_re.sub(_esc_char, filename)


def _get_output(command, cwd='.'):
return subprocess.check_output(command, shell=True, cwd=cwd).decode(errors='replace')
@@ -121,7 +129,7 @@ def get_file_content_as_binary(filename):
_skip(filename, 'File is not UTF-8 encoded')
data = None
else:
data = _get_output(f'git show :{filename}')
data = _get_output(f'git show :{_escape_filename(filename)}')
return data


@@ -134,7 +142,7 @@ def get_text_file_content(filename):
if _is_github_event() or 'pytest' in sys.modules:
data = Path(filename).read_text()
else:
data = _get_output(f'git show :{filename}')
data = _get_output(f'git show :{_escape_filename(filename)}')
return data


@@ -166,7 +174,7 @@ def get_branch_files():

def add_file_to_index(filename):
'''Add file to current commit'''
return _get_output(f'git add {filename}')
return _get_output(f'git add {_escape_filename(filename)}')


def get_commit_files():
@@ -244,13 +252,13 @@ def get_changed_lines(modified_file):
if _is_github_event():
if _is_pull_request():
output = _get_output(
f'git diff --unified=0 remotes/origin/{os.environ["GITHUB_BASE_REF"]}..remotes/origin/{os.environ["GITHUB_HEAD_REF"]} -- {modified_file}')
f'git diff --unified=0 remotes/origin/{os.environ["GITHUB_BASE_REF"]}..remotes/origin/{os.environ["GITHUB_HEAD_REF"]} -- {_escape_filename(modified_file)}')
else:
output = _get_output(
f'git diff --unified=0 HEAD~ {modified_file}')
f'git diff --unified=0 HEAD~ {_escape_filename(modified_file)}')
else:
output = _get_output(
f'git diff-index HEAD --unified=0 {modified_file}')
f'git diff-index HEAD --unified=0 {_escape_filename(modified_file)}')

lines = []
for line in output.splitlines():