-
Notifications
You must be signed in to change notification settings - Fork 127
Pre-commit hook and GitHub action for checking for missing newline at end of file #6432
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
base: master
Are you sure you want to change the base?
Conversation
Added a pre-commit hook and a GitHub action that will check if any of the committed files have a missing trailing newline (or if they have more than one trailing newlines)
b912fcb
to
d0b0127
Compare
I do not know exactly how these hooks work, but I am positive to start using them as a simple and un-intrusive way to get some more automation of things like end-of-lines, but also formatting etc. This can then be encouraged, without mandating it and with the ability to override it on a per-commit basis. As far as I understand your proposal, the whole pre-commit script only does the end-of-line cleanup, I suggest you move that to a separate file and call that from a one-line pre-commit script, then it is much more obvious how to extend it to do more if we want to. |
I get that having everything in the repository, and not relying on third party external packages, is nice, but as this is an optional thing, why not use the pre-commit framework and use existing hooks? The pre-commit system already has a end-of-file-fixer that does the same as this PR as far as can tell, with the upside that it is heavily tested and validated on other code bases as well. Another upside of pre-commit is that we can easily load in C++ specific hooks, for instance for clang-format and clang-tidy. |
Create a pre-commit orchestrator script that calls individual hook scripts. This makes it easier to extend with more hooks in the future.
With the orchestration, we are getting closer to implementing our own pre-commit-framework, and I am not sure that is a good idea. The script you created looks nice, but it seems to be rather system dependent. To get the git changes in the script, you seem to be parsing the output of the git command, rather than using a ready made library for instance. I would imagine this can easily change if we have some weird system setup? All this to say, I really struggle to see the value of adding this as a manually written script, rather than just relying on the pre-commit framework. |
This is an alternative to OPM#6432 that used manual pre-commit hooks.
This is an alternative to OPM#6432 that used manual pre-commit hooks.
Added a pre-commit hook and a GitHub action that will check if any of the committed files of type C/C++/Python/shell/yaml have a missing trailing newline (or if they have more than one trailing newlines).
This hook ensures that all staged source code files end with a newline. I.e. it adds a newline
at the end of the file if it is missing, or it truncates more than one newline at the end of the
file to a single newline.
It can be run locally or from CI (see .github/workflows/trailing_newline_checker.yml).
Rationale:
According to the POSIX standard, a "line" in a text file is defined as a sequence of
zero or more non-newline characters plus a terminating newline character. Therefore, a text file,
by definition, should end with a newline. Some reasons why we should stick to this standard:
annoying when the changes are not related to the end of the file.
Other tools that do the same:
.clang-format
file in the root of the repository (withInsertNewlineAtEOF: true
).But this also do other C++ formatting and does not apply to Python and shell scripts.
sometimes files are edited in the terminal or generated by scripts and the editor does not run.
Installation:
To install locally, run:
git config core.hooksPath .githooks
To disable locally, run:
git config --unset core.hooksPath
To disable for a single commit, run:
git commit --no-verify ...