From 83a71de11ca96aa8cf0c9cc28ef0e654b84f3d67 Mon Sep 17 00:00:00 2001 From: jeffshek Date: Thu, 2 Jun 2016 19:38:03 -0400 Subject: [PATCH 1/5] add pre-commit hooks --- README.rst | 5 +++++ cookiecutter.json | 1 + hooks/post_gen_project.py | 12 ++++++++++- .../.pre-commit-config.yaml | 20 +++++++++++++++++++ .../requirements/local.txt | 4 ++++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 {{cookiecutter.project_slug}}/.pre-commit-config.yaml diff --git a/README.rst b/README.rst index 14dcf2078..bfbe18364 100644 --- a/README.rst +++ b/README.rst @@ -132,6 +132,7 @@ Answer the prompts with your own desired options_. For example:: use_heroku [n]: n use_grunt [n]: y use_angular [n]: n + use_precommit_hooks [n]: n Select open_source_license: 1 - MIT 2 - BSD @@ -162,6 +163,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 8fe7010a2..10f8a8d52 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -22,5 +22,6 @@ "use_heroku": "n", "use_grunt": "n", "use_angular": "n", + "use_precommit_hooks": "n", "open_source_license": ["MIT", "BSD", "Not open source"] } diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index cffb052e8..e33f57c8c 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -106,6 +106,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 @@ -180,7 +187,6 @@ if '{{ cookiecutter.use_docker }}'.lower() != 'y': if '{{ cookiecutter.use_grunt }}'.lower() != 'y': remove_grunt_files() - # 7. Display a warning if use_docker and use_grunt are selected. Grunt isn't supported by our # docker config atm. if '{{ cookiecutter.use_grunt }}'.lower() == 'y' and '{{ cookiecutter.use_docker }}'.lower() == 'y': @@ -199,5 +205,9 @@ if '{{ cookiecutter.use_mailhog }}'.lower() == 'y' and '{{ cookiecutter.use_dock " mailhog service to your docker configuration manually." ) +# 8. 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..65bc20e24 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -0,0 +1,20 @@ +- 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: end-of-file-fixer + files: \.(py|sh)$ + - id: flake8 + files: \.(py|sh)$ + - id: name-tests-test + files: tests/.+\.py$ + args: ['--django'] # Use args: ['--django'] to match test*.py instead. + - id: trailing-whitespace + files: \.(py|sh|yaml|txt)$ +- 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}}/requirements/local.txt b/{{cookiecutter.project_slug}}/requirements/local.txt index c88093fef..9264998a7 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 %} \ No newline at end of file From 4d0ccdcd69ba0bbd400fb29fca6715ef852e2f33 Mon Sep 17 00:00:00 2001 From: jeffshek Date: Wed, 8 Jun 2016 00:28:12 -0400 Subject: [PATCH 2/5] remove eof and tw pre-commit --- {{cookiecutter.project_slug}}/.pre-commit-config.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml index 65bc20e24..9f0c91201 100644 --- a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -4,15 +4,11 @@ # keep ids alphabetical! see http://pre-commit.com/hooks.html - id: debug-statements files: \.py$ - - id: end-of-file-fixer - files: \.(py|sh)$ - id: flake8 files: \.(py|sh)$ - id: name-tests-test files: tests/.+\.py$ args: ['--django'] # Use args: ['--django'] to match test*.py instead. - - id: trailing-whitespace - files: \.(py|sh|yaml|txt)$ - repo: git://github.com/FalconSocial/pre-commit-python-sorter sha: 1.0.4 hooks: From b8470d61294be2e61d6cf956a17d272666bebf18 Mon Sep 17 00:00:00 2001 From: jeffshek Date: Wed, 8 Jun 2016 00:38:07 -0400 Subject: [PATCH 3/5] update name in pre-commit hook to explain django --- {{cookiecutter.project_slug}}/.pre-commit-config.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml index 9f0c91201..86a3d0c7c 100644 --- a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -7,8 +7,9 @@ - id: flake8 files: \.(py|sh)$ - id: name-tests-test + name: Django Tests should start with test_ prefix files: tests/.+\.py$ - args: ['--django'] # Use args: ['--django'] to match test*.py instead. + args: ['--django'] # args: ['--django'] will match test*.py instead. - repo: git://github.com/FalconSocial/pre-commit-python-sorter sha: 1.0.4 hooks: From 78ddd2df8a7089ff02afd29c00b37739722d4224 Mon Sep 17 00:00:00 2001 From: jeffshek Date: Wed, 8 Jun 2016 00:57:07 -0400 Subject: [PATCH 4/5] update documentation, fix some spacing issues --- README.rst | 4 ++-- {{cookiecutter.project_slug}}/README.rst | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 915063794..dfc16006e 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 ----------- @@ -128,7 +129,6 @@ Answer the prompts with your own desired options_. For example:: use_heroku [n]: n use_grunt [n]: y use_precommit_hooks [n]: n - Select open_source_license: 1 - MIT 2 - BSD 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 From eb5de902f6ff208b00792830570ec2eb7a0ee6ca Mon Sep 17 00:00:00 2001 From: jeffshek Date: Wed, 8 Jun 2016 01:04:00 -0400 Subject: [PATCH 5/5] add git commit hooks to project-generation-options.rst --- docs/project-generation-options.rst | 3 +++ 1 file changed, 3 insertions(+) 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