Skip to content

Conversation

hakonhagland
Copy link
Contributor

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:

  • Merge noise: Changes at the end of files may cause unnecessary conflicts. This is especially
    annoying when the changes are not related to the end of the file.
  • Messy diffs: Git marks it specially when one version has the newline and another doesn't.

Other tools that do the same:

  • We already have a .clang-format file in the root of the repository (with InsertNewlineAtEOF: true).
    But this also do other C++ formatting and does not apply to Python and shell scripts.
  • Editors like VSCode and Cursor have settings to automatically add a newline at the end of the file. But
    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 ...

@hakonhagland hakonhagland added the manual:irrelevant This PR is a minor fix and should not appear in the manual label Aug 29, 2025
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)
@atgeirr
Copy link
Member

atgeirr commented Sep 18, 2025

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.

@akva2, @kjetilly what do you think?

@kjetilly
Copy link
Contributor

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.
@kjetilly
Copy link
Contributor

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.

hakonhagland added a commit to hakonhagland/opm-simulators-1 that referenced this pull request Sep 18, 2025
This is an alternative to OPM#6432 that used manual pre-commit hooks.
hakonhagland added a commit to hakonhagland/opm-simulators-1 that referenced this pull request Sep 18, 2025
This is an alternative to OPM#6432 that used manual pre-commit hooks.
@hakonhagland
Copy link
Contributor Author

why not use the pre-commit framework and use existing hooks?

@kjetilly Good idea! See #6483 for a take on this alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

manual:irrelevant This PR is a minor fix and should not appear in the manual

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants