Skip to content

Commit 608a052

Browse files
committed
First commit
0 parents  commit 608a052

File tree

15 files changed

+392
-0
lines changed

15 files changed

+392
-0
lines changed

.github/workflows/add-tag.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: "Tag"
2+
3+
on:
4+
push:
5+
branches: main
6+
paths: '**.py'
7+
pull_request:
8+
branches: main
9+
paths: '**.py'
10+
workflow_dispatch:
11+
12+
jobs:
13+
add-tag:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v3
18+
- name: retag latest commit for testing
19+
run: |
20+
git config user.name 'github-actions'
21+
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
22+
git push --delete origin latest || true
23+
git tag -a latest -m 'Retag latest commit'
24+
git push origin latest

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
dist
3+
cpp_linter_hooks.egg-info

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
repos:
2+
- repo: https://github.com/shenxianpeng/cpp-linter-hooks
3+
rev: latest
4+
hooks:
5+
- id: clang-format
6+
verbose: true
7+
args: [--style=Google, --version=13]

.pre-commit-hooks.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
# This file tells https://pre-commit.com/ which hooks
3+
# are provided by this repo for use by other git repos.
4+
5+
- id: clang-format
6+
name: clang-format
7+
description: Automatically run `clang-format` against C/C++ source code
8+
entry: clang-format
9+
language: python
10+
files: \.(h\+\+|h|hh|hxx|hpp|c|cc|cpp|c\+\+|cxx)$
11+
types_or: [c, c++, c#, objective-c]
12+
13+
# - id: clang-tidy
14+
# name: clang-tidy
15+
# description: Run `clang-tidy` against C/C++ code to find warnings/errors
16+
# language: python
17+
# files: \.(h\+\+|h|hh|hxx|hpp|c|cc|cpp|c\+\+|cxx)$
18+
# types_or: [c, c++, c#, objective-c]
19+
# entry: clang-tidy
20+
21+
# - id: docker-clang-format
22+
# name: clang-format
23+
# description: Run `clang-format` against C/C++ source code in Docker container
24+
# language: docker
25+
# files: \.(h\+\+|h|hh|hxx|hpp|cuh|c|cc|cpp|cu|c\+\+|cxx|tpp|txx)$
26+
# entry: clang-format -i
27+
# args: ["-style=Google"]
28+
29+
# - id: docker-clang-tidy
30+
# name: clang-tidy
31+
# description: Run `clang-tidy` against C/C++ code to find warnings/errors in Docker container
32+
# language: docker
33+
# types_or: [c, c++, c#, objective-c]
34+
# entry: clang-tidy

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Peter Shen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# C/C++ linter hooks for pre-commit
2+
3+
Linter your C/C++ code with `clang-format` and `clang-tidy` for [pre-commit](https://pre-commit.com/).
4+
5+
**Automaticlly install `clang-format` and `clang-tidy`** when they do not exist.
6+
7+
## Usage
8+
9+
For example
10+
11+
```yaml
12+
repos:
13+
- repo: https://github.com/shenxianpeng/cpp-linter-hooks
14+
hooks:
15+
- id: clang-format
16+
args: ["--style=Google", "--version=14"]
17+
- id: clang-tidy
18+
```

cpp_linter_hooks/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
from cpp_linter_hooks.util import get_expect_version, check_installed
3+
4+
5+
clang_tools = ['clang-format', 'clang-tidy']
6+
args = list(sys.argv[1:])
7+
8+
expect_version = get_expect_version(args)
9+
10+
for tool in clang_tools:
11+
if expect_version:
12+
check_installed(tool, version=expect_version)
13+
else:
14+
check_installed(tool)

cpp_linter_hooks/clang_format.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import subprocess
2+
from cpp_linter_hooks import args, expect_version
3+
4+
5+
def run_clang_format(args) -> int:
6+
if expect_version:
7+
command = [f'clang-format-{expect_version}', '-i']
8+
else:
9+
command = ["clang-format", '-i']
10+
for arg in args:
11+
if arg == expect_version or arg.startswith("--version"):
12+
continue
13+
command.append(arg)
14+
try:
15+
subprocess.run(command, stdout=subprocess.PIPE)
16+
return 0
17+
except FileNotFoundError:
18+
return 1
19+
20+
21+
def main() -> int:
22+
run_clang_format(args)
23+
24+
25+
if __name__ == "__main__":
26+
raise SystemExit(main())

cpp_linter_hooks/clang_tidy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def clang_tidy(*args) -> None:
2+
pass

cpp_linter_hooks/util.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import subprocess
2+
3+
4+
def check_installed(tool, version='13'):
5+
command = [f'{tool}-{version} ', '--version']
6+
try:
7+
subprocess.run(command, stdout=subprocess.PIPE)
8+
except FileNotFoundError:
9+
# clang-tools exist because install_requires=['clang-tools'] in setup.py
10+
subprocess.run(['clang-tools', '-i', version], stdout=subprocess.PIPE)
11+
12+
13+
def get_expect_version(args):
14+
for arg in args:
15+
if arg.startswith("--version") : # expect specific clang-tools version.
16+
# If --version is passed in as 2 arguments, the second is version
17+
if arg == "--version" and args.index(arg) != len(args) - 1:
18+
expect_version = args[args.index(arg) + 1]
19+
# Expected split of --version=14 or --version 14
20+
else:
21+
expect_version = arg.replace(" ", "").replace("=", "").replace("--version", "")
22+
return expect_version

0 commit comments

Comments
 (0)