-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pre-commit config with pre-commit-hooks #3283
Conversation
Adding a .pre-commit-config.yaml file with some pre-commit hooks (check-added-large-files, check-yaml, end-of-file-fixer, trailing-whitespace). Also added pre-commit.ci config with autofix_prs=false, and autoupdate_schedule=quarterly.
Results from first pre-commit.ci run at https://results.pre-commit.ci/run/github/85352251/1717479459.EAFPxChRS_aFLgAcskCvfQ:
Do we want to try applying the autofixes directly in this PR? |
Yes to make sure that autofix works as expected. |
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
- id: check-added-large-files | ||
- id: check-yaml | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can also add these hooks from https://github.com/Lucas-C/pre-commit-hooks that handle LF file endings and 644 permissions? (Then we can remove these lines from style_checks.yml).
- id: trailing-whitespace | |
- 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'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, not sure why, but pre-commit.ci seeems to think our files have 664 permission instead of 644? See logs at https://results.pre-commit.ci/run/github/85352251/1717481309.E-FyucnQQ_CvuFT06LGuHg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added back the GitHub Action workflows for 644 permission checks/formatting for now at commit 5a01ad6.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're not using pre-commit.ci anymore but just GitHub Actions, I've reinstated the 644 pre-commit hook in the YAML file. Seems to be working at https://github.com/GenericMappingTools/pygmt/actions/runs/9458943127/job/26055263366?pr=3283#step:5:31:
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Initializing environment for https://github.com/Lucas-C/pre-commit-hooks.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/Lucas-C/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
check for added large files..............................................Passed
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
CRLF end-lines checker...................................................Passed
CRLF end-lines remover...................................................Passed
Set file permissions.....................................................Failed
- hook id: chmod
- exit code: 1
- files were modified by this hook
Fixing file permissions on requirements.txt: 0o755 -> 0o644
Error: Process completed with exit code 1.
Don't forget to add the |
Enforcing LF line endings and 644 permissions via pre-commit hooks!
Partially revert ce6d457 to keep the 644 permission checks and format commands.
doc/contributing.md
Outdated
Even better, you can just write `/format` and/or `pre-commit.ci autofix` in the first | ||
line of any comment in a pull request to lint the code automatically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debating on whether to just add pre-commit run --all-files
to the Makefile, or have it listed separately. Main consideration is that Windows users won't have make
installed by default, so they would only be able to run pre-commit run --all-files
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make
is added as a development dependency in environment.yml
, so even Windows developers have make
installed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, forgot that it is possible to conda install make now! I'll add it to the Makefile then.
Also need to mention |
doc/contributing.md
Outdated
Even better, you can just write `/format` and/or `pre-commit.ci autofix` in the first | ||
line of any comment in a pull request to lint the code automatically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make
is added as a development dependency in environment.yml
, so even Windows developers have make
installed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also need to mention
pre-commit.ci autofix
in the PR template.
Done at b3eee6f. But I'm also wondering if this is redundant, because I've added pre-commit run --all-files
to the make format
command, so typing /format
will also run pre-commit now 🙂
Adding What about adding |
As long as they have $ pre-commit clean # uninstall/clean out pre-commit files
$ make format
ruff check --fix --exit-zero pygmt doc/conf.py examples
All checks passed!
ruff format pygmt doc/conf.py examples
301 files left unchanged
pre-commit run --all-files
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Initializing environment for https://github.com/Lucas-C/pre-commit-hooks.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/Lucas-C/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
check for added large files..............................................Passed
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
CRLF end-lines checker...................................................Passed
CRLF end-lines remover...................................................Passed
Depends on whether we want |
Good to know. Then we don't need the pre-commit.ci service anymore, right? |
Yes, pre-commit.ci isn't absolutely necessary since we can run it via |
I'm OK with either keeping pre-commit.ci or not. |
Co-Authored-By: Dongdong Tian <[email protected]>
/format |
Ok, I've removed the Edit: Yep, 644 permission check seems to be ok, see note at #3283 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great.
Description of proposed changes
Add pre-commit hooks to enforce certain rules on all files in the repo.
The following rules are applied:
Note that
pre-commit run --all-files
has been added to the Makefile, so runningmake format
locally or/format
on a GitHub PR will apply the formatting rules.TODO:
.pre-commit-config.yaml
file for standard pre-commit hooksConfigure pre-commit-ci GitHub Appnot using, doingpre-commit
manually ourselvesReferences:
Fixes #1593
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.Slash Commands
You can write slash commands (
/command
) in the first line of a comment to performspecific operations. Supported slash command is:
/format
: automatically format and lint the code