Skip to content

Commit

Permalink
Add pre-commit config with pre-commit-hooks (#3283)
Browse files Browse the repository at this point in the history
Adding a .pre-commit-config.yaml file with some pre-commit hooks
(check-added-large-files, check-yaml, end-of-file-fixer, trailing-whitespace,
forbid-crlf, remove-crlf, chmod).

* Add .pre-commit-config.yaml to MAINIFEST.in
* Add forbid-crlf, remove-crlf and chmod hooks, remove from GitHub Actions
* Add `pre-commit run --all-files` to `make format`
* Add pre-commit to Style Checks and format-command workflows
* Update note on style_checks.yaml to mention ruff+pre-commit is ran
* Mention that pre-commit is used to enforce LF ending and 644 permissions

---------

Co-authored-by: Dongdong Tian <[email protected]>
  • Loading branch information
weiji14 and seisman committed Jun 11, 2024
1 parent 844594f commit b5242be
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 24 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/format-command.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ jobs:
# Install formatting tools
- name: Install formatting tools
run: |
python -m pip install ruff
python -m pip install ruff pre-commit
python -m pip list
sudo apt-get install dos2unix
# Run "make format" and commit the change to the PR branch
- name: Commit to the PR branch if any changes
run: |
make format
git ls-files -z | xargs -0 dos2unix --quiet
git ls-files -z | xargs -0 chmod 644
if [[ $(git ls-files -m) ]]; then
git config --global user.name 'actions-bot'
git config --global user.email '[email protected]'
Expand Down
13 changes: 4 additions & 9 deletions .github/workflows/style_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,13 @@ jobs:

- name: Install packages
run: |
python -m pip install ruff
python -m pip install ruff pre-commit
python -m pip list
sudo apt-get install dos2unix
- name: Formatting check (ruff)
run: make check

- name: Ensure files use UNIX line breaks and have 644 permission
- name: Formatting check (ruff + pre-commit)
run: |
git ls-files -z | xargs -0 dos2unix --quiet
git ls-files -z | xargs -0 chmod 644
if [[ $(git ls-files -m) ]]; then git --no-pager diff HEAD; exit 1; fi
make check
pre-commit run --all-files
- name: Ensure example scripts have at least one code block separator
run: |
Expand Down
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.5
hooks:
- id: forbid-crlf
- id: remove-crlf
- id: chmod
args: ['644']
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
All participants in the PyGMT community must abide by
the [Generic Mapping Tools organization Code of Conduct](https://github.com/GenericMappingTools/.github/blob/main/CODE_OF_CONDUCT.md).
the [Generic Mapping Tools organization Code of Conduct](https://github.com/GenericMappingTools/.github/blob/main/CODE_OF_CONDUCT.md).
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ prune examples*
prune pygmt/tests*
exclude .dvcignore
exclude .gitignore
exclude .pre-commit-config.yaml
exclude .readthedocs.yaml
exclude AUTHORSHIP.md
exclude CODE_OF_CONDUCT.md
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ test_no_images: _runtest
format:
ruff check --fix --exit-zero $(FORMAT_FILES)
ruff format $(FORMAT_FILES)
pre-commit run --all-files

check:
ruff check $(FORMAT_FILES)
Expand Down
1 change: 0 additions & 1 deletion doc/_templates/autosummary/class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@
.. raw:: html

<div style='clear:both'></div>

1 change: 0 additions & 1 deletion doc/_templates/autosummary/exception.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
.. raw:: html

<div style='clear:both'></div>

1 change: 0 additions & 1 deletion doc/_templates/autosummary/function.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@
.. raw:: html

<div style='clear:both'></div>

4 changes: 2 additions & 2 deletions doc/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
### Highlights

* 🎉 **Twelfth minor release of PyGMT** 🎉
* 🚀 Almost all module wrappers (with a few exceptions) now use in-memory GMT *virtual files* instead of intermediate temporary files to improve performance ([#2730](https://github.com/GenericMappingTools/pygmt/issues/2730))
* 🚀 Almost all module wrappers (with a few exceptions) now use in-memory GMT *virtual files* instead of intermediate temporary files to improve performance ([#2730](https://github.com/GenericMappingTools/pygmt/issues/2730))
* Almost all module wrappers (with a few exceptions) now have consistent behavior for table-like output ([#1318](https://github.com/GenericMappingTools/pygmt/issues/1318))
* Adopt [SPEC 0](https://scientific-python.org/specs/spec-0000/) policy for minimum supported versions of GMT, Python, and other core dependencies
* Adopt [SPEC 0](https://scientific-python.org/specs/spec-0000/) policy for minimum supported versions of GMT, Python, and other core dependencies

### Enhancements

Expand Down
4 changes: 2 additions & 2 deletions doc/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ the code yourself. Before committing, run it to automatically format your code:
make format
```

For consistency, we also use UNIX-style line endings (`\n`) and file permission
644 (`-rw-r--r--`) throughout the whole project.
For consistency, we also use `pre-commit` hooks to enforce UNIX-style line endings
(`\n`) and file permission 644 (`-rw-r--r--`) throughout the whole project.
Don't worry if you forget to do it. Our continuous integration systems will
warn us and you can make a new commit with the formatted code.
Even better, you can just write `/format` in the first line of any comment in a
Expand Down
1 change: 0 additions & 1 deletion doc/team.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,3 @@ year will be moved to Distinguished Contributors.
::::

:::::

1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies:
- pip
# Dev dependencies (style checks)
- codespell
- pre-commit
- ruff>=0.3.0
# Dev dependencies (unit testing)
- matplotlib-base
Expand Down
2 changes: 1 addition & 1 deletion examples/projections/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Projections
===========

PyGMT supports many map projections. Use the ``projection`` parameter to specify which
one you want to use in all plotting methods. The projection is specified by a one-letter
one you want to use in all plotting methods. The projection is specified by a one-letter
code along with (sometimes optional) reference longitude and latitude and the
width of the map (for example, **A**\ *lon0/lat0*\ [*/horizon*\ ]\ */width*). The map
height is determined based on the region and projection.
Expand Down
2 changes: 1 addition & 1 deletion pygmt/tests/data/contours.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
600 A
700 C
800 A
900 C
900 C

0 comments on commit b5242be

Please sign in to comment.