Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,41 @@ By default there is no minimum description length. Enforce one with
commit-guard --min-description-length 10
```

### Subject format

By default the description must start with a lowercase letter. To allow
uppercase descriptions:

```bash
commit-guard --no-require-lowercase
```

In `.commit-guard.toml`:

```toml
require-lowercase = false
```

By default trailing `.` is forbidden. To change the set of forbidden trailing
characters (any character is valid, including space):

```bash
commit-guard --no-trailing-chars ".,"
commit-guard --no-trailing-chars ".,!"
```

In `.commit-guard.toml`:

```toml
no-trailing-chars = [".", "!"]
```

Pass an empty list to disable the check entirely:

```toml
no-trailing-chars = []
```

### Type validation

By default the standard conventional commit types are accepted. Use `--types`
Expand Down Expand Up @@ -149,7 +184,8 @@ independently of `--enable`/`--disable`.

Place `.commit-guard.toml` in your project root (or any parent directory) to
set defaults for `enable`, `disable`, `scopes`, `require-scope`, `types`,
`max-subject-length`, `min-description-length`, and `require-trailers`.
`max-subject-length`, `min-description-length`, `require-lowercase`,
`no-trailing-chars`, and `require-trailers`.
commit-guard searches upward from the working directory and uses the first
file found.

Expand All @@ -161,6 +197,8 @@ require-scope = true
types = ["feat", "fix", "chore", "wip"]
max-subject-length = 100
min-description-length = 10
require-lowercase = false
no-trailing-chars = [".", "!"]
require-trailers = ["Closes", "Reviewed-by"]
```

Expand All @@ -170,7 +208,8 @@ enable = ["subject", "imperative"]
```

CLI flags (`--enable`, `--disable`, `--scopes`, `--require-scope`, `--types`,
`--max-subject-length`, `--min-description-length`, `--require-trailer`) take
`--max-subject-length`, `--min-description-length`, `--no-require-lowercase`,
`--no-trailing-chars`, `--require-trailer`) take
full precedence and ignore config file values when provided.

### Environment variables
Expand Down Expand Up @@ -321,6 +360,8 @@ jobs:
require-trailer: 'Closes,Reviewed-by'
max-subject-length: '100'
min-description-length: '10'
no-require-lowercase: 'true'
no-trailing-chars: '.,!'
allow-empty: 'true'
include-merges: 'true'
output-file: results.jsonl
Expand Down
11 changes: 11 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ inputs:
min-description-length:
description: Minimum description length in characters (default 0, off)
required: false
no-require-lowercase:
description: Allow description to start with uppercase (default false)
required: false
default: 'false'
no-trailing-chars:
description: Forbidden trailing characters, comma-separated (default '.')
required: false
allow-empty:
description: Exit 0 when --range yields no commits
required: false
Expand Down Expand Up @@ -75,6 +82,8 @@ runs:
CG_TYPES: ${{ inputs.types }}
CG_MAX_SUBJECT_LENGTH: ${{ inputs.max-subject-length }}
CG_MIN_DESCRIPTION_LENGTH: ${{ inputs.min-description-length }}
CG_NO_REQUIRE_LOWERCASE: ${{ inputs.no-require-lowercase }}
CG_NO_TRAILING_CHARS: ${{ inputs.no-trailing-chars }}
CG_ALLOW_EMPTY: ${{ inputs.allow-empty }}
CG_INCLUDE_MERGES: ${{ inputs.include-merges }}
CG_REQUIRE_TRAILER: ${{ inputs.require-trailer }}
Expand All @@ -92,6 +101,8 @@ runs:
ARGS+=(--max-subject-length "$CG_MAX_SUBJECT_LENGTH")
[[ -n "$CG_MIN_DESCRIPTION_LENGTH" ]] && \
ARGS+=(--min-description-length "$CG_MIN_DESCRIPTION_LENGTH")
[[ "$CG_NO_REQUIRE_LOWERCASE" == "true" ]] && ARGS+=(--no-require-lowercase)
[[ -n "$CG_NO_TRAILING_CHARS" ]] && ARGS+=(--no-trailing-chars "$CG_NO_TRAILING_CHARS")
[[ "$CG_ALLOW_EMPTY" == "true" ]] && ARGS+=(--allow-empty)
[[ "$CG_INCLUDE_MERGES" == "true" ]] && ARGS+=(--include-merges)
[[ -n "$CG_REQUIRE_TRAILER" ]] && ARGS+=(--require-trailer "$CG_REQUIRE_TRAILER")
Expand Down
8 changes: 6 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ <h2>Configuration <a href="#configuration" class="anchor">#</a></h2>
types = ["feat", "fix", "chore", "wip"]
max-subject-length = 100
min-description-length = 10
require-lowercase = false
no-trailing-chars = [".", "!"]
require-trailers = ["Closes", "Reviewed-by"]</code></pre>

<h3>Required trailers</h3>
Expand Down Expand Up @@ -492,8 +494,10 @@ <h2>GitHub Actions <a href="#github-actions" class="anchor">#</a></h2>
<code>range</code>, <code>enable</code>, <code>disable</code>,
<code>scopes</code>, <code>require-scope</code>, <code>types</code>,
<code>max-subject-length</code>, <code>min-description-length</code>,
<code>require-trailer</code>, <code>allow-empty</code>,
<code>include-merges</code>, <code>output-file</code>.
<code>no-require-lowercase</code>, <code>no-trailing-chars</code>,
<code>require-trailer</code>,
<code>allow-empty</code>, <code>include-merges</code>,
<code>output-file</code>.
</p>

<p>
Expand Down
47 changes: 43 additions & 4 deletions src/git_commit_guard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,17 @@ def _strip_comments(message):
)


def check_subject( # noqa: PLR0913 Too many arguments in function definition (7 > 5)
def check_subject( # noqa: PLR0913 Too many arguments in function definition (9 > 5)
line,
result,
allowed_scopes=frozenset(),
allowed_types=TYPES,
max_subject_length=MAX_SUBJECT_LEN,
min_description_length=0,
no_trailing_chars=frozenset("."),
*,
require_scope=False,
require_lowercase=True,
):
m = SUBJECT_RE.match(line)
if not m:
Expand All @@ -174,10 +176,10 @@ def check_subject( # noqa: PLR0913 Too many arguments in function definition (7
result.error(f"unknown scope: {scope}", check=Check.SUBJECT)

desc = m.group("desc")
if desc[0].isupper():
if require_lowercase and desc[0].isupper():
result.error("description must not start with uppercase", check=Check.SUBJECT)
if desc.endswith("."):
result.error("description must not end with period", check=Check.SUBJECT)
if no_trailing_chars and desc[-1] in no_trailing_chars:
result.error(f"description must not end with {desc[-1]!r}", check=Check.SUBJECT)
if len(line) > max_subject_length:
result.error(
f"subject too long: {len(line)} > {max_subject_length}", check=Check.SUBJECT
Expand Down Expand Up @@ -305,6 +307,8 @@ class Args:
allowed_types: frozenset
max_subject_length: int
min_description_length: int
require_lowercase: bool
no_trailing_chars: frozenset
rev_range: str | None
allow_empty: bool
include_merges: bool
Expand Down Expand Up @@ -345,6 +349,22 @@ def _resolve_min_description_length(args, config):
return 0


def _resolve_require_lowercase(args, config):
if args.require_lowercase is not None:
return args.require_lowercase
if "require-lowercase" in config:
return config["require-lowercase"]
return True


def _resolve_no_trailing_chars(args, config):
if args.no_trailing_chars is not None:
return frozenset(c for c in args.no_trailing_chars.split(",") if c)
if "no-trailing-chars" in config:
return frozenset(config["no-trailing-chars"])
return frozenset(".")


def _resolve_required_trailers(args, config):
if args.require_trailer:
return [t.strip() for t in args.require_trailer.split(",")]
Expand Down Expand Up @@ -431,6 +451,19 @@ def _parse_args():
metavar="N",
help="minimum description length in characters (default: 0, off)",
)
parser.add_argument(
"--no-require-lowercase",
dest="require_lowercase",
action="store_false",
default=None,
help="allow description to start with uppercase (default: disallowed)",
)
parser.add_argument(
"--no-trailing-chars",
default=None,
metavar="CHAR[,CHAR,...]",
help="forbidden trailing characters in description (default: '.')",
)
parser.add_argument(
"--range",
dest="rev_range",
Expand Down Expand Up @@ -473,6 +506,8 @@ def _parse_args():
allowed_types = _resolve_types(args, config)
max_subject_length = _resolve_max_subject_length(args, config)
min_description_length = _resolve_min_description_length(args, config)
require_lowercase = _resolve_require_lowercase(args, config)
no_trailing_chars = _resolve_no_trailing_chars(args, config)
required_trailers = _resolve_required_trailers(args, config)

if args.allow_empty and not args.rev_range:
Expand Down Expand Up @@ -507,6 +542,8 @@ def _parse_args():
allowed_types=allowed_types,
max_subject_length=max_subject_length,
min_description_length=min_description_length,
require_lowercase=require_lowercase,
no_trailing_chars=no_trailing_chars,
rev_range=args.rev_range,
allow_empty=args.allow_empty,
include_merges=args.include_merges,
Expand Down Expand Up @@ -560,7 +597,9 @@ def _run_checks(args, rev, message, result):
args.allowed_types,
args.max_subject_length,
args.min_description_length,
args.no_trailing_chars,
require_scope=args.require_scope,
require_lowercase=args.require_lowercase,
)
if Check.IMPERATIVE in args.enabled:
if desc is None:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_git_commit_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,41 @@ def test_uppercase_description(self):
check_subject("fix: Add token", r)
assert not r.ok

def test_uppercase_description_allowed(self):
r = Result()
check_subject("fix: Add token", r, require_lowercase=False)
assert r.ok

def test_lowercase_required_by_default(self):
r = Result()
check_subject("fix: add token", r)
assert r.ok

def test_trailing_period(self):
r = Result()
check_subject("fix: add token.", r)
assert not r.ok

def test_trailing_char_custom(self):
r = Result()
check_subject("fix: add token!", r, no_trailing_chars=frozenset("!"))
assert not r.ok

def test_trailing_char_space(self):
r = Result()
check_subject("fix: add token ", r, no_trailing_chars=frozenset(". "))
assert not r.ok

def test_trailing_chars_empty_disables_check(self):
r = Result()
check_subject("fix: add token.", r, no_trailing_chars=frozenset())
assert r.ok

def test_trailing_chars_multiple(self):
r = Result()
check_subject("fix: add token!", r, no_trailing_chars=frozenset(".!"))
assert not r.ok

def test_subject_too_long(self):
r = Result()
check_subject("fix: " + "a" * 68, r) # 73 chars total
Expand Down
Loading