Skip to content

Commit 1603b74

Browse files
committed
Add a precommit driver
1 parent 49c309c commit 1603b74

15 files changed

Lines changed: 1040 additions & 55 deletions

.clang-tidy

Lines changed: 0 additions & 43 deletions
This file was deleted.

make_pretty.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash -e
2+
3+
ABACUS_ROOT=$(dirname "$0")
4+
5+
"${ABACUS_ROOT}/tools/precommit/precommit.py" --allow-modifications "$@"
6+
7+
#EOF

tools/precommit/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM ubuntu:24.04
2+
3+
# Based on CP2K's tools/precommit/Dockerfile, adapted for ABACUS.
4+
5+
WORKDIR /opt/abacus-precommit
6+
COPY install_requirements.sh requirements.txt ./
7+
RUN ./install_requirements.sh
8+
ENV PATH="/opt/venv/bin:/opt/abacus-precommit:$PATH"
9+
10+
ARG REVISION
11+
ENV REVISION=${REVISION}
12+
13+
COPY . ./
14+
CMD ["gunicorn", "--bind=:8080", "--workers=1", "--threads=8", "--timeout=0", "precommit_server:app"]
15+
16+
# EOF

tools/precommit/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# ABACUS Precommit
2+
3+
This is a CP2K-style precommit system adapted for ABACUS. The local driver is
4+
`tools/precommit/precommit.py`; the formatting tools are provided by a small
5+
Flask/gunicorn precommit server.
6+
7+
The design intentionally follows CP2K's usage model:
8+
9+
- one local driver script;
10+
- `obj/precommit/cache.json` mtime cache;
11+
- file-level processing;
12+
- local backup before each tool modifies a file;
13+
- default check-only mode;
14+
- `--allow-modifications` to keep formatter changes;
15+
- remote or local Docker server for external tools.
16+
17+
## Install Git hook
18+
19+
From the ABACUS repository root:
20+
21+
```bash
22+
ln -fs ../../tools/precommit/precommit.py .git/hooks/pre-commit
23+
```
24+
25+
## Run checks
26+
27+
```bash
28+
./tools/precommit/precommit.py
29+
```
30+
31+
The default mode does not modify source files. If a formatter would change a
32+
file, the driver restores the original content and prints a unified diff.
33+
34+
## Apply modifications
35+
36+
```bash
37+
./tools/precommit/precommit.py --allow-modifications
38+
```
39+
40+
Short option:
41+
42+
```bash
43+
./tools/precommit/precommit.py -m
44+
```
45+
46+
## Ignore cache
47+
48+
```bash
49+
./tools/precommit/precommit.py --no-cache
50+
```
51+
52+
Short option:
53+
54+
```bash
55+
./tools/precommit/precommit.py -a
56+
```
57+
58+
## Process selected files
59+
60+
```bash
61+
./tools/precommit/precommit.py source/module/foo.cpp source/module/foo.h
62+
```
63+
64+
## Local server
65+
66+
Until an ABACUS precommit server is deployed, run the server locally with Docker:
67+
68+
```bash
69+
cd tools/precommit
70+
./start_local_server.sh
71+
```
72+
73+
Then, in another terminal from the repository root:
74+
75+
```bash
76+
export ABACUS_PRECOMMIT_SERVER="http://127.0.0.1:8080"
77+
./tools/precommit/precommit.py
78+
```
79+
80+
## Tools
81+
82+
The first ABACUS version keeps CP2K's lightweight file-level model and maps it to
83+
ABACUS' C++-first source tree:
84+
85+
- C/C++/CUDA/HIP/OpenCL: `clang-format`
86+
- Python: `ast.parse` + `black`
87+
- Shell: `shfmt` + `shellcheck`
88+
- Markdown: `mdformat --wrap=100`
89+
- CMake: `cmake-format -i`
90+
- Makefile: local `format_makefile.py`
91+
- all files: local `check_file_properties.py`
92+
93+
`clang-tidy` and `compile_commands.json` are intentionally not part of this
94+
CP2K-style first version. They are build-level static-analysis concerns, while
95+
this precommit driver is a lightweight file-level formatting and convention gate.
96+
97+
## Notes for ABACUS maintainers
98+
99+
The only non-CP2K structural change is that C++ is first-class. CP2K rejects
100+
most C++ files; this ABACUS version formats `.c`, `.cc`, `.cpp`, `.cxx`, `.h`,
101+
`.hh`, `.hpp`, `.hxx`, `.cu`, `.cuh`, `.hip`, and `.cl` files.
102+
103+
`check_file_properties.py` contains the ABACUS-specific convention checks. The
104+
banner/license policy should be tightened once the exact ABACUS source header is
105+
agreed upon.
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
---
22
Language: Cpp
3-
BasedOnStyle: Microsoft
3+
BasedOnStyle: LLVM
44

55
AlwaysBreakTemplateDeclarations: Yes
66

7+
ColumnLimit: 120
78
AllowAllArgumentsOnNextLine: false
89
AllowAllParametersOfDeclarationOnNextLine: false
910
BinPackArguments: false
1011
BinPackParameters: false
1112

12-
BreakBeforeBinaryOperators: All
1313
BreakBeforeTernaryOperators: true
1414

1515
Cpp11BracedListStyle: true
1616
FixNamespaceComments: true
1717

18-
# InsertBraces: true # only for clang-format version15.0.0 or later
19-
20-
# About include
21-
IncludeBlocks: Regroup
18+
IncludeBlocks: Preserve
2219
IncludeIsMainRegex: '([-_](test|unittest))?$'
2320
IncludeIsMainSourceRegex: ''
2421

2522
IndentWidth: 4
26-
IndentWrappedFunctionNames: true
2723

28-
# About Point
2924
DerivePointerAlignment: false
3025
PointerAlignment: Left
3126

3227
ReflowComments: true
33-
SortIncludes: true
28+
SortIncludes: false
3429
SortUsingDeclarations: true
3530

36-
# About space
31+
# ABACUS follows the usual C/C++ convention: control statements use a space
32+
# before parentheses (`if (...)`), but function calls/declarations do not
33+
# (`foo(...)`). GNU style defaults to `Always`, which would rewrite calls as
34+
# `foo (...)` and produce noisy, unidiomatic diffs.
35+
SpaceBeforeParens: ControlStatements
36+
3737
SpaceAfterCStyleCast: false
3838
SpaceAfterLogicalNot: false
3939
SpaceBeforeRangeBasedForLoopColon: false
@@ -44,7 +44,6 @@ SpacesInContainerLiterals: false
4444
SpacesInParentheses: false
4545
SpacesInSquareBrackets: false
4646

47-
Standard: c++11
48-
TabWidth: 4
47+
Standard: c++17
4948
UseTab: Never
5049
...

0 commit comments

Comments
 (0)