diff --git a/README.rst b/README.rst index 5c6613775..48bd85627 100644 --- a/README.rst +++ b/README.rst @@ -50,6 +50,7 @@ Optional Integrations * Serve static files from Amazon S3 or Whitenoise_ * Configuration for Celery_ +* Configuration for Precommit-Hooks_ * Integration with MailHog_ for local email testing * Integration with Sentry_ for error logging * Integration with NewRelic_ for performance monitoring @@ -72,7 +73,7 @@ Optional Integrations .. _docker-compose: https://www.github.com/docker/compose .. _Opbeat: https://opbeat.com/ .. _PythonAnywhere: https://www.pythonanywhere.com/ - +.. _Precommit-Hooks_: http://pre-commit.com/ Constraints ----------- @@ -127,6 +128,7 @@ Answer the prompts with your own desired options_. For example:: use_docker [y]: y use_heroku [n]: n use_grunt [n]: y + use_precommit_hooks [n]: n Select open_source_license: 1 - MIT 2 - BSD @@ -157,6 +159,10 @@ For development, see the following for local development: .. _`Developing locally`: http://cookiecutter-django.readthedocs.io/en/latest/developing-locally.html .. _`Developing locally using docker`: http://cookiecutter-django.readthedocs.io/en/latest/developing-locally-docker.html +If you chose to install pre-commit hooks, make sure to also run this after git init:: + + $ pre-commit install + Community ----------- diff --git a/cookiecutter.json b/cookiecutter.json index 9b21f3cdc..0c1d606ad 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -18,6 +18,7 @@ "use_docker": "y", "use_heroku": "n", "js_task_runner": ["Gulp", "Grunt", "None"], + "use_precommit_hooks": "n", "use_lets_encrypt": "n", "open_source_license": ["MIT", "BSD", "Apache Software License 2.0", "Not open source"] } diff --git a/docs/project-generation-options.rst b/docs/project-generation-options.rst index 2ea27c53b..cc1da45c8 100644 --- a/docs/project-generation-options.rst +++ b/docs/project-generation-options.rst @@ -49,6 +49,9 @@ use_python2 [n] By default, the Python code generated will be for Python 3.x. But if you answer `y` here, it will be legacy Python 2.7 code. +use_precommit_hooks [n] + If yes, include a basic configurations for git hooks that have to pass on each commit (or skipped). + .. _WhiteNoise: https://github.com/evansd/whitenoise .. _Celery: https://github.com/celery/celery .. _MailHog: https://github.com/mailhog/MailHog diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index d83d92287..83f0e44f8 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -111,6 +111,13 @@ def remove_pycharm_dir(project_directory): shutil.rmtree(docs_dir_location) +def remove_pre_commit_file(): + """ + Removes pre-commit hook yaml file + """ + os.remove(os.path.join(PROJECT_DIRECTORY, '.pre-commit-config.yaml')) + + def remove_heroku_files(): """ Removes files needed for heroku if it isn't going to be used @@ -245,5 +252,9 @@ if '{{ cookiecutter.use_lets_encrypt }}'.lower() == 'y' and '{{ cookiecutter.use "You must generate a dhparams.pem file before running docker-compose in a production environment." ) +# 11. If pre-commit hooks aren't used, remove pre-commit yaml file +if '{{ cookiecutter.use_precommit_hooks }}'.lower() != 'y': + remove_pre_commit_file() + # 4. Copy files from /docs/ to {{ cookiecutter.project_slug }}/docs/ # copy_doc_files(PROJECT_DIRECTORY) diff --git a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml new file mode 100644 index 000000000..86a3d0c7c --- /dev/null +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +- repo: git://github.com/pre-commit/pre-commit-hooks + sha: "6b1aa19425b35f8c34f43ada8f1045b028dc19a5" + hooks: + # keep ids alphabetical! see http://pre-commit.com/hooks.html + - id: debug-statements + files: \.py$ + - id: flake8 + files: \.(py|sh)$ + - id: name-tests-test + name: Django Tests should start with test_ prefix + files: tests/.+\.py$ + args: ['--django'] # args: ['--django'] will match test*.py instead. +- repo: git://github.com/FalconSocial/pre-commit-python-sorter + sha: 1.0.4 + hooks: + - id: python-import-sorter + args: ['--diff'] diff --git a/{{cookiecutter.project_slug}}/README.rst b/{{cookiecutter.project_slug}}/README.rst index 55a520c54..912a800a6 100644 --- a/{{cookiecutter.project_slug}}/README.rst +++ b/{{cookiecutter.project_slug}}/README.rst @@ -73,6 +73,26 @@ Please note: For Celery's import magic to work, it is important *where* the cele {% endif %} +{% if cookiecutter.use_precommit_hooks == "y" %} + +Pre-Commit Hooks +^^^^^^^^^^^^^^^^ + +To use pre-commit hooks when committing messages, after initial git commit, run +.. code-block:: bash + + pre-commit install + +Now on each commit message, this will run whatever commit plugins have been enabled. To skip a pre-commit hook, add a -n (no verify) parameter at the end of your commit message. + +.. code-block:: bash + + git commit -m "i put a really bad pdb in here" -n + + +{% endif %} + + {% if cookiecutter.use_mailhog == "y" %} Email Server diff --git a/{{cookiecutter.project_slug}}/requirements/local.txt b/{{cookiecutter.project_slug}}/requirements/local.txt index 0b43dde2d..368b8da1a 100644 --- a/{{cookiecutter.project_slug}}/requirements/local.txt +++ b/{{cookiecutter.project_slug}}/requirements/local.txt @@ -14,3 +14,7 @@ ipdb==0.10.0 pytest-django==2.9.1 pytest-sugar==0.7.1 + +{% if cookiecutter.use_precommit_hooks == 'y' -%} +pre-commit==0.8.2 +{%- endif %}