diff --git a/environment/git/install-hooks.sh b/environment/git/install-hooks.sh index 2db431b63..c1ef038c7 100755 --- a/environment/git/install-hooks.sh +++ b/environment/git/install-hooks.sh @@ -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" ########################################################### diff --git a/environment/git/pre-commit b/environment/git/pre-commit index 2494e2cea..573435e47 100755 --- a/environment/git/pre-commit +++ b/environment/git/pre-commit @@ -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. diff --git a/environment/git/pre-commit-autopep8 b/environment/git/pre-commit-autopep8 new file mode 100755 index 000000000..21a6cb98d --- /dev/null +++ b/environment/git/pre-commit-autopep8 @@ -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])