Skip to content

Commit

Permalink
Merge pull request #2 from alma/fix/handle-multiple-files
Browse files Browse the repository at this point in the history
Support empty file and multiple arguments
  • Loading branch information
mgu authored Sep 3, 2021
2 parents f4f7b34 + 595279e commit ee211ff
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions pre_commit_hooks/check-comments.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,38 @@ if ! command which jq &>/dev/null; then
exit 1
fi

if [[ "$(cloc --json "$@" | jq '.SUM.comment')" -eq 0 ]]; then
>&2 echo "File $@ contains 0 comments"
exit 1
fi
has_errors=0

function check_file
{
local file_path="$1"
local stats=$(cloc --json "$file_path")
if [ -z "$stats" ]
then
# no stats, empty file?
return
fi

local lines_count=$(echo "$stats" | jq '.header.n_lines')
if [ "$lines_count" -le 1 ]
then
# empty file
return
fi

local comments_count=$(echo "$stats" | jq '.SUM.comment')
if [ "$comments_count" -eq 0 ]
then
>&2 echo "File $@ contains 0 comments"
has_errors=1
fi

# all good!
return
}

exit 0
for file_path in "$@"
do
check_file "$file_path"
done
exit $has_errors

0 comments on commit ee211ff

Please sign in to comment.