Skip to content

Commit

Permalink
feat: ✨ add githook
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgrv committed Jun 22, 2024
1 parent 63ee583 commit 2fcc56e
Show file tree
Hide file tree
Showing 20 changed files with 538 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
]
},
"extensions": [
"mads-hartmann.bash-ide-vscode"
"mads-hartmann.bash-ide-vscode",
"mhutchie.git-graph"
]
}
},
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"../src/gitutils": {},
"../src/gitversion": {}
"ghcr.io/tomgrv/devcontainer-features/gitutils": {},
"ghcr.io/tomgrv/devcontainer-features/githooks": {}
},
"remoteUser": "node",
"updateContentCommand": "npm install -g @devcontainers/cli"
}
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.devcontainer/githooks/_commit-and-tag-version.json
.devcontainer/githooks/_commitlint.json
.devcontainer/githooks/_config.json
.devcontainer/githooks/_git-precommit-checks.json
.devcontainer/githooks/_husky.json
.devcontainer/githooks/_lint-staged.json
.devcontainer/githooks/_prettier.json
.devcontainer/githooks/_scripts.json
.devcontainer/githooks/commit-msg
.devcontainer/githooks/config.json
.devcontainer/githooks/config.sh
.devcontainer/githooks/devcontainer-feature.json
.devcontainer/githooks/install.sh
.devcontainer/githooks/post-checkout
.devcontainer/githooks/post-merge
.devcontainer/githooks/pre-commit
.devcontainer/githooks/pre-push
.devcontainer/githooks/prepare-commit-msg
.devcontainer/githooks/README.md
.devcontainer/githooks/_commit-msg.sh
.devcontainer/githooks/_post-checkout.sh
.devcontainer/githooks/_post-merge.sh
.devcontainer/githooks/_pre-commit.sh
.devcontainer/githooks/_pre-push.sh
.devcontainer/githooks/_prepare-commit-msg.sh
131 changes: 131 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"commit-and-tag-version": {
"bumpFiles": [
{
"filename": "composer.json",
"type": "json"
},
{
"filename": "package.json",
"type": "json"
},
{
"filename": "VERSION",
"type": "plain-text"
}
],
"types": [
{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"hidden": true
},
{
"type": "docs",
"hidden": true
},
{
"type": "style",
"hidden": true
},
{
"type": "refactor",
"hidden": true
},
{
"type": "perf",
"hidden": true
},
{
"type": "test",
"hidden": true
}
],
"scripts": {
"prebump": "gitversion -config .gitversion -showvariable MajorMinorPatch"
}
},
"scripts": {
"release": "commit-and-tag-version --no-verify --",
"lint": "lint-staged",
"update": "npm-check-updates -i -u",
"update-all": "npm run update -ws --root",
"test": "echo \"Warning: no test specified\""
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
],
"rules": {
"subject-case": [
2,
"never",
[
"start-case",
"pascal-case",
"upper-case"
]
],
"scope-enum": [
2,
"always",
[
"deps",
"release",
"security",
"i18n",
"config",
"add",
"remove",
"breaking",
"modules",
"packages",
"ui-ux",
"api",
"model"
]
]
}
},
"config": {
"commitizen": {
"path": "@commitlint/cz-commitlint"
}
},
"git-precommit-checks": {
"rules": [
{
"message": "You've got leftover conflict markers",
"regex": "/^[<>|=]{4,}/m"
},
{
"filter": "(^package\\.json|\\.git-precommit-checks.json)$",
"message": "You have unfinished devs",
"nonBlocking": "true",
"regex": "(?:FIXME|TODO)"
}
]
},
"lint-staged": {
"*.{js,jsx,ts,tsx,md,html,css,json,vue, yaml, yml}": [
"prettier --write"
],
"*.php": [
"composer lint"
]
},
"prettier": {
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true,
"insertPragma": true
}
}
42 changes: 42 additions & 0 deletions src/githooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

# Git Utils

This feature provides a set of utilities for working with Git repositories.

The following aliases are included: [./alias.json](./src/gitutils/alias.json)

## Example Usage

```json
"features": {
"ghcr.io/tomgrv/devcontainer-features/gitutils:1": {
"version": "latest"
}
}
```

## Options

| Options Id | Description | Type | Default Value |
|-----|-----|-----|-----|
| version | The version of GitUtils to install. | string | latest |

## GitFlow

Additionnaly, the feature installs the [git-flow](https:://github.com/nvie/gitflow) extension and sets up the Git configuration to use it.

Shortcuts are also added to the `git` command to make it easier to use the `git-flow` commands:

- `git beta` is a shortcut for `git flow release start`
- `git hfix` is a shortcut for `git flow hotfix start`
- `git prod` is a shortcut for `git flow release finish` and `git flow hotfix finish`

Those shortcuts work in cunjunction with the `gitversion` utility to automatically update the version number of the application.

## Interactive Utilities

- `git fixup` - Amend the specified commit with current changes and rebase

## Contributing

If you have a feature that you would like to add to this repository, please open an issue or submit a pull request.
55 changes: 55 additions & 0 deletions src/githooks/_commit-and-tag-version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"commit-and-tag-version": {
"bumpFiles": [
{
"filename": "composer.json",
"type": "json"
},
{
"filename": "package.json",
"type": "json"
},
{
"filename": "VERSION",
"type": "plain-text"
}
],
"types": [
{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"hidden": true
},
{
"type": "docs",
"hidden": true
},
{
"type": "style",
"hidden": true
},
{
"type": "refactor",
"hidden": true
},
{
"type": "perf",
"hidden": true
},
{
"type": "test",
"hidden": true
}
]
},
"scripts": {
"release": "commit-and-tag-version --no-verify --"
}
}
12 changes: 12 additions & 0 deletions src/githooks/_commit-msg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
export PATH=/usr/bin:$PATH

# Enable colors
if [ -t 1 ]; then
exec >/dev/tty 2>&1
fi

# Apply commitlint rules to the latest commit message
npx chalk-cli --no-stdin -t "{blue →} Applying commitlint rules to the latest commit..."
PLUGINS=$(cat package.json | npx jqn '.commitlint.extends' | tr -d "'[]:")
npm list --global $PLUGINS 2>/dev/null 1>&2 || npm install --global $PLUGINS 2>/dev/null 1>&2 && npx commitlint --edit "$1" && npx devmoji -e
31 changes: 31 additions & 0 deletions src/githooks/_commitlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"commitlint": {
"extends": ["@commitlint/config-conventional"],
"rules": {
"subject-case": [
2,
"never",
["start-case", "pascal-case", "upper-case"]
],
"scope-enum": [
2,
"always",
[
"deps",
"release",
"security",
"i18n",
"config",
"add",
"remove",
"breaking",
"modules",
"packages",
"ui-ux",
"api",
"model"
]
]
}
}
}
7 changes: 7 additions & 0 deletions src/githooks/_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"config": {
"commitizen": {
"path": "@commitlint/cz-commitlint"
}
}
}
16 changes: 16 additions & 0 deletions src/githooks/_git-precommit-checks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"git-precommit-checks": {
"rules": [
{
"message": "You've got leftover conflict markers",
"regex": "/^[<>|=]{4,}/m"
},
{
"filter": "(^package\\.json|\\.git-precommit-checks.json)$",
"message": "You have unfinished devs",
"nonBlocking": "true",
"regex": "(?:FIXME|TODO)"
}
]
}
}
13 changes: 13 additions & 0 deletions src/githooks/_lint-staged.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scripts": {
"lint": "lint-staged"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,md,html,css,json,vue, yaml, yml}": [
"prettier --write"
],
"*.php": [
"composer lint"
]
}
}
23 changes: 23 additions & 0 deletions src/githooks/_post-checkout.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh
export PATH=/usr/bin:$PATH

# Enable colors
if [ -t 1 ]; then
exec >/dev/tty 2>&1
fi

# Check if file changed
isChanged() {
git diff --name-only HEAD@{1} HEAD | grep "^$1" >/dev/null 2>&1
}

# Check if rebase
isRebase() {
git rev-parse --git-dir | grep -q 'rebase-merge' || git rev-parse --git-dir | grep -q 'rebase-apply' >/dev/null 2>&1
}

# Check if the current Git command is a rebase
if test "$GIT_COMMAND" = "rebase"; then
npx chalk-cli --no-stdin -t "{green ✔} Skip post-checkout hook during rebase."
exit 0
fi
Loading

0 comments on commit 2fcc56e

Please sign in to comment.