Skip to content

Commit 92bbf6d

Browse files
authored
Moving all Alma pre-commit hooks under the same hood
2 parents f0e9df9 + 1534f7b commit 92bbf6d

4 files changed

Lines changed: 79 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.mypy_cache/
22
*.egg-info/
33
__pycache__/
4+
.idea/*

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@
44
entry: check-file-change
55
language: python
66
files: ^$
7+
- id: check-comments
8+
name: Count comments in files
9+
description: Check if file contains at least more than 0% of comments
10+
entry: pre_commit_hooks/check-comments.sh
11+
language: script
12+
types: [python]

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ Some Alma hooks for pre-commit.
66
See also: https://github.com/pre-commit/pre-commit
77

88

9-
### Using pre-commit-hooks with pre-commit
9+
## Using pre-commit-hooks with pre-commit
1010

11-
Add this to your `.pre-commit-config.yaml`
11+
Add this to your `.pre-commit-config.yaml`.
12+
Only use the sections of the checks you want to use (each section starts with `- id: a-check-name`.
1213

1314
```yaml
1415
- repo: https://github.com/alma/pre-commit-hooks
@@ -21,12 +22,25 @@ Add this to your `.pre-commit-config.yaml`
2122
- config.dev.yaml:eacaec4b8c4665dfce2d0e082acd0d39787457da
2223
- config.prod.yaml:eacaec4b8c4665dfce2d0e082acd0d39787457da
2324
- --
25+
- id: check-comments
2426
```
2527
26-
### Hooks available
28+
## Hooks available
2729
28-
#### check-file-change
30+
The hooks below are available in this repository.
31+
32+
### check-file-change
2933
3034
Make sure files didn't change.
3135
You must provide list of target files with their expected checksum in a given algorithm. (Default is sha1)
3236
You can change the algorithm with the ``--algorithm=sha256`` option.
37+
38+
### check-comment
39+
40+
Checks that the modified files contain at least one line of comment.
41+
42+
#### Dependencies
43+
44+
This hook relies on (should be installed on the local machine / VM):
45+
* [cloc](https://github.com/AlDanial/cloc)
46+
* [jq](https://stedolan.github.io/jq/)

pre_commit_hooks/check-comments.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o pipefail
5+
set -o nounset
6+
7+
DEBUG=${DEBUG:=0}
8+
[[ "$DEBUG" = "1" ]] && set -o xtrace
9+
10+
if ! command which cloc &>/dev/null; then
11+
>&2 echo 'cloc command not found, please install it'
12+
exit 1
13+
fi
14+
15+
if ! command which jq &>/dev/null; then
16+
>&2 echo 'jq command not found, please install it'
17+
exit 1
18+
fi
19+
20+
has_errors=0
21+
22+
function check_file
23+
{
24+
local file_path="$1"
25+
local stats=$(cloc --json "$file_path")
26+
if [ -z "$stats" ]
27+
then
28+
# no stats, empty file?
29+
return
30+
fi
31+
32+
local lines_count=$(echo "$stats" | jq '.header.n_lines')
33+
if [ "$lines_count" -le 1 ]
34+
then
35+
# empty file
36+
return
37+
fi
38+
39+
local comments_count=$(echo "$stats" | jq '.SUM.comment')
40+
if [ "$comments_count" -eq 0 ]
41+
then
42+
>&2 echo "File $@ contains 0 comments"
43+
has_errors=1
44+
fi
45+
46+
# all good!
47+
return
48+
}
49+
50+
for file_path in "$@"
51+
do
52+
check_file "$file_path"
53+
done
54+
exit $has_errors

0 commit comments

Comments
 (0)