Skip to content

Commit

Permalink
Add autopep8 git hook (#1100)
Browse files Browse the repository at this point in the history
* Bad python for commit test

* ?

* Add autopep8

* Cleanup

* bad formatting

* testing again

* One more test

* That test didnt work for flake8 anyway
  • Loading branch information
brryan authored Aug 6, 2021
1 parent 871d8ba commit a659254
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
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"
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])

0 comments on commit a659254

Please sign in to comment.