Skip to content

Commit 7e60de5

Browse files
feat(ref: no-ref): pre-commit msg
1 parent a57d8b1 commit 7e60de5

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed

.github/hooks/commit-msg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
commit_msg=$(cat .git/COMMIT_EDITMSG)
6+
echo "$commit_msg" | dart run commitlint_cli --edit "$1"

.github/hooks/pre-commit

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# To avoid running this pre-commit simply write your commit like:
2+
# git commit -m "test commit" --no-verify
3+
# with the --no-verify flag being the operative addition
4+
5+
printf "\e[33;1m%s\e[0m\n" 'Pre-Commit'
6+
7+
# Undo the stash of the files
8+
pop_stash_files () {
9+
if [ -n "$hasChanges" ]; then
10+
printf "\e[33;1m%s\e[0m\n" '=== Applying git stash changes ==='
11+
git stash pop
12+
fi
13+
}
14+
15+
# Stash unstaged files
16+
hasChanges=$(git diff)
17+
if [ -n "$hasChanges" ]; then
18+
printf "\e[33;1m%s\e[0m\n" 'Stashing unstaged changes'
19+
git stash push --keep-index
20+
fi
21+
22+
# Flutter import sorter
23+
printf "\e[33;1m%s\e[0m\n" '=== Running Dart import sorter ==='
24+
dart run import_sorter:main
25+
hasNewFilesSorted=$(git diff)
26+
if [ -n "$hasNewFilesSorted" ]; then
27+
git add .
28+
printf "\e[33;1m%s\e[0m\n" 'Sorted imports added to git stage'
29+
fi
30+
printf "\e[33;1m%s\e[0m\n" 'Finished running Flutter import sorter'
31+
printf '%s\n' "${avar}"
32+
33+
# Dart fix
34+
printf "\e[33;1m%s\e[0m\n" '=== Running Dart fix ==='
35+
dart fix --apply
36+
hasNewFilesFixed=$(git diff)
37+
if [ -n "$hasNewFilesFixed" ]; then
38+
git add .
39+
printf "\e[33;1m%s\e[0m\n" 'Dart fix files added to git stage'
40+
fi
41+
printf "\e[33;1m%s\e[0m\n" 'Finished running Dart fix'
42+
printf '%s\n' "${avar}"
43+
44+
# Dart formatter
45+
printf "\e[33;1m%s\e[0m\n" '=== Running Dart formatter ==='
46+
dart format .
47+
48+
hasNewFilesFormatted=$(git diff)
49+
if [ -n "$hasNewFilesFormatted" ]; then
50+
git add .
51+
printf "\e[33;1m%s\e[0m\n" 'Formatted files added to git stage'
52+
fi
53+
printf "\e[33;1m%s\e[0m\n" 'Finished running Dart Formatter'
54+
printf '%s\n' "${avar}"
55+
56+
# Flutter Analyzer
57+
printf "\e[33;1m%s\e[0m\n" '=== Running Flutter analyzer ==='
58+
flutter analyze
59+
60+
if [ $? -ne 0 ]; then
61+
printf "\e[31;1m%s\e[0m\n" '=== Flutter analyzer error ==='
62+
pop_stash_files
63+
exit 1
64+
fi
65+
66+
printf "\e[33;1m%s\e[0m\n" 'Finished running Flutter analyzer'
67+
printf '%s\n' "${avar}"
68+
69+
70+
# Unit tests
71+
printf "\e[33;1m%s\e[0m\n" '=== Running Unit Tests ==='
72+
flutter test -r expanded
73+
74+
if [ $? -ne 0 ]; then
75+
printf "\e[31;1m%s\e[0m\n" '=== Unit tests error ==='
76+
pop_stash_files
77+
exit 1
78+
fi
79+
80+
printf "\e[33;1m%s\e[0m\n" 'Finished running Unit Tests'
81+
printf '%s\n' "${avar}"
82+
83+
pop_stash_files
84+
85+
printf "\e[33;1m%s\e[0m\n" 'Finished all tasks'

.github/scripts/setup_hooks.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
3+
# Define the directory containing sample hooks
4+
SAMPLE_HOOKS_DIR=".github/hooks"
5+
6+
# Define the target directory for Git hooks
7+
GIT_HOOKS_DIR=".git/hooks"
8+
9+
# Function to copy or replace hooks
10+
copy_or_replace_hooks() {
11+
for hook in "$SAMPLE_HOOKS_DIR"/*; do
12+
hook_name=$(basename "$hook")
13+
target_hook="$GIT_HOOKS_DIR/"
14+
if [ -f "$target_hook" ]; then
15+
echo "Replacing existing hook: $hook_name"
16+
else
17+
echo "Copying new hook: $hook_name"
18+
fi
19+
if [ -L "$target_hook$hook_name" ]; then
20+
rm -r $target_hook$hook_name
21+
fi
22+
23+
cp $hook $target_hook
24+
chmod +x $target_hook$hook_name # Ensure executable permission is set
25+
done
26+
27+
git config --unset core.hooksPath
28+
}
29+
30+
# Main function
31+
main() {
32+
# Check if .git/hooks directory exists
33+
if [ ! -d "$GIT_HOOKS_DIR" ]; then
34+
echo "Error: .git/hooks directory not found. Are you in a Git repository?"
35+
exit 1
36+
fi
37+
38+
# Copy or replace hooks
39+
copy_or_replace_hooks
40+
41+
echo "Git hooks setup complete."
42+
}
43+
44+
# Run the main function
45+
main

commitlint.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
include: package:commitlint_cli/commitlint.yaml
2+
3+
rules:
4+
scope-empty:
5+
- 2
6+
- never
7+
scope-enum:
8+
- 0
9+
- never

pubspec.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ packages:
3030
url: "https://pub.dev"
3131
source: hosted
3232
version: "0.11.3"
33+
ansi:
34+
dependency: transitive
35+
description:
36+
name: ansi
37+
sha256: "070af96189f9da6f996cee46049682bdcd1d191b483e13f9d2a2600729d8b2a1"
38+
url: "https://pub.dev"
39+
source: hosted
40+
version: "0.4.2"
3341
args:
3442
dependency: transitive
3543
description:
@@ -54,6 +62,14 @@ packages:
5462
url: "https://pub.dev"
5563
source: hosted
5664
version: "2.1.1"
65+
change_case:
66+
dependency: transitive
67+
description:
68+
name: change_case
69+
sha256: f4e08feaa845e75e4f5ad2b0e15f24813d7ea6c27e7b78252f0c17f752cf1157
70+
url: "https://pub.dev"
71+
source: hosted
72+
version: "1.1.0"
5773
characters:
5874
dependency: transitive
5975
description:
@@ -102,6 +118,14 @@ packages:
102118
url: "https://pub.dev"
103119
source: hosted
104120
version: "1.19.0"
121+
commitlint_cli:
122+
dependency: "direct dev"
123+
description:
124+
name: commitlint_cli
125+
sha256: "7882210129f2616d4899ad85073a75ef293deac693dd6f7dc4f0492b0758322b"
126+
url: "https://pub.dev"
127+
source: hosted
128+
version: "0.8.1"
105129
convert:
106130
dependency: transitive
107131
description:
@@ -462,6 +486,14 @@ packages:
462486
url: "https://pub.dev"
463487
source: hosted
464488
version: "2.1.4"
489+
verbose:
490+
dependency: transitive
491+
description:
492+
name: verbose
493+
sha256: "8e63580e35d58a15e4fca702fe91766430b1d28738c160c466e5cb10c373002a"
494+
url: "https://pub.dev"
495+
source: hosted
496+
version: "0.1.1"
465497
vm_service:
466498
dependency: transitive
467499
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dev_dependencies:
1818
sdk: flutter
1919
flutter_lints: ^2.0.0
2020
lint: ^2.3.0
21+
commitlint_cli: ^0.8.1
2122
jsdaddy_custom_lints:
2223
git:
2324
url: https://github.com/JsDaddy/dart-linter-rules

0 commit comments

Comments
 (0)