|
| 1 | +--- |
| 2 | +description: General information based on the latest ./README.md content |
| 3 | +globs: |
| 4 | +--- |
| 5 | +Update it if APIs change: |
| 6 | + |
| 7 | +# GitLint |
| 8 | + |
| 9 | +> Efficient Git Commit Message Linting and Formatting |
| 10 | + |
| 11 | +GitLint is a tool for enforcing consistent Git commit message conventions. It analyzes commit messages to ensure they follow the [Conventional Commits](https://www.conventionalcommits.org/) specification and other configurable rules. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +# Install globally |
| 17 | +npm install -g @stacksjs/gitlint |
| 18 | + |
| 19 | +# Or using bun |
| 20 | +bun install -g @stacksjs/gitlint |
| 21 | +``` |
| 22 | + |
| 23 | +_We are looking to get it published as `gitlint` on npm, but it's not allowing us to do so due to `git-lint`. Please npm 🙏🏽_ |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +### CLI |
| 28 | + |
| 29 | +```bash |
| 30 | +# Check a commit message from a file |
| 31 | +gitlint path/to/commit-message.txt |
| 32 | + |
| 33 | +# Use with git commit message hook (common use case) |
| 34 | +gitlint --edit $1 |
| 35 | + |
| 36 | +# Show help |
| 37 | +gitlint --help |
| 38 | +``` |
| 39 | + |
| 40 | +### Git Hooks Integration |
| 41 | + |
| 42 | +GitLint can automatically install Git hooks for your repository: |
| 43 | + |
| 44 | +```bash |
| 45 | +# Install the commit-msg hook |
| 46 | +gitlint hooks --install |
| 47 | + |
| 48 | +# Force overwrite if a hook already exists |
| 49 | +gitlint hooks --install --force |
| 50 | + |
| 51 | +# Uninstall the hooks |
| 52 | +gitlint hooks --uninstall |
| 53 | +``` |
| 54 | + |
| 55 | +Or manually add to your `.git/hooks/commit-msg` file: |
| 56 | + |
| 57 | +```bash |
| 58 | +#!/bin/sh |
| 59 | +gitlint --edit "$1" |
| 60 | +``` |
| 61 | + |
| 62 | +Or use with [husky](https://github.com/typicode/husky): |
| 63 | + |
| 64 | +```jsonc |
| 65 | +// package.json |
| 66 | +{ |
| 67 | + "husky": { |
| 68 | + "hooks": { |
| 69 | + "commit-msg": "gitlint --edit $HUSKY_GIT_PARAMS" |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## Configuration |
| 76 | + |
| 77 | +Create a `gitlint.config.js` file in your project root: |
| 78 | + |
| 79 | +```js |
| 80 | +// gitlint.config.js |
| 81 | +module.exports = { |
| 82 | + verbose: true, |
| 83 | + rules: { |
| 84 | + 'conventional-commits': 2, |
| 85 | + 'header-max-length': [2, { maxLength: 72 }], |
| 86 | + 'body-max-line-length': [2, { maxLength: 100 }], |
| 87 | + 'body-leading-blank': 2, |
| 88 | + 'no-trailing-whitespace': 1 |
| 89 | + }, |
| 90 | + ignores: [ |
| 91 | + '^Merge branch', |
| 92 | + '^Merge pull request' |
| 93 | + ] |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Rule Levels |
| 98 | + |
| 99 | +- `0` or `off`: Disable the rule |
| 100 | +- `1` or `warning`: Warning (doesn't cause exit code to be non-zero) |
| 101 | +- `2` or `error`: Error (causes exit code to be non-zero) |
| 102 | + |
| 103 | +## Built-in Rules |
| 104 | + |
| 105 | +- `conventional-commits`: Enforces conventional commit format (`<type>(scope): description`) |
| 106 | +- `header-max-length`: Enforces a maximum header length |
| 107 | +- `body-max-line-length`: Enforces a maximum body line length |
| 108 | +- `body-leading-blank`: Enforces a blank line between header and body |
| 109 | +- `no-trailing-whitespace`: Checks for trailing whitespace |
| 110 | + |
| 111 | +## Programmatic Usage |
| 112 | + |
| 113 | +```js |
| 114 | +import { lintCommitMessage, parseCommitMessage } from '@stacksjs/gitlint' |
| 115 | + |
| 116 | +// Lint a commit message |
| 117 | +const result = lintCommitMessage('feat: add new feature') |
| 118 | +console.log(result.valid) // true or false |
| 119 | +console.log(result.errors) // array of error messages |
| 120 | +console.log(result.warnings) // array of warning messages |
| 121 | + |
| 122 | +// Parse a commit message |
| 123 | +const parsed = parseCommitMessage('feat(scope): description\n\nBody text\n\nCloses #123') |
| 124 | +console.log(parsed.type) // 'feat' |
| 125 | +console.log(parsed.scope) // 'scope' |
| 126 | +console.log(parsed.references) // [{issue: '123', ...}] |
| 127 | +``` |
| 128 | + |
| 129 | +## Testing |
| 130 | + |
| 131 | +```bash |
| 132 | +bun test |
| 133 | +``` |
0 commit comments