Skip to content

Commit 6178aee

Browse files
authored
[Infrastructure,Scripts] Add more pre-commit hooks (#18232)
1 parent b8307bb commit 6178aee

4 files changed

Lines changed: 121 additions & 5 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,47 @@
22
# See https://pre-commit.com/hooks.html for more hooks
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v6.0.0
5+
rev: 3e8a8703264a2f4a69428a0aa4dcb512790b2c8c # frozen: v6.0.0
66
hooks:
7-
- id: trailing-whitespace
7+
- id: check-added-large-files
8+
- id: check-ast
9+
- id: check-case-conflict
10+
- id: check-executables-have-shebangs
11+
- id: check-merge-conflict
12+
- id: check-symlinks
13+
- id: check-yaml
814
- id: end-of-file-fixer
15+
- id: trailing-whitespace
16+
- repo: https://github.com/shellcheck-py/shellcheck-py
17+
rev: a6564f5d57444e123433a5c9af733e3975c77229 # frozen: v0.11.0.1-1
18+
hooks:
19+
- id: shellcheck
20+
- repo: https://github.com/astral-sh/ruff-pre-commit
21+
rev: f12be1ebaa5351c1fc76472de98db2c3446c8253 # frozen: v0.16.10
22+
hooks:
23+
- id: ruff-check # linter
24+
args: ["--fix"]
25+
- id: ruff-format # formatter
26+
- repo: local
27+
hooks:
28+
- id: format-includes
29+
name: format includes
30+
language: python
31+
entry: Scripts/format_includes.py
32+
types: [c++]
933
- repo: https://github.com/pre-commit/mirrors-clang-format
10-
rev: v20.1.3 # clang-format version (keep synced with https://github.com/alisw/ali-bot/blob/master/.github/workflows/c++-code-formatting.yml)
34+
# clang-format version (keep synced with https://github.com/alisw/ali-bot/blob/master/.github/workflows/c++-code-formatting.yml)
35+
rev: 7ec07eeb4135dc6ecf410d24ac4ead986a5d413d # frozen: v20.1.3
1136
hooks:
1237
- id: clang-format
1338
- repo: https://github.com/cpplint/cpplint
14-
rev: 2.0.2
39+
rev: 350702a4d39395571bd118c9a3cff0a573488d61 # frozen: 2.0.2
1540
hooks:
1641
- id: cpplint
42+
- repo: local
43+
hooks:
44+
- id: O2 linter
45+
name: O2 linter
46+
language: python
47+
entry: Scripts/o2_linter.py
48+
types_or: [c++, cmake, python]

‎Scripts/format_includes.awk‎

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
else if ( h ~ /^([[:alnum:]_]+\/)+[[:alnum:]_]+\.h/ ) { $2 = "<"h">" } # other third-party
1616
else if ( $2 ~ /^".*\./ ) { } # other local-looking file
1717
else if ( h ~ /^[[:lower:]_]+\.h/ ) { $2 = "<"h">" } # C system
18-
else if ( h ~ /^[[:lower:]_\/]+/ ) { $2 = "<"h">" } # C++ system
18+
else if ( h ~ /^[[:lower:]_\/]+$/ ) { $2 = "<"h">" } # C++ system
1919
}
2020
print
2121
}

‎Scripts/format_includes.py‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2019-2020 CERN and copyright holders of ALICE O2.
4+
# See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
5+
# All rights not expressly granted are reserved.
6+
#
7+
# This software is distributed under the terms of the GNU General Public
8+
# License v3 (GPL Version 3), copied verbatim in the file "COPYING".
9+
#
10+
# In applying this license CERN does not waive the privileges and immunities
11+
# granted to it by virtue of its status as an Intergovernmental Organization
12+
# or submit itself to any jurisdiction.
13+
14+
"""!
15+
@brief Fix include style
16+
@author Vít Kučera <vit.kucera@cern.ch>, Inha University
17+
@date 2026-10-08
18+
19+
NB: Run before sorting.
20+
21+
Usage: format_includes.py FILE [FILE ...]
22+
"""
23+
24+
import re
25+
import sys
26+
27+
LOCAL = ('"', '"')
28+
EXTERNAL = ("<", ">")
29+
30+
INCLUDE = re.compile(r"(\s*#include\s+)(\S+)")
31+
32+
33+
def fix_line(line: str) -> str:
34+
m = INCLUDE.match(line)
35+
if not m:
36+
return line
37+
pre, tok = m.groups()
38+
rest = line[m.end() :]
39+
h = tok[1:-1]
40+
41+
if re.match(r"(PWG[A-Z]{2}|Common|ALICE3|DPG|EventFiltering|PID|Tools|Tutorials)/.*\.h", h):
42+
d = LOCAL # O2Physics
43+
elif re.match(
44+
r"(Algorithm|CCDB|Common[A-Z]|DataFormats|DCAFitter|Detectors|EMCAL|FDD|Field|Framework|FT0|FV0|GlobalTracking|GPU|ITS|MathUtils|MCH|MFT|MID|PHOS|ReconstructionDataFormats|SimulationDataFormat|TOF|TPC|ZDC).*/.*\.h",
45+
h,
46+
):
47+
d = EXTERNAL # O2
48+
elif re.match(r"(T[A-Z]|Math/|Roo[A-Z])[A-Za-z0-9/]+\.h", h):
49+
d = EXTERNAL # ROOT
50+
elif re.match(r"KF[A-Z][A-Za-z0-9]+\.h", h):
51+
d = EXTERNAL # KFParticle
52+
elif re.match(r"(fastjet/|onnxruntime)", h):
53+
d = EXTERNAL # FastJet, ONNX
54+
elif re.match(r".*DataModel/", h):
55+
d = LOCAL # incomplete path to DataModel
56+
elif re.match(r"([A-Za-z0-9_]+/)+[A-Za-z0-9_]+\.h", h):
57+
d = EXTERNAL # other third-party
58+
elif re.match(r'".*\.', tok):
59+
return line # other local-looking file
60+
elif re.match(r"[a-z_]+\.h", h):
61+
d = EXTERNAL # C system
62+
elif re.match(r"[a-z_/]+$", h):
63+
d = EXTERNAL # C++ system (whole string)
64+
else:
65+
return line
66+
67+
return pre + d[0] + h + d[1] + rest
68+
69+
70+
def process(path: str):
71+
# newline='' keeps the original line endings untouched.
72+
with open(path, newline="") as f:
73+
lines = f.readlines()
74+
new_lines = [fix_line(line) for line in lines]
75+
if new_lines != lines:
76+
with open(path, "w", newline="") as f:
77+
f.writelines(new_lines)
78+
79+
80+
if __name__ == "__main__":
81+
if len(sys.argv) < 2:
82+
sys.exit(__doc__)
83+
for p in sys.argv[1:]:
84+
process(p)

‎Scripts/o2_linter.py‎

100644100755
File mode changed.

0 commit comments

Comments
 (0)