Skip to content

Commit 95cdae0

Browse files
authored
Merge pull request #3 from db-ui/feature/pre_commit_hooks
feat: Pre-Commit hooks
2 parents d9b87a6 + e1cd154 commit 95cdae0

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

.env.template

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# It will force the user to add an e-mail for this project, before committing.
2+

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
.idea
44
.vscode
55
.kotlin
6+
7+
.env
8+
69
/local.properties
710
.DS_Store
811
/build

build.gradle.kts

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,13 @@ plugins {
33
alias(libs.plugins.com.android.library) apply false
44
alias(libs.plugins.jetbrains.kotlin.android) apply false
55
alias(libs.plugins.compose.compiler) apply false
6-
}
6+
}
7+
task<Copy>("installGitHook") {
8+
delete(".git/hooks/pre-commit")
9+
10+
fileMode = 0x777
11+
from(File(rootProject.rootDir, "scripts/pre-commit"))
12+
into(File(rootProject.rootDir, ".git/hooks"))
13+
}
14+
15+
tasks.getByPath(":app:preBuild").dependsOn(":installGitHook")

scripts/check-commit-mail.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env node
2+
import * as ChildProcess from 'node:child_process';
3+
import * as process from 'node:process';
4+
5+
const checkCommitMail = () => {
6+
console.warn(`Check COMMIT_MAIL`);
7+
if (!process.env.COMMIT_MAIL) {
8+
console.error(
9+
`No COMMIT_MAIL set in .env, please take a look at the file '.env.template'`
10+
);
11+
process.exit(1);
12+
}
13+
14+
const currentMail = ChildProcess.execSync('git config user.email')
15+
.toString()
16+
.trim()
17+
.toLowerCase();
18+
const commitMail = process.env.COMMIT_MAIL.trim().toLowerCase();
19+
if (currentMail !== commitMail) {
20+
console.error(
21+
`currentMail: ${currentMail} !== initialMail: ${commitMail}`
22+
);
23+
console.error(
24+
`Please set your commit user mail for this project like: 'git config user.email '${commitMail}''`
25+
);
26+
process.exit(1);
27+
}
28+
};
29+
30+
checkCommitMail();

scripts/pre-commit

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
git stash -q --keep-index
4+
5+
echo "Running git pre-commit hook"
6+
7+
./gradlew lint &&
8+
node --env-file=.env ./scripts/check-commit-mail.js &&
9+
node ./scripts/validate-branch-name.js
10+
11+
RESULT=$?
12+
13+
git stash pop -q
14+
15+
# return 1 exit code if running checks fails
16+
[ $RESULT -ne 0 ] && exit 1
17+
exit 0

scripts/validate-branch-name.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env node
2+
import * as ChildProcess from 'node:child_process';
3+
import * as process from 'node:process';
4+
5+
const checkValidBranchName = () => {
6+
console.warn(`Check VALID_BRANCH_NAME`);
7+
8+
const currentBranchName = ChildProcess.execSync('git symbolic-ref --short HEAD')
9+
.toString()
10+
.trim()
11+
12+
const regex = /^(feature|fix|hotfix|release)\/.+$/g;
13+
const found = currentBranchName.match(regex) !== null;
14+
process.exit(found ? 0 : 1);
15+
};
16+
17+
checkValidBranchName();

0 commit comments

Comments
 (0)