Use pre-commit, Clang-Format, format-makefile, shfmt

An area for discussing new ideas and additions to Oolite.

Moderators: another_commander, winston

Post Reply
User avatar
mcarans
---- E L I T E ----
---- E L I T E ----
Posts: 742
Joined: Sun Jun 20, 2010 6:00 pm

Use pre-commit, Clang-Format, format-makefile, shfmt

Post by mcarans »

I suggest adopting standardised formatting for all Oolite source code using pre-commit with Clang-Format, format-makefile, shfmt, using the tool defaults which are industry standards or near as these days. This will also ensure that using different IDEs and text editors doesn't mess things up.
User avatar
mcarans
---- E L I T E ----
---- E L I T E ----
Posts: 742
Joined: Sun Jun 20, 2010 6:00 pm

Re: Use pre-commit, Clang-Format, format-makefile, shfmt

Post by mcarans »

To prevent history destruction with so many files touched, we would need to create .git-blame-ignore-revs with the commit hash of the squashed and merged PR. When Git or GitHub sees a commit in this file, it looks past that commit to the version of the line before it changed so that the formatting changes are ignored. More on this here.

Code: Select all

# 1. Run formatter & Commit
git commit -a -m "Style: Mass reformat"

# 2. Get Hash
git rev-parse HEAD > .git-blame-ignore-revs

# 3. Config Git to use it
git config blame.ignoreRevsFile .git-blame-ignore-revs

# 4. Commit the ignore file
git add .git-blame-ignore-revs
git commit -m "Add ignore-revs file"
All developers need to do: git config blame.ignoreRevsFile .git-blame-ignore-revs in future as part of checking out Oolite.

A suitable pre-commit config:

Code: Select all

repos:
  # 1. Standard Pre-commit Hooks (Essential for Makefiles)
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-added-large-files
      - id: check-case-conflict
      - id: mixed-line-ending
        args: ['--fix=lf']

  # 2. Clang-Format (C, C++, Obj-C)
  - repo: https://github.com/pre-commit/mirrors-clang-format
    rev: v17.0.6
    hooks:
      - id: clang-format
        # You can define types if you only want it on specific files
        types_or: [c, c++, objective-c, cuda]

  # 3. shfmt (Shell Scripts)
  - repo: https://github.com/scop/pre-commit-shfmt
    rev: v3.7.0-4
    hooks:
      - id: shfmt
        # args:
        # -w: write changes to file
        # -s: simplify code
        # -i 4: indent with 4 spaces (standard for shell)
        args: ["-w", "-s", "-i", "4"] 

  # 4. Checkmake (Makefile Linter)
  - repo: https://github.com/mrtazz/checkmake.git
    rev: 0.2.2
    hooks:
      - id: checkmake
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 7192
Joined: Wed Feb 28, 2007 7:54 am

Re: Use pre-commit, Clang-Format, format-makefile, shfmt

Post by another_commander »

This seems complicated with quite high risk of messing up something with all needed configs etc. Could we consider it after releasing 1.92?
User avatar
mcarans
---- E L I T E ----
---- E L I T E ----
Posts: 742
Joined: Sun Jun 20, 2010 6:00 pm

Re: Use pre-commit, Clang-Format, format-makefile, shfmt

Post by mcarans »

another_commander wrote: Tue Jan 06, 2026 9:12 am
This seems complicated with quite high risk of messing up something with all needed configs etc. Could we consider it after releasing 1.92?
Sure that makes sense.
Post Reply