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

Add autopep8 git hook #1100

Merged
merged 8 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion environment/git/install-hooks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
###########################################################
# CONFIGURATION:
# select which pre-commit hooks are going to be installed
HOOKS="pre-commit pre-commit-clang-format pre-commit-flake8 pre-commit-fprettify"
HOOKS="pre-commit pre-commit-clang-format pre-commit-autopep8 pre-commit-flake8 pre-commit-fprettify"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need pre-commit-flake8 if we adopt pre-commit-autopep8?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't mind switching to autopep8 from flake8, that would solve the above consistency issue I brought up. I do like that autopep8 will automatically apply formatting changes, but I don't know if there are reasons for us to still prefer flake8 at the CI level

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The superlinter CI check that we use will run flake8. I want to keep that CI because it has a very low maintenance cost. It sounds like we need to keep both for now.

HOOKS="$HOOKS pre-commit-cmake-format pre-commit-cmake-lint"
TOOLS="common.sh"
###########################################################
Expand Down
2 changes: 2 additions & 0 deletions environment/git/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# the order in which they are listed.
HOOKS="pre-commit-clang-format"

# only run autopep8 if the tool is available
[[ $(which autopep8 2> /dev/null | wc -w) -gt 0 ]] && HOOKS+=" pre-commit-autopep8"
# only run flake8 if the tool is available.
[[ $(which flake8 2> /dev/null | wc -w) -gt 0 ]] && HOOKS+=" pre-commit-flake8"
# only run cmake-format if the tool is available.
Expand Down
18 changes: 18 additions & 0 deletions environment/git/pre-commit-autopep8
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
import os
import subprocess

if __name__ == '__main__':
output = subprocess.run(['git', 'rev-parse', '--show-toplevel'], capture_output=True)
base_dir = output.stdout.decode('utf-8').strip()
output = subprocess.run(['git', 'status', '--porcelain'], capture_output=True)
all_files = output.stdout.decode('utf-8')
modified_files = []
for line in all_files.split('\n'):
if 'A' in line[0:2] or 'M' in line[0:2]:
modified_files.append(os.path.join(base_dir, line[2:].strip()))

for filename in modified_files:
autopep8_call = ['autopep8', '--in-place', filename]
subprocess.call(autopep8_call)
subprocess.call(['git', 'add', filename])