Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ci: add git hooks #13

Merged
merged 13 commits into from
Apr 18, 2022
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ You may open this project on your favourite IDE that supports Flutter through a

If you are confused you can pick using VSCode with the flutter extension or use Android Studio with Flutter plugin installed. If you have the flutter-sdk and dart-sdk you can directly run the below commands to build this app.

```bash
# Setup git hooks
git config core.hooksPath hooks/
```

```bash
# Disable analytics
flutter config --no-analytics
Expand Down
10 changes: 9 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
Expand All @@ -9,6 +10,13 @@
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

analyzer:
exclude: [build/**]
language:
strict-casts: true
strict-inference: true
strict-raw-types: true

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
Expand All @@ -23,7 +31,7 @@ linter:
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
39 changes: 39 additions & 0 deletions hooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

if [ -z "$1" ]; then
echo "Missing argument (commit message). Did you try to run this manually?"
exit 1
fi

# This adds the Author Identity like commit -s
SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

# This example catches duplicate Signed-off-by lines.

test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}

commit_title=$(head -n1 < "$1")

if [[ $commit_title == "" ]]; then
exit 0
fi

# ignore merge requests
if echo "$commit_title" | grep -qP "^Merge branch \'"; then
echo "commit-msg hook: ignoring branch merge"
exit 0
fi

# check semantic versioning scheme
if ! echo "$commit_title" | grep -qP '^(?:feat|fix|docs|style|refactor|perf|test|chore)\(?(?:\w+|\s|\-|_)?\)?:\s(?:\s|\w+|\W*)+'; then
echo "commit-msg hook: Your commit title did not follow semantic versioning: $commit_title"
echo "commit-msg hook: Please see https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#commit-message-format"
exit 1
fi


10 changes: 10 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -eo pipefail

echo "pre-commit hook: running 'flutter analyze'..."
flutter analyze --fatal-infos --fatal-warnings
echo "pre-commit hook: running 'flutter test'..."
flutter test
echo "pre-commit hook: running 'dart format'..."
dart format .