Put the pre-commit file under .git/hooks/ of your Python project, and make sure it is well-formatted (LF) and it has the correct permissions.
The following steps will get you the pre-commit hook in your project:
cd path/to/your/projectcurl -fsSL "https://gist.githubusercontent.com/Cynnexis/cd7fdc7b911ac39b623a3a62105e7d45/raw/pre-commit" -o .git/hooks/pre-commit- If
yapfis not in yourPATH, edit.git/hooks/pre-commitand specify the absolute path to yourYAPFexecutable at line 12. chmod 776 .git/hooks/pre-commit- If you use Windows:
dos2unix .git/hooks/pre-commit
If you want to automate its creation from a Makefile, use the following rule:
SHELL := /bin/bash
.ONESHELL:
.PHONY: help configure-git configure
# [...]
help:
# [...]
@echo " configure - Configure the project folder."
@echo " configure-git - Configure the project folder for git usage. Use 'configure' for global configuration of the project."
# [...]
.git/hooks/pre-commit:
curl -fsSL "https://gist.githubusercontent.com/Cynnexis/cd7fdc7b911ac39b623a3a62105e7d45/raw/pre-commit" -o ".git/hooks/pre-commit"
@if command -v "dos2unix" > /dev/null 2>&1; then \
dos2unix ".git/hooks/pre-commit"; \
else \
echo "dos2unix not found. If you are on Windows, you may consider installing it."; \
fi
configure-git: .git/hooks/pre-commit
configure: configure-git
# [...]If you use Visual Studio Code, you can create a task for YAPF:
.vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "YAPF",
"type": "shell",
"group": "none",
"command": "yapf",
"args": [
"-i",
"-r",
"${workspaceFolder}"
],
"presentation": {
"reveal": "silent"
},
"problemMatcher": []
}
]
}To bind this task to a keyboard shortcut, add the following JSON object to your global keybindings.json:
keybindings.json:
{
"key": "ctrl+shift+y",
"command": "workbench.action.tasks.runTask",
"args": "YAPF",
"when": "workbenchState == workspace && editorLangId == python"
}