File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11.mypy_cache /
22* .egg-info /
33__pycache__ /
4+ .idea /*
Original file line number Diff line number Diff line change 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]
Original file line number Diff line number Diff line change @@ -6,9 +6,10 @@ Some Alma hooks for pre-commit.
66See 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
3034Make sure files didn't change.
3135You must provide list of target files with their expected checksum in a given algorithm. (Default is sha1)
3236You 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/)
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments