Page 1 of 1

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

Posted: Sun Jan 04, 2026 4:48 am
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.

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

Posted: Tue Jan 06, 2026 3:32 am
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

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

Posted: Tue Jan 06, 2026 9:12 am
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?

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

Posted: Tue Jan 06, 2026 7:08 pm
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.