Last active
January 12, 2023 14:52
-
-
Save taosif7-dreamorbit/cb4ca5a570ba4a68523f4494140fb10b to your computer and use it in GitHub Desktop.
git_hooks.dart file to use with git_hooks, and pubspec.yaml file changes for setting up git_hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include: package:flutter_lints/flutter.yaml | |
# Additional information about this file can be found at | |
# https://dart.dev/guides/language/analysis-options | |
# For information on linting rules refer this | |
# https://dart.dev/tools/linter-rules | |
analyzer: | |
exclude: [ build/**, tools/*] | |
errors: | |
unused_element: false | |
linter: | |
rules: | |
avoid_print: true | |
always_declare_return_types: true | |
avoid_empty_else: true | |
use_enums: true | |
always_use_package_imports: true | |
avoid_relative_lib_imports: true | |
avoid_type_to_string: true | |
avoid_types_as_parameter_names: true | |
cancel_subscriptions: true | |
close_sinks: true | |
discarded_futures: true | |
hash_and_equals: true | |
no_duplicate_case_values: true | |
no_logic_in_create_state: true | |
throw_in_finally: true | |
unnecessary_statements: true | |
unused_element: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:dart_pre_commit/dart_pre_commit.dart'; | |
import 'package:git_hooks/git_hooks.dart'; | |
void main(List<String> arguments) { | |
Map<Git, UserBackFun> params = { | |
Git.commitMsg: _conventionalCommitMsg, | |
Git.preCommit: _preCommit, | |
}; | |
GitHooks.call(arguments, params); | |
} | |
Future<bool> _preCommit() async { | |
// Run dart_pre_commit package function to auto run various flutter commands | |
final result = await DartPreCommit.run(); | |
return result.isSuccess; | |
} | |
Future<bool> _conventionalCommitMsg() async { | |
var commitMsg = Utils.getCommitEditMsg(); | |
RegExp conventionCommitPattern = RegExp( | |
r'''^(feat|fix|refactor|build|chore|perf|ci|docs|revert|style|test|merge){1}(\([\w\-\.]+\))?(!)?:( )?([\w ])+([\s\S]*)'''); | |
// Check if it matches conventional commit | |
if (conventionCommitPattern.hasMatch(commitMsg)) { | |
return true; // you can return true let commit go | |
// If failed, check if issue is due to invalid tag | |
} else if (!RegExp(r'(feat|fix|refactor|build|chore|perf|ci|docs|revert|style|test|merge)').hasMatch(commitMsg)) { | |
print( | |
'🛑 Invalid type used in commit message. It should be one of (feat|fix|refactor|build|chore|perf|ci|docs|revert|style|test|merge)'); | |
// else refer the dev to conventional commit site | |
} else { | |
print('🛑 Commit message should follow conventional commit pattern: https://www.conventionalcommits.org/en/v1.0.0/'); | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
dev_dependencies: | |
# Code protocol enforcements | |
flutter_lints: ^2.0.1 | |
dart_pre_commit: ^4.0.0 | |
git_hooks: ^1.0.0 | |
# Due to some issue in dart_test_tools we're overriding it to an older version. Might replace in future | |
dependency_overrides: | |
dart_test_tools: 4.5.3+1 | |
dart_pre_commit: | |
outdated: false # Remove this line if you want to keep all your dependencies upto date | |
pull-up-dependencies: false | |
lib-exports: false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment